mgip/laravel-database-schedule

多应用任务数据库管理

dev-master 2022-10-03 09:09 UTC

This package is auto-updated.

Last update: 2024-09-30 01:38:05 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;
    }
}

访问仪表板,并将命令列出以进行计划,创建如下示例的计划:Create Schedule

运行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 |
+----------------------------------------------------------------------------------------+-----------+-------------+----------------------------+

变更日志

CHANGELOG.md

致谢