inspectioneering / taskrunner
通过单个PHP脚本运行项目中所有重复的任务,而不是创建无限多的cron作业。
Requires
- malkusch/lock: ^1.0
- monolog/monolog: >=1.0
- mtdowling/cron-expression: ^1.1
- symfony/console: >=2.8
- symfony/yaml: >=2.8
Requires (Dev)
- phpunit/phpunit: ^4.0
This package is not auto-updated.
Last update: 2024-09-28 19:52:53 UTC
README
通过单个PHP脚本运行项目中所有重复的任务,而不是创建无限多的cron作业。
安装
- 通过composer安装包:
composer require inspectioneering/taskrunner
- 创建一个
tasks.yml
文件(见下文) - 创建一个或多个要执行的Task类。您可以复制位于
vendor/inspectioneering/taskrunner/src
的ExampleTask.php
文件以开始。
设置
TaskRunner将根据您在tasks.yml
文件中提供的cron定义调用单个任务,这些任务扩展自Inspectioneering\TaskRunner\Task
抽象类。此配置文件包含每个单个任务以及每个任务所需的任何必需和可选参数。
默认情况下,TaskRunner将在您的项目根目录或您的config
文件夹中查找tasks.yml
文件。您可以使用--config-dir=...
选项指定不同的文件位置。
默认情况下,任何任务的输出都将简单地输出到stdout。您可以在tasks.yml文件中定义一个或多个Monolog通道/处理程序。有关详细信息,请参阅下一节。
可选地,您可以引用一个在运行任何任务之前将被调用的bootstrap文件。例如,如果您需要连接到数据库以执行特定任务,这将非常有用。
用法
只有当任务满足您在tasks.yml文件中定义的cron标准时,任务才会执行。
通过命令行列出所有配置的任务
vendor/bin/taskrunner list
通过命令行运行任务
vendor/bin/taskrunner run
- 运行所有活动任务vendor/bin/taskrunner run --task=[name]
- 运行单个任务
或者,将以下条目添加到您的CRON(crontab -e
)
* * * * * /my/project/directory/vendor/bin/taskrunner run
强制运行任务,无论cron条目是否有效
vendor/bin/taskrunner run --task=[name] --force
tasks.yml配置
tasks.yml文件应包含一个或多个任务的定义。每个任务需要以下两个参数
class
- 要运行的任务的类名。类应扩展Task
,如果需要,应包括命名空间。cron
- 决定任务应多久执行一次的CRON表达式。
理想情况下,应使用某种锁定机制来配置任务。也就是说,如果TaskRunner在之前的执行完成之前尝试再次运行相同的任务,它应该失败。您目前可以使用基于文件或数据库的锁定方法。
您还可以使用Monolog库配置一个或多个日志通道。为此,请在monolog.handlers
属性中将每个通道定义为一个数组,格式如下
- [ "\\Namespace\\Of\\MonologHandler", "argument", "argument", ... "log level" ]
其中日志级别可以是以下之一
- 'emergency'
- 'alert'
- 'critical'
- 'error'
- 'warning'
- 'notice'
- 'info'
- 'debug'
完整的tasks.yml配置文件示例
# Instantiate a bootstrap file when running tasks.
bootstrap: "vendor/autoload.php"
# Configure a locking mechanism
locking:
# Options are "file" or "database"
type: "file"
# Default path is /tmp.
file_path: "relative/path/in/my/project"
# Configure Monolog logging.
monolog:
# Optionally use a different name for the logger (defaults to 'tasks').
name: "example"
# Define one or more logging handlers.
handlers:
# First argument is fully-qualified class name, and subsequent arguments are constructor parameters.
# See https://github.com/Seldaek/monolog
- [ "\\Monolog\\Handler\\StreamHandler", "example/tasks.log", "info" ]
# You can define as many handlers as you would like.
- [ "\\Monolog\\Handler\\RotatingFileHandler", "example/tasks.log", 7, "debug" ]
# Tasks are declared here.
tasks:
# Underscored name of the task. When calling this task individually from the command line, use this name.
example_task:
# The fully-qualified classname to call. The class must extend \Inspectioneering\TaskRunner\Task.
class: "\\Namespace\\To\\ExampleTask"
# Cron definition, using the standard crontab notation. This example would run every 5 minutes.
cron: "5 * * * *"
编写任务
任务是扩展\Inspectioneering\TaskRunner\Task
类的类,并且必须包含公共的execute()
函数。
您可以使用 $log
类变量配合 Monolog,利用标准的 PSR-3 记录日志方法(例如 $this->log->info('something');
或 $this->log->emergency('uh oh!');
)。理想情况下,您应充分利用 Monolog 提供的各种处理器,在不同错误级别执行不同的操作。例如,如果任务由于任何原因失败,您可以记录一个关键错误并发送给您电子邮件,而信息和警告日志则简单地写入文件。
一个示例任务可能如下所示
<?php
// src/MyApp/Tasks/ExampleTask.php
namespace MyApp\Tasks;
use Inspectioneering\TaskRunner\Task;
class ExampleTask extends Task
{
public function execute()
{
$this->log->info("This is an example.");
}
}