dek-cz/nette-crunz

该软件包的最新版本(dev-main)没有可用的许可信息。

dev-main 2023-09-27 09:14 UTC

This package is auto-updated.

Last update: 2024-08-27 11:11:06 UTC


README

crunzphp/crunz 实现到 nette 框架中

一次性安装 cron 作业,从代码中管理其余部分。

Crunz 是一个框架无关的软件包,它使用流畅的 API 在 PHP 中安排周期性任务(cron 作业)。

Crunz 能够执行任何类型的可执行命令以及 PHP 闭包。

安装

要安装它

composer require dek-cz/nette-crunz

使用方法

创建简单任务

任务定义可以如下所示

crunz:
    tasks:
        - 
            command: 'ls'
            parameters: [ '-la' , '-rt' ]
            runningWhen: 'everyMinute'
            on: ''
            at: ''
            in: ''
            from: ''
            to: ''
            preventOverlapping: true
            description: ''
        - 
            command: 'du'
            parameters: [ '-hS' : '' ]
            expression: '* * * * *'
            preventOverlapping: true
            description: ''
        - 
            command: [@testSer, 'run']
            expression: '* * * * *'
            preventOverlapping: false
            description: 'Tests\DekApps\TestService::run'
        - 
            command: [Tests\DekApps\TestService2(_,1), 'getPlus']
            expression: '* * * * *'
            preventOverlapping: false
            description: 'Tests\DekApps\TestService2::getPlus'
        - 
            command: [@testSer, 'run']
            expression: '* * * * *'
            preventOverlapping: false
            description: 'Tests\DekApps\TestService::run'
            events:
                skip: 
                    - [@testSer, 'skip']


services:
    testSer: Tests\DekApps\TestService

配置

extensions:
    crunz: DekApps\Crunz\DI\CrunzExtension

Crunz 以 YAML 格式提供了几个配置选项。要修改配置设置,强烈建议拥有自己的配置文件副本,而不是修改原始文件。

要创建配置文件的副本,首先我们需要发布配置文件

/project/vendor/bin/crunz publish:config
The configuration file was generated successfully

结果,配置文件的副本将在我们的项目根目录中创建。

配置文件如下所示

# Crunz Configuration Settings

# This option defines where the task files and
# directories reside.
# The path is relative to the project's root directory,
# where the Crunz is installed (Trailing slashes will be ignored).
source: tasks

# The suffix is meant to target the task files inside the ":source" directory.
# Please note if you change this value, you need
# to make sure all the existing tasks files are renamed accordingly.
suffix: Tasks.php

# Timezone is used to calculate task run time
# This option is very important and not setting it is deprecated
# and will result in exception in 2.0 version.
timezone: ~

# This option define which timezone should be used for log files
# If false, system default timezone will be used
# If true, the timezone in config file that is used to calculate task run time will be used
timezone_log: false

# By default the errors are not logged by Crunz
# You may set the value to true for logging the errors
log_errors: false

# This is the absolute path to the errors' log file
# You need to make sure you have the required permission to write to this file though.
errors_log_file:

# By default the output is not logged as they are redirected to the
# null output.
# Set this to true if you want to keep the outputs
log_output: false

# This is the absolute path to the global output log file
# The events which have dedicated log files (defined with them), won't be
# logged to this file though.
output_log_file:

# By default line breaks in logs aren't allowed.
# Set the value to true to allow them.
log_allow_line_breaks: false

# By default empty context arrays are shown in the log.
# Set the value to true to remove them.
log_ignore_empty_context: false

# This option determines whether the output should be emailed or not.
email_output: false

# This option determines whether the error messages should be emailed or not.
email_errors: false

# Global Swift Mailer settings
#
mailer:
    # Possible values: smtp, mail, and sendmail
    transport: smtp
    recipients:
    sender_name:
    sender_email:


# SMTP settings
#
smtp:
    host:
    port:
    username:
    password:
    encryption:

任务文件

任务文件类似于 crontab 文件。就像 crontab 文件一样,它们可以包含一个或多个任务。

通常我们在项目根目录下的 tasks/ 目录中创建我们的任务文件。

默认情况下,Crunz 假设所有任务文件都位于项目根目录下的 tasks/ 目录中。

指定源目录有两种方式:1)配置文件 2)作为事件运行器命令的参数。

我们可以通过将参数传递给事件运行器来显式设置源路径

* * * * * cd /project && vendor/bin/crunz schedule:run [/path/to/tasks/directory]

其他有用命令

我们已经使用了一些 crunz 命令,如 schedule:runpublish:config

要查看 crunz 的所有有效选项和参数,我们可以运行以下命令

vendor/bin/crunz --help

列出任务

其中这些命令之一是 crunz schedule:list,它以表格格式列出定义的任务(在收集的 *.Tasks.php 文件中)。

vendor/bin/crunz schedule:list

+---+---------------+-------------+--------------------+
| # | Task          | Expression  | Command to Run     |
+---+---------------+-------------+--------------------+
| 1 | Sample Task   | * * * * 1 * | command/to/execute |
+---+---------------+-------------+--------------------+

默认情况下,列表以文本格式显示,但可以通过 --format 选项更改格式。

json 格式列出,命令

vendor/bin/crunz schedule:list --format json

将输出

[
    {
        "number": 1,
        "task": "Sample Task",
        "expression": "* * * * 1",
        "command": "command/to/execute"
    }
]

强制运行

在开发期间,可能会很有用强制运行所有任务,而不管它们的实际运行时间,这可以通过在 schedule:run 中添加 --force 来实现

vendor/bin/crunz schedule:run --force

要强制运行单个任务,使用上面的 schedule:list 命令来确定任务编号,然后按如下方式运行

vendor/bin/crunz schedule:run --task 1 --force