1
This commit is contained in:
65
vendor/topthink/think-queue/src/queue/command/Listen.php
vendored
Normal file
65
vendor/topthink/think-queue/src/queue/command/Listen.php
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\queue\command;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\queue\Listener;
|
||||
|
||||
class Listen extends Command
|
||||
{
|
||||
/** @var Listener */
|
||||
protected $listener;
|
||||
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('queue:listen')
|
||||
->addOption('queue', null, Option::VALUE_OPTIONAL, 'The queue to listen on', null)
|
||||
->addOption('delay', null, Option::VALUE_OPTIONAL, 'Amount of time to delay failed jobs', 0)
|
||||
->addOption('memory', null, Option::VALUE_OPTIONAL, 'The memory limit in megabytes', 128)
|
||||
->addOption('timeout', null, Option::VALUE_OPTIONAL, 'Seconds a job may run before timing out', 60)
|
||||
->addOption('sleep', null, Option::VALUE_OPTIONAL, 'Seconds to wait before checking queue for jobs', 3)
|
||||
->addOption('tries', null, Option::VALUE_OPTIONAL, 'Number of times to attempt a job before logging it failed', 0)
|
||||
->setDescription('Listen to a given queue');
|
||||
}
|
||||
|
||||
public function initialize(Input $input, Output $output)
|
||||
{
|
||||
$this->listener = new Listener($this->findCommandPath());
|
||||
$this->listener->setSleep($input->getOption('sleep'));
|
||||
$this->listener->setMaxTries($input->getOption('tries'));
|
||||
|
||||
$this->listener->setOutputHandler(function ($type, $line) use ($output) {
|
||||
$output->write($line);
|
||||
});
|
||||
}
|
||||
|
||||
public function execute(Input $input, Output $output)
|
||||
{
|
||||
$delay = $input->getOption('delay');
|
||||
|
||||
$memory = $input->getOption('memory');
|
||||
|
||||
$timeout = $input->getOption('timeout');
|
||||
|
||||
$queue = $input->getOption('queue') ?: 'default';
|
||||
|
||||
$this->listener->listen($queue, $delay, $memory, $timeout);
|
||||
}
|
||||
|
||||
protected function findCommandPath()
|
||||
{
|
||||
return defined('ROOT_PATH') ? ROOT_PATH : dirname($_SERVER['argv'][0]);
|
||||
}
|
||||
}
|
||||
31
vendor/topthink/think-queue/src/queue/command/Restart.php
vendored
Normal file
31
vendor/topthink/think-queue/src/queue/command/Restart.php
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\queue\command;
|
||||
|
||||
use think\Cache;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\Output;
|
||||
|
||||
class Restart extends Command
|
||||
{
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('queue:restart')->setDescription('Restart queue worker daemons after their current job');
|
||||
}
|
||||
|
||||
public function execute(Input $input, Output $output)
|
||||
{
|
||||
Cache::set('think:queue:restart', time());
|
||||
$output->writeln("<info>Broadcasting queue restart signal.</info>");
|
||||
}
|
||||
}
|
||||
46
vendor/topthink/think-queue/src/queue/command/Subscribe.php
vendored
Normal file
46
vendor/topthink/think-queue/src/queue/command/Subscribe.php
vendored
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
|
||||
namespace think\queue\command;
|
||||
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Argument;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\Queue;
|
||||
use think\Url;
|
||||
|
||||
class Subscribe extends Command
|
||||
{
|
||||
public function configure()
|
||||
{
|
||||
$this->setName('queue:subscribe')
|
||||
->setDescription('Subscribe a URL to an push queue')
|
||||
->addArgument('name', Argument::REQUIRED, 'name')
|
||||
->addArgument('url', Argument::REQUIRED, 'The URL to be subscribed.')
|
||||
->addArgument('queue', Argument::OPTIONAL, 'The URL to be subscribed.')
|
||||
->addOption('option', null, Option::VALUE_IS_ARRAY | Option::VALUE_OPTIONAL, 'the options');
|
||||
}
|
||||
|
||||
public function execute(Input $input, Output $output)
|
||||
{
|
||||
|
||||
$url = $input->getArgument('url');
|
||||
if (!preg_match('/^https?:\/\//', $url)) {
|
||||
$url = Url::build($url);
|
||||
}
|
||||
|
||||
Queue::subscribe($input->getArgument('name'), $url, $input->getArgument('queue'), $input->getOption('option'));
|
||||
|
||||
$output->write('<info>Queue subscriber added:</info> <comment>' . $input->getArgument('url') . '</comment>');
|
||||
}
|
||||
}
|
||||
210
vendor/topthink/think-queue/src/queue/command/Work.php
vendored
Normal file
210
vendor/topthink/think-queue/src/queue/command/Work.php
vendored
Normal file
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: yunwuxin <448901948@qq.com>
|
||||
// +----------------------------------------------------------------------
|
||||
namespace think\queue\command;
|
||||
|
||||
use think\Config;
|
||||
use think\console\Command;
|
||||
use think\console\Input;
|
||||
use think\console\input\Option;
|
||||
use think\console\Output;
|
||||
use think\Hook;
|
||||
use think\queue\Job;
|
||||
use think\queue\Worker;
|
||||
use Exception;
|
||||
use Throwable;
|
||||
use think\Cache;
|
||||
use think\exception\Handle;
|
||||
use think\exception\ThrowableError;
|
||||
|
||||
class Work extends Command
|
||||
{
|
||||
|
||||
/**
|
||||
* The queue worker instance.
|
||||
* @var \think\queue\Worker
|
||||
*/
|
||||
protected $worker;
|
||||
|
||||
protected function initialize(Input $input, Output $output)
|
||||
{
|
||||
$this->worker = new Worker();
|
||||
}
|
||||
|
||||
protected function configure()
|
||||
{
|
||||
$this->setName('queue:work')
|
||||
->addOption('queue', null, Option::VALUE_OPTIONAL, 'The queue to listen on')
|
||||
->addOption('daemon', null, Option::VALUE_NONE, 'Run the worker in daemon mode')
|
||||
->addOption('delay', null, Option::VALUE_OPTIONAL, 'Amount of time to delay failed jobs', 0)
|
||||
->addOption('force', null, Option::VALUE_NONE, 'Force the worker to run even in maintenance mode')
|
||||
->addOption('memory', null, Option::VALUE_OPTIONAL, 'The memory limit in megabytes', 128)
|
||||
->addOption('sleep', null, Option::VALUE_OPTIONAL, 'Number of seconds to sleep when no job is available', 3)
|
||||
->addOption('tries', null, Option::VALUE_OPTIONAL, 'Number of times to attempt a job before logging it failed', 0)
|
||||
->setDescription('Process the next job on a queue');
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
* @param Input $input
|
||||
* @param Output $output
|
||||
* @return int|null|void
|
||||
*/
|
||||
public function execute(Input $input, Output $output)
|
||||
{
|
||||
$queue = $input->getOption('queue');
|
||||
|
||||
$delay = $input->getOption('delay');
|
||||
|
||||
$memory = $input->getOption('memory');
|
||||
|
||||
if ($input->getOption('daemon')) {
|
||||
Hook::listen('worker_daemon_start', $queue);
|
||||
$this->daemon(
|
||||
$queue, $delay, $memory,
|
||||
$input->getOption('sleep'), $input->getOption('tries')
|
||||
);
|
||||
} else {
|
||||
$response = $this->worker->pop($queue, $delay, $input->getOption('sleep'), $input->getOption('tries'));
|
||||
$this->output($response);
|
||||
}
|
||||
}
|
||||
|
||||
protected function output($response)
|
||||
{
|
||||
if (!is_null($response['job'])) {
|
||||
/** @var Job $job */
|
||||
$job = $response['job'];
|
||||
if ($response['failed']) {
|
||||
$this->output->writeln('<error>Failed:</error> ' . $job->getName());
|
||||
} else {
|
||||
$this->output->writeln('<info>Processed:</info> ' . $job->getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动一个守护进程执行任务.
|
||||
*
|
||||
* @param string $queue
|
||||
* @param int $delay
|
||||
* @param int $memory
|
||||
* @param int $sleep
|
||||
* @param int $maxTries
|
||||
* @return array
|
||||
*/
|
||||
protected function daemon($queue = null, $delay = 0, $memory = 128, $sleep = 3, $maxTries = 0)
|
||||
{
|
||||
$lastRestart = $this->getTimestampOfLastQueueRestart();
|
||||
|
||||
while (true) {
|
||||
$this->runNextJobForDaemon(
|
||||
$queue, $delay, $sleep, $maxTries
|
||||
);
|
||||
|
||||
if ( $this->memoryExceeded($memory) ) {
|
||||
Hook::listen('worker_memory_exceeded', $queue);
|
||||
$this->stop();
|
||||
}
|
||||
|
||||
if ( $this->queueShouldRestart($lastRestart) ) {
|
||||
Hook::listen('worker_queue_restart', $queue);
|
||||
$this->stop();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 以守护进程的方式执行下个任务.
|
||||
*
|
||||
* @param string $queue
|
||||
* @param int $delay
|
||||
* @param int $sleep
|
||||
* @param int $maxTries
|
||||
* @return void
|
||||
*/
|
||||
protected function runNextJobForDaemon($queue, $delay, $sleep, $maxTries)
|
||||
{
|
||||
try {
|
||||
$response = $this->worker->pop($queue, $delay, $sleep, $maxTries);
|
||||
|
||||
$this->output($response);
|
||||
} catch (Exception $e) {
|
||||
$this->getExceptionHandler()->report($e);
|
||||
} catch (Throwable $e) {
|
||||
$this->getExceptionHandler()->report(new ThrowableError($e));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取上次重启守护进程的时间
|
||||
*
|
||||
* @return int|null
|
||||
*/
|
||||
protected function getTimestampOfLastQueueRestart()
|
||||
{
|
||||
return Cache::get('think:queue:restart');
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否要重启守护进程
|
||||
*
|
||||
* @param int|null $lastRestart
|
||||
* @return bool
|
||||
*/
|
||||
protected function queueShouldRestart($lastRestart)
|
||||
{
|
||||
return $this->getTimestampOfLastQueueRestart() != $lastRestart;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查内存是否超出
|
||||
* @param int $memoryLimit
|
||||
* @return bool
|
||||
*/
|
||||
protected function memoryExceeded($memoryLimit)
|
||||
{
|
||||
return (memory_get_usage() / 1024 / 1024) >= $memoryLimit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取异常处理实例
|
||||
*
|
||||
* @return \think\exception\Handle
|
||||
*/
|
||||
protected function getExceptionHandler()
|
||||
{
|
||||
static $handle;
|
||||
|
||||
if (!$handle) {
|
||||
|
||||
if ($class = Config::get('exception_handle')) {
|
||||
if (class_exists($class) && is_subclass_of($class, "\\think\\exception\\Handle")) {
|
||||
$handle = new $class;
|
||||
}
|
||||
}
|
||||
if (!$handle) {
|
||||
$handle = new Handle();
|
||||
}
|
||||
}
|
||||
|
||||
return $handle;
|
||||
}
|
||||
|
||||
/**
|
||||
* 停止执行任务的守护进程.
|
||||
* @return void
|
||||
*/
|
||||
public function stop()
|
||||
{
|
||||
die;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user