最后我解决了它那方式:
1st。以可调用URL的方式设计操作。例如:http://localhost/render-image/14523665
2nd。将挂起的操作存储在如下表格中:操作(opertation-id,operation-url,operation-status)。
一旦准备好我设计了一个简短的PHP脚本,执行以下操作:
<?php
if(WORKER_IS_ONLINE !== true) exit();
OperationsQueue::CleanQueue();
while(OperationsQueue::RunningOperations() < WORKER_MAX_OPERATIONS) {
launch_operation(OperationsQueue::DequeOperation());
}
sleep(WORKER_SLEEP_TIME);
launch_operation('/operations_queue/deque');
此附配的功能(launch_operation
和OperationsQueue
类)做如下(注意,这还没有实现):
<?php
define('WORKER_SLEEP_TIME', 15); // Time to sleep between sweeps.
define('WORKER_HOST', 'localhost'); // Hostname where the operations will be performed.
define('WORKER_PORT', 80); // Port where the operations will be performed.
define('WORKER_SOCKET_TIMEOUT', 80); // Port where the operations will be performed.
define('WORKER_MAX_OPERATIONS', 2); // Max simultaneous operations.
define('WORKER_MAX_LAUNCHES', 2); // Max operations launched within a sweep.
define('WORKER_IS_ONLINE', false); // Bool value that marks whether the worker must be working or not.
function launch_operation($operation) {
$fp = fsockopen(WORKER_HOST, WORKER_PORT, $errno, $errstr, WORKER_SOCKET_TIMEOUT);
if ($fp) {
$out = 'GET ' . $operation . " HTTP/1.1\r\n";
$out .= 'Host: ' . WORKER_HOST . "\r\n\r\n";
fwrite($fp, $out);
fclose($fp);
}
}
class OperationsQueue {
public static function RunningOperations(){
// connect to DB an perform: SELECT COUNT(*) FROM operations WHERE status = 'PROCESSING';
return 1;
}
public static function CleanQueue(){
// connect to DB an perform: DELETE FROM operations WHERE status IN ('DONE', 'CANCELED');
}
public static function DequeOperation(){
// connect to DB an perform: SELECT * FROM operations WHERE status = 'PENDING' ORDER BY id ASC LIMIT 0, 1;
// UPDATE operations SET status = 'PROCESSING', tries = tries+1 WHERE id = $id;
return 'operation_url';
}
}
我认为这可能对其他人有用。正如你所看到的,它将操作链接起来
提交到一个队列,并有一个解析队列工作的crontab。 – BugFinder
该进程是否必须在没有webserver调用的服务器(如cron脚本)上运行? – ntvf
“标记为”的行意味着有一列包含此信息?你可以在发生这种情况的服务器上编写并运行脚本吗? – quinestor