robersonfaria / laravel-database-schedule
在友好的界面上管理您的Laravel任务调度,并将计划保存到数据库中。
Requires
- php: ^7.2|^8.0
- laravel/framework: ^5.8|6.*|7.*|8.*|9.*|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^5.20|^8.0|^9.0
- phpunit/phpunit: ^8.5|^9.5.10|^10.5
- squizlabs/php_codesniffer: ^3.6
- dev-master
- 1.4.0
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.1
- 1.2.0
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.28
- 1.0.27
- 1.0.26
- 1.0.25
- 1.0.24
- 1.0.23
- 1.0.22
- 1.0.21
- 1.0.20
- 1.0.19
- 1.0.18
- 1.0.17
- 1.0.16
- 1.0.15
- 1.0.14
- 1.0.13
- 1.0.12
- 1.0.11
- 1.0.10
- 1.0.9
- 1.0.8
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-pull/60
- dev-config-tests
This package is auto-updated.
Last update: 2024-09-04 18:37:17 UTC
README
文档
此库在您的应用程序中创建了一个路由(默认:/schedule),您可以在其中管理在任何给定时刻将执行哪些计划,这些计划将被记录在数据库中,并且可以通过界面进行更改、激活、停用或删除,而无需重新部署应用程序。
安装
- 运行
composer require robersonfaria/laravel-database-schedule
- 运行
php artisan migrate
环境变量
您可以通过设置以下环境变量来配置计划
- SCHEDULE_TIMEZONE : 默认与应用程序配置相同,但如果您需要在不同时区运行计划,则可以使用此变量进行配置
- SCHEDULE_CACHE_DRIVER : 默认为
file
- SCHEDULE_CACHE_ENABLE : 默认当
APP_DEBUG=true
时禁用,当APP_DEBUG=false
时启用
配置
有几个库配置选项,要更改设置,您可以获取项目的配置文件
php artisan vendor:publish --provider="RobersonFaria\DatabaseSchedule\DatabaseSchedulingServiceProvider" --tag="config"
仪表板授权
仪表板授权在 /schedule
URI 上公开仪表板。
在配置文件中,您可以定义是否限制对 /schedule
路由的访问,默认为 true。如果访问受限制,则用户必须登录并满足在 viewDatabaseSchedule
中定义的要求权限控制访问。
<?php return [ //... /** * If restricted_access is true, the user must be authenticated and meet the definition of `viewDatabaseSchedule` gate */ 'restricted_access' => env('SCHEDULE_RESTRICTED_ACCESS', true), //... ]
注意,此值也可以使用 SCHEDULE_RESTRICTED_ACCESS 环境变量进行更改。
注意:如果 restricted_access 设置为 false,则对 /schedule
路由的访问将是公开的。
您必须在服务提供程序中定义权限,Laravel 默认已经带来了 App\Providers\AuthServiceProvider
提供程序用于此目的。请参阅 Laravel 文档https://laravel.net.cn/docs/8.x/authorization#gates
您可以根据需要修改此权限以限制对数据库调度仪表板的访问。
protected function gate() { Gate::define('viewDatabaseSchedule', function ($user) { return in_array($user->email, [ 'roberson.faria@gmail.com', ]); }); }
示例
如果您想限制对路由的访问权限,使其只能由具有特定角色的用户访问,您可以这样操作。
Gate::define('viewDatabaseSchedule', function ($user) { return $user->hasRole('administrator'); });
基本上,如果您的权限有 return true
,则允许访问,如果 return false
,则限制访问。
组
如果您有很多工作,您可以通过在 config/database-schedule.php
中启用组功能来简化管理。
/** * If you have a lot of jobs, you can group them for easier managing of jobs. */ 'enable_groups' => true,
这将允许您在作业列表中仅过滤属于特定组的作业。
计划任务示例
创建计划任务的命令 app/Console/Commands/test.php
<?php namespace App\Console\Commands; use Illuminate\Console\Command; class test extends Command { /** * The name and signature of the console command. * * @var string */ protected $signature = 'command:test {user} {initialDate} {finalDate}'; /** * The console command description. * * @var string */ protected $description = 'Command description'; /** * Create a new command instance. * * @return void */ public function __construct() { parent::__construct(); } /** * Execute the console command. * * @return int */ public function handle() { $this->info('Hello ' . $this->argument('user')); $this->info("Initial Date: " . $this->argument('initialDate')); $this->info("Final Date: " . $this->argument('finalDate')); return 0; } }
访问仪表板,该命令将被列出以进行计划,创建以下示例中的计划:
运行 artisan 命令以运行计划任务
php artisan schedule:run
控制台输出将如下所示
Running scheduled command: ('/usr/bin/php7.4' 'artisan' command:test 1 '2022-02-02 00:00:00' '2022-04-02 00:00:00' > 'path/to/storage/logs/schedule-8763d2ce5a20ee888dd9d8a7e5a5cfcd4b315375.log' 2>&1 ;
如果您标记了通过电子邮件发送输出,您将收到类似于此的电子邮件
时间表列表
您还可以使用 artisan 命令列出已注册和活动的命令
$ php artisan schedule:list +----------------------------------------------------------------------------------------+-----------+-------------+----------------------------+ | Command | Interval | Description | Next Due | +----------------------------------------------------------------------------------------+-----------+-------------+----------------------------+ | '/usr/bin/php7.4' 'artisan' inspire | * * * * * | | 2022-03-02 17:05:00 +00:00 | | '/usr/bin/php7.4' 'artisan' command:test 1 '2022-02-02 00:00:00' '2022-04-02 00:00:00' | * * * * * | | 2022-03-02 17:05:00 +00:00 | +----------------------------------------------------------------------------------------+-----------+-------------+----------------------------+
变更日志
鸣谢
- 该库受到了therezor/laravel-database-schedule库的启发