robersonfaria/laravel-database-schedule

在友好的界面上管理您的Laravel任务调度,并将计划保存到数据库中。

1.4.0 2024-04-04 17:43 UTC

README

Laravel Database Schedule

文档

此库在您的应用程序中创建了一个路由(默认:/schedule),您可以在其中管理在任何给定时刻将执行哪些计划,这些计划将被记录在数据库中,并且可以通过界面进行更改、激活、停用或删除,而无需重新部署应用程序。

List Schedules

Create Schedules

Show History Schedules

安装

  1. 运行 composer require robersonfaria/laravel-database-schedule
  2. 运行 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 ;

如果您标记了通过电子邮件发送输出,您将收到类似于此的电子邮件

Mail Output

时间表列表

您还可以使用 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 |
+----------------------------------------------------------------------------------------+-----------+-------------+----------------------------+

变更日志

变更日志.md

鸣谢