webtechnick / cakephp-queue-plugin
CakePHP 队列插件 - 完整的工具,用于后台和安排耗时任务。可以将几乎所有内容从 CakePHP 脚本、模型和动作排队,到标准的 PHP 命令,甚至是 shell 命令。
Requires
- php: >=5.3.0
- composer/installers: *
This package is not auto-updated.
Last update: 2024-09-28 14:35:31 UTC
README
- 作者: Nick Baker
- 版本: 1.0.0
- 许可证: MIT
- 网站: http://www.webtechnick.com
功能
完整工具,用于后台和安排耗时任务。可以将几乎所有内容从 CakePHP 脚本、模型和动作排队,到标准的 PHP 命令,甚至是 shell 命令。
变更日志
- 1.0.0 初次发布
安装
将存储库克隆到您的 app/Plugin/Queue
目录中
git clone https://github.com/webtechnick/CakePHP-Queue-Plugin.git app/Plugin/Queue
或者您可以通过 https://getcomposer.org.cn 安装
在您的 app/Config/bootstrap.php
文件中加载插件
CakePlugin::load('Queue');
将模式运行到您的数据库中,以安装所需的表。
cake schema create --plugin Queue
设置
创建文件 app/Config/queue.php
,默认使用 app/Plugin/Queue/Config/queue.php.default
$config = array(
'Queue' => array(
'log' => true, //logs every task run in a log file.
'limit' => 1, //limit how many queues can run at a time. (default 1).
'allowedTypes' => array( //restrict what type of command can be queued.
1, //model
2, //shell
3, //url
//4, //php_cmd
//5, //shell_cmd
),
'archiveAfterExecute' => true, //will archive the task once finished executing for quicker queues
'cache' => '+5 minute', //how long to cache cpu usage and list. (false disables cache)
'cores' => '8', //number of cpu cores to correctly gauge current CPU load.
)
);
Cron 设置(可选)
一旦您开始添加队列中的项目,您就需要处理它们。您可以通过几种方式来实现。
-
通过设置 cron(推荐)
crontab -e */1 * * * * /path/to/app/Plugin/Queue/scripts/process_queue.sh
注意:cron 脚本假定从应用程序目录的根目录可以执行 Console/cake。
-
通过内置的 shell 处理队列
cake Queue.queue process
-
通过内置库处理队列
App::uses('Queue','Queue.Lib'); Queue::process();
-
通过导航到队列插件管理员的 Web 界面并处理队列(功能尚不可用)
快速入门指南(Shell)
将任务添加到队列中。
$ cake Queue.queue add "Queue.QueueTask::find('first')"
Task succesfully added.
525387a1-2dd0-4100-a48f-4f4be017215a queued model
Command: Queue.QueueTask::find('first')
Priority: 100
处理队列。
$ cake Queue.queue process
Processing Queue.
Success.
查看添加的任务。
$ cake Queue.queue view 525387a1-2dd0-4100-a48f-4f4be017215a
525387a1-2dd0-4100-a48f-4f4be017215a finished model
Command: Queue.QueueTask::find('first')
Priority: 100
Executed on Monday 7th of October 2013 10:19:10 PM. And took 0 ms.
Result: {"QueueTask":{"id":"525387a1-2dd0-4100-a48f-4f4be017215a","user_id":null,"created":"2013-10-07 22:18:41","modified":"2013-10-07 22:19:10","executed":null,"scheduled":null,"scheduled_end":null,"reschedule":null,"start_time":"1381205950","end_time":null,"cpu_limit":null,"is_restricted":false,"priority":"100","status":"2","type":"1","command":"Queue.QueueTask::find('first')","result":null,"execution_time":null,"type_human":"model","status_human":"in progress"}}
快速入门指南(库)
将任务添加到队列中。
App::uses('Queue', 'Queue.Lib');
$task = Queue::add("Queue.QueueTask::find('first')");
/* $task =
'QueueTask' => array(
'priority' => (int) 100,
'command' => 'Queue.QueueTask::find('first')',
'type' => (int) 1,
'scheduled' => null,
'scheduled_end' => null,
'reschedule' => null,
'cpu_limit' => null,
'modified' => '2013-10-07 22:22:36',
'created' => '2013-10-07 22:22:36',
'user_id' => null,
'id' => '5253888c-ae18-4ffd-991a-4436e017215a'
) */
处理队列。
$result = Queue::process();
/* $result will be boolean true */
查看任务。
$task = Queue::view('5253888c-ae18-4ffd-991a-4436e017215a');
/* $task is the string representation, same as queue view. Not as useful */
$task = Queue::findById('5253888c-ae18-4ffd-991a-4436e017215a');
/* $task is now an associative array, much more useful.
array(
'id' => '52538a36-df1c-4186-a50a-4076e017215a',
'user_id' => null,
'created' => '2013-10-07 22:29:42',
'modified' => '2013-10-07 22:29:42',
'executed' => '2013-10-07 22:29:42',
'scheduled' => null,
'scheduled_end' => null,
'reschedule' => null,
'start_time' => '1381206582',
'end_time' => '1381206582',
'cpu_limit' => null,
'is_restricted' => false,
'priority' => '100',
'status' => '3',
'type' => '1',
'command' => 'Queue.QueueTask::find('first')',
'result' => '{"QueueTask":{"id":"524b0c44-a3a0-4956-8428-dc3ee017215a","user_id":null,"created":"2013-10-01 11:54:12","modified":"2013-10-01 11:54:12","executed":null,"scheduled":null,"scheduled_end":null,"reschedule":null,"start_time":null,"end_time":null,"cpu_limit":null,"is_restricted":false,"priority":"100","status":"1","type":"1","command":"SomeModel::action(\"param\",\"param2\")","result":"","execution_time":null,"type_human":"model","status_human":"queued"}}',
'execution_time' => '0',
'type_human' => 'model',
'status_human' => 'finished'
) */
将任务添加到队列类型和命令
一旦您决定如何处理您的队列,就是时候开始将任务添加到队列中了。
使用内置的 shell 或库来添加任务。
Queue::add($command, $type, $options = array());
cake Queue.queue add "command" -t type
您可以使用 5 种 命令类型。默认是 模型。
-
模型 : 执行的模型函数。示例
#命令字符串 Model::action() Plugin.Model::action("param1","pararm2")
#将命令添加到队列中。 Queue::add("Plugin.Model::action('param1','pararm2')"); cake Queue.queue add "Plugin.Model::action('param1','pararm2')"
Models are setup via ClassRegistry::init() and methods executed on the model object as public method calls.
-
Shell : 执行的 CakePHP shell。示例
#命令字符串 ClearCache.clear_cache shell_command -f flag arg1 arg2
#将命令添加到队列中。 Queue::add("shell_command -f flag arg1 arg2", 'shell'); cake Queue.queue add "shell_command -f flag arg1 arg2" -t shell
-
Url : 请求 action 的 URL。示例
#命令字符串 /path/to/url
#将命令添加到队列中。 Queue::add("/path/to/url", 'url'); cake Queue.queue add "/path/to/url" -t url
-
php_cmd : PHP 命令,一个简单或复杂的 PHP 命令。示例
#命令字符串 3 + 5 mail('nick@example.com','subject','message')
将命令添加到队列中。Queue::add("mail('nick@example.com','subject','message')", 'php_cmd'); cake Queue.queue add "mail('nick@example.com','subject','message')" -t php_cmd
-
shell_cmd : 基本的Bash shell命令。示例
#命令字符串 echo 'hello' && echo 'world'
#将命令添加到队列中。Queue::add("echo 'hello' && echo 'world'", 'shell_cmd'); cake Queue.queue add "echo 'hello' && echo 'world'" -t shell_cmd
注意 php_cmd
和 shell_cmd
默认不允许。您必须在 app/Config/queue.php
中将其启用。
查看您的队列
您可以通过调用 Queue::next($limit);
或shell来查看任何给定时间队列中的内容以及接下来要执行的内容。
cake Queue.queue next 10
Retrieving Queue List.
1) 52537d35-95d0-48aa-a48d-416de017215a queued url
Command: /path/to/url
Priority: 100
Restricted By:
Start: 2013-10-11 03:00:00
CPU <= 95%
2) 52537d09-6bf0-4961-94f0-4201e017215a queued model
Command: Model::action()
Priority: 100
3) 52535ab8-6298-4ab3-9fc6-4b49e017215a queued shell_cmd
Command: echo 'hello' && echo 'world'
Priority: 100
Restricted By:
Start: 2013-10-07 23:00:00
4) 52537d35-95d0-48aa-a48d-416de017215a queued url
Command: /path/to/url
Priority: 100
Restricted By:
Start: 2013-10-11 03:00:00
CPU <= 95%
您将看到一个即将到来的队列列表,包括您为任务设置的任何限制。
任务限制
您可以分配给任务多种限制,这些限制将修改它们在队列中的顺序并影响任务执行的时间。受限任务优先于非受限任务(基本队列任务)。队列处理过程将在查找非受限任务之前先查找受限任务,因为非受限任务按定义不是时间敏感的。
任务选项
-
计划启动 限制:您可以将任务计划为在特定计划时间过后才执行。
Queue::add($command, $type, $options = array( 'start' => 'Friday 11pm', ));
队列Shell
cake Queue.queue add "command" -t type --start "Friday 11pm"
-
计划结束 限制:您可以指定一个结束时间以创建一个执行时间的“窗口”。当使用此选项时,您还必须指定一个重新计划选项,如果错过“窗口”,则将生效。
Queue::add($command, $type, $options = array( 'start' => 'Friday 11pm', 'end' => 'Saturday 5am', 'reschedule' => '+1 week' //如果错过窗口,将 '+1 week' 添加到 'Friday 11pm' 和 'Saturday 5am'));
队列Shell
cake Queue.queue add "command" -t type --start "Friday 11pm" --end "Saturday 5am" --reschedule "+1 week"
-
Cpu Load 限制:您可以选择一个CPU负载限制,只在CPU低于一定阈值时执行任务。
当当前CPU负载低于95%时将执行任务
Queue::add($command, $type, $options = array( 'cpu' => '95', ));
队列Shell
cake Queue.queue add "command" -t type --cpu "95"
-
优先级 限制:默认情况下,所有添加的任务都赋予100的优先级。您可以在添加时更改此优先级,这将允许您提前排队。数字越低,越接近队列顶部。
这会将任务跳到队列顶部(在限制任务之后)
Queue::add($command, $type, $options = array( 'priority' => '1', ));
队列Shell
cake Queue.queue add "command" -t type -p 1
混合匹配您的限制,以完全控制任务的执行时间和在队列中的位置。
手动运行任务
您可以绕过队列处理过程,显式手动运行任务。这将绕过任何限制,执行命令并返回结果。
//Queue::run($id);
#cake Queue.queue run <id>
$ cake Queue.queue run 52535ab8-6298-4ab3-9fc6-4b49e017215a
Running 52535ab8-6298-4ab3-9fc6-4b49e017215a
Success.
52535ab8-6298-4ab3-9fc6-4b49e017215a finished shell_cmd
Command: echo 'hello' && echo 'world'
Priority: 100
Restricted By:
Start: 2013-10-07 23:00:00
Executed on Monday 7th of October 2013 10:06:27 PM. And took 0 ms.
Result: "hello\nworld\n"
查看任务
您可以在任何时间查看队列或队列日志(执行后的存档)中的任何任务。您将看到所有元数据,如果是已完成的任务,您还将看到任务的结果以及执行时间和完成所需时间。
//$task = Queue::view($id);
#cake Queue.queue view <id>
$ cake Queue.queue view 52535ab8-6298-4ab3-9fc6-4b49e017215a
52535ab8-6298-4ab3-9fc6-4b49e017215a finished shell_cmd
Command: echo 'hello' && echo 'world'
Priority: 100
Restricted By:
Start: 2013-10-07 23:00:00
Executed on Monday 7th of October 2013 10:06:27 PM. And took 0 ms.
Result: "hello\nworld\n"
查看正在进行的任务
您可以通过库或shell查看当前正在进行的任务。
$queue = Queue::inProgress();
cake Queue.queue in_progress
注意:您还可以通过运行 cake Queue.queue in_progress_count
或 Queue::inProgressCount();
来获取仅进度计数。
从队列中移除任务
从队列中移除任务。请注意,默认情况下,这不会移除正在进行的任务。
//Queue::remove($id);
cake Queue.queue remove <id>
注意: 您可以通过将 true
传递给 Queue::remove($id, true);
或使用 cake queue.queue remove <id> -f
来强制移除正在进行的任务。
享受
享受这个插件吧!