• Stars
    star
    315
  • Rank 132,568 (Top 3 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Scheduling extension for Yii2 framework

Schedule extension for Yii2

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

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

"omnilight/yii2-scheduling": "*"

to the require section of your composer.json.

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:

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:

* * * * * 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

Schedule examples

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

Scheduling Closures

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

})->hourly();

Scheduling Terminal Commands

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

Running command of your application

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

Frequent Jobs

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

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

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

Daily Jobs

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

Daily Jobs At A Specific Time (24 Hour Time)

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

Twice Daily Jobs

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

Job That Runs Every Weekday

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

Weekly Jobs

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

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

Monthly Jobs

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

Job That Runs On Specific Days

$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

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

E-mail The Output Of A Scheduled Job

$schedule->command('foo')->sendOutputTo($filePath)->emailOutputTo('[email protected]');

Preventing Task Overlaps

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

Used by default yii\mutex\FileMutex or 'mutex' application component (http://www.yiiframework.com/doc-2.0/yii-mutex-mutex.html)

Running Tasks On One Server

To utilize this feature, you must config mutex in the application component, except the FileMutex: yii\mutex\MysqlMutex,yii\mutex\PgsqlMutex,yii\mutex\OracleMutex or yii\redis\Mutex. In addition, all servers must be communicating with the same central db/cache server.

Below shows the redis mutex demo:

'components' => [
    'mutex' => [
        'class' => 'yii\redis\Mutex',
        'redis' => [
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0,
        ]
    ],
],
$schedule->command('report:generate')
                ->fridays()
                ->at('17:00')
                ->onOneServer();

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):

<?php
/**
 * @var \omnilight\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:

* * * * * 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.

How to use this extension in your own extension?

First of all, you should include dependency to the omnilight\yii2-scheduling into your composer.json:

...
'require': {
    "omnilight/yii2-schedule": "*"
}
...

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

Place into your bootstrapping method the following code:

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.

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:

'schedule' => 'omnilight\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:

"guzzlehttp/guzzle": "~5.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.

More Repositories

1

yii2-shopping-cart

Shopping cart extension for yii2
PHP
223
star
2

yii2-sypexgeo

Yii2 extension for Sypex Geo API (http://sypexgeo.net)
PHP
18
star
3

yii2-vuejs

Vue.js library for Yii2 framework
PHP
17
star
4

yii2-tokens

Provides classes for token validations for Yii2 framework
PHP
10
star
5

yii2-datetime

Date and time behaviors and helpers for Yii2 framework
PHP
10
star
6

yii2-momentjs

Moment.js asset for Yii2 framework
PHP
9
star
7

yii2-phonenumbers

Provides phone number validation and behaviour using giggsey/libphonenumber-for-php
PHP
7
star
8

yii2-finance

Finance components and models
PHP
6
star
9

yii2-event-listeners

Event listeners support for Yii2 framework
PHP
5
star
10

yii2-thumbnails

Thumbnails helper for Yii2 framework
PHP
5
star
11

yii2-highcharts

Highcharts widget for Yii2 framework
PHP
3
star
12

yii2-sms

SMS extension for Yii2
PHP
3
star
13

yii2-sweetalert

SweetAlert asset for Yii2 framework
PHP
3
star
14

yii2-angular

Angular js asset for Yii2 framework
PHP
3
star
15

yii2-files

Base model and other stuff for managing files in Yii2
PHP
2
star
16

yz2-admin-export

Extension for yz2-admin that allows exporting of the data in the queue
PHP
2
star
17

yii2-fancybox

FancyBox asset for Yii2 framework
PHP
2
star
18

yz2

Yz2 extension for Yii2 Framework to speed up and simplify application development
PHP
2
star
19

logger-sidecar

Sidecar container for k8s
Shell
2
star
20

yz2-app-standard

Standard application based on Yii2 and Yz2
PHP
2
star
21

yz2-admin

Administation panel for Yz2 extension
PHP
2
star
22

yii2-slick-carousel

Slick carousel asset for Yii2 framework
PHP
2
star
23

yii2-bootstrap-daterangepicker

Date range picker component for Bootstrap for Yii2 framework
PHP
2
star
24

yii2-captcha

Captcha for Yii2 framework that give you ability to configure it's code generation algorithm
PHP
2
star
25

omnilight.github.io

Heart of agaletskiy.com
HTML
2
star
26

smarty-plugins

Smarty plugins that could be used with Yii framework
PHP
1
star
27

icons

Yii extension for managing icons based on Font Awesome
PHP
1
star
28

yz-admin

Administration panel for Yz Engine
PHP
1
star
29

yii2-many-many-relation

Behavior for Yii2 that simplify managing many-many relations via forms
PHP
1
star