最后我这样解决了:
第一个。以可通过 URL 调用的方式设计操作。例如:http://localhost/render-image/14523665
第二。将挂起的操作存储在如下表中:操作(操作 ID、操作 URL、操作状态)。
准备就绪后,我设计了一个简短的 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';
}
}
我认为它可能对其他人有用。如您所见,它链接了操作。