Blog Detail

21

Jun
Manage Laravel Task Scheduling & Save Schedules to Database cover image

arrow_back Manage Laravel Task Scheduling & Save Schedules to Database

Laravel database schedule is a package that manages your Laravel Task Scheduling in a friendly interface and saves schedules to the database. This package creates a route(default: /schedule) in your application where it is possible to manage which schedules will be executed at any given moment, these schedules are recorded in the database and can be changed, activated, inactivated, or deleted via the interface without the need for new application deployment.

Installation

You can install this package via composer.

Run composer require robersonfaria/laravel-database-schedule

Run this command for migration:

Run php artisan migrate

Environment variables

You can set the following environment variables to configure schedules:

  • SCHEDULE_TIMEZONE : The default is the same configured for the application, but if you need the schedules to run in a different timezone, it is possible to configure it with this variable
  • SCHEDULE_CACHE_DRIVER : The default is file
  • SCHEDULE_CACHE_ENABLE : The default is disabled when APP_DEBUG=true and enabled when APP_DEBUG=false

Configurations

There are several library configuration options, to change the settings you can get the configuration file for your project.

php artisan vendor:publish --provider="RobersonFaria\DatabaseSchedule\DatabaseSchedulingServiceProvider" --tag="config"

Dashboard Authorization

Dashboard Authorization exposes a dashboard at /schedule URI.

In the configuration file, it is possible to define whether to restrict access to route /schedule, the default is true. If access is restricted, the user must be logged in and meet the requirements defined in the viewDatabaseSchedule gate controls access.

<?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),
    //...
]

Note that this value can also be changed using the SCHEDULE_RESTRICTED_ACCESS environment variable.

ATTENTION: if restricted_access is set to false, access to the / schedule route will be public.

You must define the gates in your service providers, laravel by default already brings the provider App\Providers\AuthServiceProvider for this purpose.

You are free to modify this gate as needed to restrict access to your Database Schedule Dashboard.

protected function gate()
{
    Gate::define('viewDatabaseSchedule', function ($user) {
        return in_array($user->email, [
            'roberson.faria@gmail.com',
        ]);
    });
}

Scheduled Task Example

Create the command for your scheduled task app/Console/Commands/test.php:

Access the dashboard and the command will be listed for scheduling, create a schedule like an example below:

Run the artisan command to run scheduled tasks

php artisan schedule:run

The console output will look like this:

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 ;

For more details, You can visit its complete documentation on Github.

Published at : 21-06-2022

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project