/**
 * @param Status $status
 * @param int $id
 * @param String $name
 * @param int $size
 */
function process(Status $status, $id, $name, $size)
{
    $factor = intval($size / 100);
    for ($i = 0; $i <= $size; $i += $factor) {
        sleep(1);
        // simulate that is copying a piece of the file
        $status->updateStatus($id, Status::PROCESSING . ":" . intval($i / $factor));
    }
    sleep(1);
    $status->updateStatus($id, Status::DONE);
    sleep(2);
}
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use JLaso\ToolsLib\Json;
use JLaso\ToolsLib\Status;
$id = $_REQUEST["id"];
$task = $_REQUEST["_task"];
if (!$id) {
    Json::error('The id is mandatory in order to process your request!');
} else {
    $status = new Status($task);
    Json::ok(array('id' => $id, 'status' => $status->getInfo($id)));
}
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use JLaso\ToolsLib\Json;
use JLaso\ToolsLib\Status;
$id = isset($_REQUEST["id"]) ? intval($_REQUEST["id"]) : null;
$task = $_REQUEST["_task"];
if (null === $id) {
    Json::error('The "id" is mandatory in order to process your request!');
} else {
    $statusService = new Status($task);
    $taskStatus = explode(":", $statusService->getInfo($id));
    $status = isset($taskStatus[0]) ? $taskStatus[0] : 'unknown';
    $percent = isset($taskStatus[1]) ? intval($taskStatus[1]) : ($status == "done" ? 100 : 0);
    Json::ok(array('id' => $id, 'status' => $status, 'percent' => $percent, 'raw' => $statusService->getInfo($id)));
}
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use JLaso\ToolsLib\Json;
use JLaso\ToolsLib\Status;
$task = $_REQUEST["_task"];
$ids = rtrim($_REQUEST["ids"], ",");
if (!$task || !$ids) {
    Json::error('The "_task" and "ids" are mandatory in order to process your request!');
} else {
    $result = array();
    foreach (explode(",", $ids) as $id) {
        $id = intval($id);
        $statusService = new Status($task . "-" . $id);
        $status = $statusService->getInfo($id);
        if (!isset($result[$id])) {
            $result[$id] = array("id" => $id);
            $temp = explode(":", $status);
            $result[$id]["percent"] = isset($temp[1]) ? intval($temp[1]) : 0;
            $result[$id]["status"] = isset($temp[0]) ? $temp[0] : $status;
        }
    }
    Json::ok(array('info' => array_values($result)));
}
Exemplo n.º 5
0
<?php

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;
// get the parameters as JSON
$postParams = Json::getBodyParams();
$id = isset($postParams['id']) ? $postParams['id'] : 0;
if (!$id) {
    Json::error("you have to send and ID to start processing");
    exit;
}
$status = new Status($postParams["_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
<?php

require_once __DIR__ . '/../vendor/autoload.php';
use JLaso\ToolsLib\Status;
$status = new Status();
$st1 = $status->getInfo(1);
print "status for id=1 is " . $st1 . PHP_EOL;
$status->updateStatus(1, "status-#" . rand(10, 30));
$st2 = $status->getInfo(1);
print "and now is " . $st2 . PHP_EOL;