PHPackages                             demokn/yii2-scheduling - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. demokn/yii2-scheduling

ActiveYii2-extension

demokn/yii2-scheduling
======================

Scheduling extension for Yii2 framework

095PHP

Since Aug 20Pushed 5y agoCompare

[ Source](https://github.com/demokn/yii2-scheduling)[ Packagist](https://packagist.org/packages/demokn/yii2-scheduling)[ RSS](/packages/demokn-yii2-scheduling/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (1)Used By (0)

Schedule extension for Yii2
===========================

[](#schedule-extension-for-yii2)

This extension is the port of Laravel's Schedule component ()

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

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

```

or add

```
"omnilight/yii2-scheduling": "*"
```

to the `require` section of your composer.json.

Description
-----------

[](#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
-----------------

[](#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('foo@example.com');
```

**Preventing Task Overlaps**

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

If needed, you may specify how many minutes must pass before the "without overlapping" lock expires. By default, the lock will expire after 24 hours:

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

**Running Tasks On One Server**

> To utilize this feature, your application must be using the `memcached` or `redis` cache driver as your application's default cache driver. In addition, all servers must be communicating with the same central cache server.

```
$schedule->command('report:generate')
                ->fridays()
                ->at('17:00')
                ->onOneServer();
```

**Background Tasks**

By default, multiple commands scheduled at the same time will execute sequentially. If you have long-running commands, this may cause subsequent commands to start much later than anticipated. If you would like to run commands in the background so that they may all run simultaneously, you may use the `runInBackground` method:

```
$schedule->command('report:generate')->daily()->runInBackground($currentScheduleFilePath);
```

> The `runInBackground` method may only be used when scheduling tasks via the `command` and `exec` methods.

How to use this extension in your application?
----------------------------------------------

[](#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):

```
