2017 © Pedro Peláez
 

yii2-extension yii2-scheduling

Scheduling extension for Yii2 framework

image

yii2mod/yii2-scheduling

Scheduling extension for Yii2 framework

  • Tuesday, September 26, 2017
  • by disem
  • Repository
  • 3 Watchers
  • 10 Stars
  • 4,336 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 6 Versions
  • 14 % Grown

The README.md

, (*1)

Yii2 Schedule Extension


This extension is the port of Laravel's Schedule component (http://laravel.com/docs/master/artisan#scheduling-artisan-commands), (*2)

Latest Stable Version Total Downloads License Build Status Scrutinizer Code Quality, (*3)

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

php composer.phar require yii2mod/yii2-scheduling "*"

or add, (*6)

"yii2mod/yii2-scheduling": "*"

to the require section of your composer.json., (*7)

Description

This project is inspired by the Laravel's Schedule component and tries to bring it's simplicity to the Yii framework. Quote from Laravel's documentation:, (*8)

In the past, developers have generated a Cron entry for each console command they wished to schedule. 
However, this is a headache. Your console schedule is no longer in source control, 
and you must SSH into your server to add the Cron entries. Let's make our lives easier.

After installation all you have to do is to put single line into crontab:, (*9)

* * * * * php /path/to/yii yii schedule/run --scheduleFile=@path/to/schedule.php 1>> /dev/null 2>&1

You can put your schedule into the schedule.php file, or add it withing bootstrapping of your extension or application, (*10)

Schedule examples

This extension is support all features of Laravel's Schedule, except environments and maintance mode., (*11)

Scheduling Closures, (*12)

$schedule->call(function()
{
    // Do some task...

})->hourly();

Scheduling Terminal Commands, (*13)

$schedule->exec('composer self-update')->daily();

Running command of your application, (*14)

$schedule->command('migrate')->cron('* * * * *');

Frequent Jobs, (*15)

$schedule->command('foo')->everyFiveMinutes();

$schedule->command('foo')->everyTenMinutes();

$schedule->command('foo')->everyThirtyMinutes();

Daily Jobs, (*16)

$schedule->command('foo')->daily();

Daily Jobs At A Specific Time (24 Hour Time), (*17)

$schedule->command('foo')->dailyAt('15:00');

Twice Daily Jobs, (*18)

$schedule->command('foo')->twiceDaily();

Job That Runs Every Weekday, (*19)

$schedule->command('foo')->weekdays();

Weekly Jobs, (*20)

$schedule->command('foo')->weekly();

// Schedule weekly job for specific day (0-6) and time...
$schedule->command('foo')->weeklyOn(1, '8:00');

Monthly Jobs, (*21)

$schedule->command('foo')->monthly();

Job That Runs On Specific Days, (*22)

$schedule->command('foo')->mondays();
$schedule->command('foo')->tuesdays();
$schedule->command('foo')->wednesdays();
$schedule->command('foo')->thursdays();
$schedule->command('foo')->fridays();
$schedule->command('foo')->saturdays();
$schedule->command('foo')->sundays();

Only Allow Job To Run When Callback Is True, (*23)

$schedule->command('foo')->monthly()->when(function()
{
    return true;
});

Only Allow Job To Run When skip Callback is False, (*24)

$schedule->command('foo')->monthly()->skip(function()
{
    return false;
});

Set Custom Description For Scheduled Job, (*25)

$schedule->command('foo')->monthly()->description('command description');

E-mail The Output Of A Scheduled Job, (*26)

$schedule->command('foo')->sendOutputTo($filePath)->emailOutputTo('foo@example.com');

To see the list of registered tasks, you can use the schedule/list command as below:, (*27)

php yii schedule/list

+---+-------------------------------------+-------------+-----------------------------------------------------+
| # | Task                                | Expression  | Command to Run                                      |
+---+-------------------------------------+-------------+-----------------------------------------------------+
| 1 | Delete the logs older than week     | 0 0 * * 0 * | /usr/bin/php5 yii app/delete-logs-older-then-week   |
+---+-------------------------------------+-------------+-----------------------------------------------------+

How to use this extension in your application?

You should create the following file under @console/config/schedule.php (note: you can create file with any name and extension and anywere on your server, simpli ajust the name of the scheduleFile in the command below):, (*28)

<?php
/**
 * @var \yii2mod\scheduling\Schedule $schedule
 */

// Place here all of your cron jobs

// This command will execute ls command every five minutes
$schedule->exec('ls')->everyFiveMinutes();

// This command will execute migration command of your application every hour
$schedule->command('migrate')->hourly();

// This command will call callback function every day at 10:00
$schedule->call(function(\yii\console\Application $app) {
    // Some code here...
})->dailyAt('10:00');

Next your should add the following command to your crontab:, (*29)

* * * * * php /path/to/yii yii schedule/run --scheduleFile=@console/config/schedule.php 1>> /dev/null 2>&1

That's all! Now all your cronjobs will be runned as configured in your schedule.php file., (*30)

How to use this extension in your own extension?

First of all, you should include dependency to the yii2mod\yii2-scheduling into your composer.json:, (*31)

'require': {
    "yii2mod/yii2-scheduling": "*"
}

Next you should create bootstrapping class for your extension, as described in the http://www.yiiframework.com/doc-2.0/guide-structure-extensions.html#bootstrapping-classes, (*32)

Place into your bootstrapping method the following code:, (*33)

public function bootstrap(Application $app)
{
    if ($app instanceof \yii\console\Application) {
        if ($app->has('schedule')) {
            /** @var omnilight\scheduling\Schedule $schedule */
            $schedule = $app->get('schedule');
            // Place all your shedule command below
            $schedule->command('my-extension-command')->dailyAt('12:00');
        }
    }
}

Add to the README of your extension info for the user to register schedule component for the application and add schedule/run command to the crontab as described upper., (*34)

Using schedule component

You do not have to use schedule component directly or define it in your application if you use schedule only in your application (and do not want to give ability for extensions to register they own cron jobs). But if you what to give extensions ability to register cronjobs, you should define schedule component in the application config:, (*35)

'schedule' => 'yii2mod\scheduling\Schedule',

Using addition functions

If you want to use thenPing method of the Event, you should add the following string to the composer.json of your app:, (*36)

"guzzlehttp/guzzle": "~5.3|~6.0"

Note about timezones

Please note, that this is PHP extension, so it use timezone defined in php config or in your Yii's configuration file, so set them correctly., (*37)

The Versions

26/09 2017

dev-master

9999999-dev

Scheduling extension for Yii2 framework

  Sources   Download

The Requires

 

The Development Requires

by Igor Chepurnoy
by Igor Chepurnoi

yii cron scheduling

26/09 2017

1.1.1

1.1.1.0

Scheduling extension for Yii2 framework

  Sources   Download

The Requires

 

The Development Requires

by Igor Chepurnoi

yii cron scheduling

22/09 2017

1.1

1.1.0.0

Scheduling extension for Yii2 framework

  Sources   Download

The Requires

 

The Development Requires

by Igor Chepurnoi

yii cron scheduling

05/03 2017

1.0.2

1.0.2.0

Scheduling extension for Yii2 framework

  Sources   Download

The Requires

 

The Development Requires

by Igor Chepurnoy

yii cron scheduling

05/03 2017

1.0.1

1.0.1.0

Scheduling extension for Yii2 framework

  Sources   Download

The Requires

 

The Development Requires

by Igor Chepurnoy

yii cron scheduling

03/03 2017

1.0

1.0.0.0

Scheduling extension for Yii2 framework

  Sources   Download

The Requires

 

The Development Requires

by Igor Chepurnoy

yii cron scheduling