set_time_limit(0);
ignore_user_abort(true);
// this file wants to simulate a real large process that have to be executed in background
require_once __DIR__ . '/../../vendor/autoload.php';
use JLaso\ToolsLib\Json;
use JLaso\ToolsLib\Status;
$id = isset($_POST['id']) ? $_POST['id'] : null;
$fileName = isset($_POST['name']) ? $_POST['name'] : "";
$fileSize = isset($_POST['size']) ? $_POST['size'] : "";
if (null === $id || !$fileName || !$fileSize) {
    Json::error("you have to send 'id','name' and 'size' to start processing");
    exit;
}
$status = new Status($_POST["_task"]);
// if the status file exists means that another instance of this task is working
if ($status->existsStatusFile()) {
    Json::ok(array('id' => $id, 'status' => Status::QUEUED));
    exit;
}
$status->touchStatusFile();
// the next lines terminates the output buffer and let believe the requester that the program had finished
ob_start();
Json::ok();
header("Content-Length: " . ob_get_length());
header('Connection: close');
ob_end_flush();
flush();
session_write_close();
// wait a little before the huge work
sleep(1);
// this is some kind of magic, in order to copy the status file  if something wrong happens