nr/processcronjobs
Composer 安装命令:
composer require nr/processcronjobs
包简介
Manage the execution of CronJobs
关键字:
README 文档
README
What it does
The module provides paths under which cronjobs can be registered. It lists all registered cronjobs and can execute individual ones manually. The last execution, the last status and, in the event of an error, the last error message are displayed.
Features
- Clear overview of all registered CronJobs
- Easy to set timing (onInit or onReady) and delay (LazyCron)
- Individual path (endpoint) that executes the CronJobs. Configuration of a secret path segment and additional path segments (namespaces) for selected CronJobs.
- Display of the time of the last execution and any error messages
- Run CronJobs as a specific ProcessWire user
- Notes system to attach warnings or additional info to CronJobs
Install
- Copy the files for this module to /site/modules/ProcessCronJobs/
- In admin: Modules > Refresh. Install ProcessCronJobs.
- Go to Setup > CronJobs
- Copy and Install example Module (
modules/ProcessCronJobs/example/ProcessCronJobsRegistration.module.example) to register your CronJobs in the__constructor()method. You can also use any other__constructor()method. It makes sense to register the CronJobs as early as possible so that they can also be executed on onInit. - Set up the real cron that calls the ProcessCronJobs provided endpoint.
- Type
crontab -ein your unix console - Add this line and save the file:
* * * * * curl --silent "https://example.com/cron/"Find out more about setting up CronJobs (Wikipedia).
- Type
Install via composer
- Execute the following command in your website root directory.
composer require nr/processcronjobs
Register a CronJob
Simple
A simple example for registering a CronJob.
wire()->addHookBefore('ProcessCronJobs::register', function(HookEvent $event){ /** @var ProcessCronJobs $processCronJobs */ $processCronJobs = $event->object; $processCronJobs->add( 'SuperSimpleCronJobOnDemand', function(CronJob $cron){ echo "Hello Cron"; }, ); });
Delayed
This CronJob should run once a day at init.
wire()->addHookBefore('ProcessCronJobs::register', function(HookEvent $event){ /** @var ProcessCronJobs $processCronJobs */ $processCronJobs = $event->object; $processCronJobs->add( 'MyFirstCronJobEveryDay', function(CronJob $cron){ echo "What a beautiful day"; }, [ 'lazyCron' => 'LazyCron::everyDay', 'timing' => CronJob::timingInit, ] ); });
Long Running
This CronJob runs for a very long time and is called directly by a "real" CronJob so as not to block other CronJobs. The endpoint for this CronJob is https://example.com/cron/longrunning/ or https://example.com/cron/###your_secret###/longrunning/.
CronJobs that have a namespace (own path segment) cannot be delayed with LazyCron, because LazyCron can only be started by a single request. LazyCron creates a lock file and thus blocks the execution of parallel calls.
wire()->addHookBefore('ProcessCronJobs::register', function(HookEvent $event){ /** @var ProcessCronJobs $processCronJobs */ $processCronJobs = $event->object; $processCronJobs->add( 'SuperLongRunningSpecialCronJob', function(CronJob $cron){ echo "I have so much work to do"; }, [ 'timing' => CronJob::timingInit, 'ns' => 'longrunning' ] ); });
Run as a specific user
This CronJob runs as the user "cronbot". The previous user is restored after execution. If the user is not found, the CronJob is automatically disabled.
wire()->addHookBefore('ProcessCronJobs::register', function(HookEvent $event){ /** @var ProcessCronJobs $processCronJobs */ $processCronJobs = $event->object; $processCronJobs->add( 'ImportAsSpecificUser', function(CronJob $cron){ echo "Running as: " . wire()->user->name; }, [ 'user' => 'cronbot', ] ); });
Process View
Configuration
Modules > Configure > ProcessCronJobs
You will find the following configuration options in the module settings:
- The trigger path, i.e. the path that triggers the CronJob processing, can be adjusted here (default: cron/).
- A secret path segment can be created that must be appended to the trigger path so that processing is started.
- Automatic processing can generally be stopped (status).
- The cache of ProcessCronJobs can be emptied. Things like the time of the last call, the status of the last call and possibly an error message are stored in the cache.
Different Callback Types
You can define callbacks in different ways:
Anonymous Function
$processCronJobs->add( 'AnonymousFunctionCronJob', function(CronJob $cron){ echo "Using anonymous function"; } );
Object Method
$processCronJobs->add( 'ObjectMethodCronJob', [$this, 'myFunction'] );
Static Class Method
$processCronJobs->add( 'StaticMethodCronJob', '\\ProcessWire\\MyClass::myStaticFunction' );
The CronJob object
| Option | Type | Default | Description |
|---|---|---|---|
name |
String | Unique name in PascalCase e.g. MyFirstCronJob. |
|
callback |
Callable | function(CronJob $cron){} |
Function to be executed. Can be an anonymous function, an array [$object, 'methodName'], or a static class method string '\\ProcessWire\\MyClass::myStaticFunction'. |
lazyCron |
null, String | null |
If empty, the CronJob is executed without delay as soon as the path is called. |
ns |
null, String | null |
If empty, the CronJob is called via the default path. |
timing |
Integer | CronJob::timingReady |
The CronJon can be called either at onInit (1) or onReady (2). OnInit is earlier and therefore faster, but not all functions of ProcessWire are available here, e.g. page and language. |
timingStr |
String | onReady |
This is just a getter property. |
user |
null, User, String, Integer | null |
Run the CronJob as a specific ProcessWire user. Accepts a User object, username or user ID. The previous user is restored after execution. If the user is not found, the CronJob is automatically disabled and a note is added. |
notes |
Array | [] |
Array of notes attached to the CronJob. Notes are displayed as warnings in the admin UI. Use $cron->addNote('message') to add a note. |
disabled |
Boolean | false |
This can be used to deactivate the cronjob, e.g. disabled = $config->debug. |
trigger |
Integer | CronJob::triggerNever |
Displays the last trigger for execution. Possible values are: 1 (Never): CronJob has never been executed 2 (Auto): CronJob was last executed directly via the "real" Cron (onDemand). 4 (Lazy): CronJob was called up with a time delay via the LazyCron. 8 (Force): The CronJob was started manually 16 (Error): The last call ended with an error (see log). |
triggerStr |
String | Unknown |
This is just a getter property. |
lastRun |
Integer | 0 |
Contains the last execution time as a Unix timestamp. Stored and retrieved in the ProcessWire cache. |
lastError |
String | Possible error message from the last call |
nr/processcronjobs 适用场景与选型建议
nr/processcronjobs 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「module」 「cronjob」 「processwire」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 nr/processcronjobs 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 nr/processcronjobs 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 nr/processcronjobs 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Installs processwire 3rd party modules to PW system "site/modules" directory.
Provides ProcessWire integration for various template engines such as Twig.
ProcessWire module adding Smarty to the TemplateEngineFactory
Module for ProcessWire which automatically generates Blurhashs.
Add AlpineJS to ProcessWire admin
A cronjob entrypoint for oxid
统计信息
- 总下载量: 2
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-07-11

