elisteam/yii2-cronjobs

cronjobs

Installs: 336

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 37

Type:yii2-extension

dev-master 2016-02-18 12:07 UTC

This package is not auto-updated.

Last update: 2024-09-20 18:28:48 UTC


README

Easiest way to put crontab on your console scripts.

This extension is based on this. Thanks Yiivgeny.

And next, Denis has adapted it for yii2. I was only updated it for my needs and continue developing.

Changes from origin:

  • Work with yii2
  • Set config in params (not in phpDocs).

Denis transfer ​​settings of crontab in local settings(params) configuration, so that the application can be run on different servers with different sets of crontab.

Installation

Step 1

The preferred way to install this extension is through composer.

Next, need to run:

composer require --prefer-dist elisteam/yii2-cronjobs "dev-master"

or add:

...
"require": {
    ...
    "elisteam/yii2-cronjobs": "dev-master"
}

to the require section of your composer.json file.

Step 2: Set aliase

Set aliase @runnerScript in console config. This absolutely path to runner script (I can not find another way to get runner script). Change path to runner script as your project.

Yii::setAlias('@runnerScript', dirname(dirname(dirname(__FILE__))) .'/yii');

Step 3: Add to console config

Edit this file (or ohter your own console config):

config/console.php
return [
    //...
    'controllerMap' => [
    	'cron' => [
    	    'class' => 'elisteam\cronjobs\CronController'
    	],
    ],
]

Step 4: Add task to system scheduler

Add task to system scheduler (cron on unix, task scheduler on windows) to run every minute:

* * * * * /path/to/yii/application/protected/yii cron

Usage

Add in params array with cron sets:

'cronJobs' =>[
    'test/example1' => [
    	'cron'      => '* * * * *',
    ],
    'test/example2' => [
    	'cron'      => '10 * * * *',
    ],
],

You can point any settings from this.

More usage examples

1 Use log path for stdout:
/**
 * Parameters stdout and stderr can be a mask including next variables:
 *     %L - yii2 logsDir
 *     %C - command name
 *     %A - action name
 *     %P - PID of running process
 *     %D(format) - date format; the same sintax as that date()
 *
 */
 return [
    'cronJobs' =>[
    	'test/example1' => [
    	    'cron'        => '* * * * *',
    	    'cron-stdout' => '/tmp/Example_%C.%A.log'
    	],
    ]
]
2 Using custom stderr:
return [
    'cronJobs' =>[
    	'test/example2' => [
    	    'cron'        => '* * * * *',
    	    'cron-stdout' => '/tmp/Example_%C.%A.log',
    	    'cron-stderr' => '/tmp/ExampleCommandError.log'
    	],
    ]
]
3 Using args:
return [
    'cronJobs' =>[
    	'test/example2' => [
    	    'cron'        => '* * * * *',
    	    'cron-args' => '--limit=5 --offset=10'
    	],
    ]
]
4 Using tags:

This will be work only if run command will have a dbserver or cacheserver tags. For example:

./yii cron run   dbserver storageserver
return [
    'cronJobs' =>[
    	'test/example2' => [
    	    'cron'        => '* * * * *',
    	    'cron-tags' => 'dbserver cacheserver'
    	],
    ]
]
5 Using extended time format:
return [
    'cronJobs' =>[
    	'test/example2' => [
    	    'cron'        => '10,25-30,40 *\2 15-21,23-27 1-6\2 *'
    	],
    ]
]