spatie / laravel-short-schedule
通过子分钟频率调度 artisan 命令
Requires
- php: ^8.2
- illuminate/cache: ^10|^11.0
- react/event-loop: ^1.5
- spatie/laravel-package-tools: ^1.16.4
- spatie/temporary-directory: ^2.2.1
Requires (Dev)
- mockery/mockery: ^1.6.11
- orchestra/testbench: ^8.0|^9.0.3
- phpunit/phpunit: ^10.5.15
- spatie/test-time: ^1.3.3
README
Laravel 的原生调度器允许您每分钟调度一次 artisan 命令。
如果您需要以更高的频率执行某些操作,例如每秒执行一次,那么您就找到了正确的包。安装了 laravel-short-schedule 后,您就可以这样做:
// in your routes/console.php file use Spatie\ShortSchedule\Facades\ShortSchedule; // this command will run every second ShortSchedule::command('artisan-command')->everySecond(); // this command will run every 30 seconds ShortSchedule::command('another-artisan-command')->everySeconds(30); // this command will run every half a second ShortSchedule::command('another-artisan-command')->everySeconds(0.5);
或者,您可以将以下内容添加到控制台内核的 shortSchedule
方法中
// in app\Console\Kernel.php protected function shortSchedule(\Spatie\ShortSchedule\ShortSchedule $shortSchedule) { // this command will run every second $shortSchedule->command('artisan-command')->everySecond(); // this command will run every 30 seconds $shortSchedule->command('another-artisan-command')->everySeconds(30); // this command will run every half a second $shortSchedule->command('another-artisan-command')->everySeconds(0.5); // this command will run every second and its signature will be retrieved from command automatically $shortSchedule->command(\Spatie\ShortSchedule\Tests\Unit\TestCommand::class)->everySecond(); }
您是视觉学习者吗?
在这个视频中,您将看到该包的演示。
想知道它是如何工作的吗?那么请观看这个视频。
最后,还有这个视频展示了如何测试该包。您将了解如何测试由ReactPHP驱动的循环。
这些视频也是Laravel Package Training的一部分。
支持我们
我们投入了大量资源来创建一流的开放源代码包。您可以通过购买我们的付费产品之一来支持我们。
我们非常感谢您从您家乡寄给我们明信片,并说明您正在使用我们哪些包。您可以在我们的联系页面上找到我们的地址。我们将在我们的虚拟明信片墙上发布所有收到的明信片。
安装
您可以通过 composer 安装此包
composer require spatie/laravel-short-schedule
在您的生产环境中,您可以使用此命令启动短期调度器
php artisan short-schedule:run
您应该使用进程监控器(如Supervisor)来确保此任务始终运行,并在服务器启动时自动启动它。每次您更改计划时,都应该重新启动此命令。
处理内存泄漏
为了处理泄漏内存的命令,您可以设置短期调度工作进程的生存时间(以秒为单位)
php artisan short-schedule:run --lifetime=60 // after 1 minute the worker will be terminated
在给定秒数后,工作进程及其所有子进程将被终止,释放所有内存。然后 supervisor(或类似的监视器)将其重新启动。
Lumen
在您的 Lumen 项目中运行 php artisan short-schedule:run
命令之前,您应该将 ShortScheduleRunCommand
的副本复制到您的 app/Commands
文件夹中
cp ./vendor/spatie/laravel-short-schedule/src/Commands/ShortScheduleRunCommand.php ./app/Console/Commands/ShortScheduleRunCommand.php
接下来,编辑新的 ShortScheduleRunCommand.php
文件,将命名空间从 namespace Spatie\ShortSchedule\Commands;
更改为 namespace App\Console\Commands;
,然后您就可以开始了!
用法
在您的 routes/console.php 文件中,您可以使用 ShortSchedule
门面来安排命令。
// in your routes/console.php file use Spatie\ShortSchedule\Facades\ShortSchedule; ShortSchedule::command('artisan-command')->everySecond();
在 app\Console\Kernel
中,您应该添加一个名为 shortSchedule
的方法。
// in app\Console\Kernel.php protected function shortSchedule(\Spatie\ShortSchedule\ShortSchedule $shortSchedule) { // this artisan command will run every second $shortSchedule->command('artisan-command')->everySecond(); // this artisan command will run every second, its signature will be resolved from container $shortSchedule->command(\Spatie\ShortSchedule\Tests\Unit\TestCommand::class)->everySecond(); }
指定秒数
您可以通过这种方式每秒运行一个 artisan 命令
ShortSchedule::command('artisan-command')->everySecond();
您可以使用 everySeconds
指定特定的秒数
ShortSchedule::command('artisan-command')->everySeconds(30);
您甚至可以以亚秒频率安排任务。此任务将每半秒运行一次。
ShortSchedule::command('artisan-command')->everySeconds(0.5);
安排 shell 命令
使用 exec
来安排 bash 命令。
ShortSchedule::exec('bash-command')->everySecond();
防止重叠
默认情况下,即使上一个调用仍在运行,计划中的命令也会运行。
您可以通过添加 withoutOverlapping
来防止这种情况。
ShortSchedule::command('artisan-command')->everySecond()->withoutOverlapping();
时间约束之间
限制任务在开始和结束时间之间运行。
ShortSchedule::command('artisan-command')->between('09:00', '17:00')->everySecond();
使用溢出天是安全的。在此示例中,命令将在 21:00 到 01:00 之间的每秒运行
ShortSchedule::command('artisan-command')->between('21:00', '01:00')->everySecond();
真值测试约束
如果给定的闭包返回一个真值,则将运行命令。闭包将以与命令计划相同的频率进行评估。因此,如果您计划每秒运行命令,则给定的闭包也将每秒运行。
ShortSchedule::command('artisan-command')->when(fn() => rand() %2)->everySecond();
环境约束
命令将仅在给定的环境中运行。
ShortSchedule::command('artisan-command')->environments('production')->everySecond();
您也可以传递一个数组
ShortSchedule::command('artisan-command')->environments(['staging', 'production'])->everySecond();
复合约束
您可以使用上面提到的所有约束。只有当所有使用的约束通过时,命令才会执行。
ShortSchedule::command('artisan-command') ->between('09:00', '17:00') ->when($callable) ->everySecond();
维护模式
当 Laravel 处于维护模式时,命令不会运行。如果您想强制在维护模式下运行命令,可以使用 runInMaintenanceMode
方法。
ShortSchedule::command('artisan-command')->everySecond()->runInMaintenanceMode();
在单个服务器上运行任务
限制命令仅在某一时间运行在一个服务器上。
ShortSchedule::command('artisan-command')->everySecond()->onOneServer();
事件
对这些事件做出响应时执行任何代码都是阻塞的。如果您的代码执行时间较长,所有短计划作业都将延迟。我们强烈建议将您希望对这些事件做出响应的任何代码放入队列中。
Spatie\ShortSchedule\Events\ShortScheduledTaskStarting
此事件将在任务即将开始之前触发。它有这些公共属性
command
:将要执行的命令字符串process
:用于执行命令的Symfony\Component\Process\Process
实例
Spatie\ShortSchedule\Events\ShortScheduledTaskStarted
此事件将在任务开始之前触发。它有这些公共属性
command
:正在执行的命令字符串process
:执行命令的Symfony\Component\Process\Process
实例
测试
composer test
更新日志
有关最近更改的更多信息,请参阅 更新日志。
贡献
有关详细信息,请参阅 贡献指南。
安全性
如果您发现有关安全的错误,请通过 security@spatie.be 发送邮件,而不是使用问题跟踪器。
鸣谢
许可证
MIT 许可证(MIT)。请参阅 许可证文件 以获取更多信息。