yii-scheduler

Extension that can be used to schedule tasks for Yii projects via command line or database table
17 followers

yii-scheduler

Extension that schedules tasks for Yii projects

Description

This scheduler is intended to be used to schedule various operations that should be performed either once off (once) at a certain day and time or in a repeating fashion (hourly, daily, weekly or monthly). It provides a database table where it stores scheduled items and will determine which tasks should be run at any given moment when you run the run command. You can schedule urls to be visited via cURL or command line operations by providing the full path to the command.

Under the hood

Basically, you should set up a cron job to run the following command regularly. Maybe every hour or less.:

./yiic scheduler run

or

./yiic scheduler run --name="some name"

Then when cron executes the command, yii scheduler will look up the database and check if there are any entries (that are not flagged as deleted or executed) that have time values in the past. If it finds any matches, it will look up either the 'url' field and execute that via cURL or the 'command' field and run the command as is via phps shell_exec command. After the command has been run or url has been executed, if a flag other than --once has been set (ie: --hourly, --daily, --weekly, or --monthly), yii scheduler will created a new record in the table identical to the one just executed except with the time value incremented accordingly. There are problems with this approach if you stop your cron for a time and have a number of items in yii-scheduler that end up being some time in the past. If you then start up your cron again, you will get commands executing, incrementing and still being in the past and so executing again. I should fix this but currently dont have the time to do so. Be aware!

Installation

Grab the project from github: https://github.com/digitalsadhu/yii-scheduler

Copy the file SchedulerCommand.php into your protected/commands directory Add a scheduled task using the command line 'add' command. This will create the database table for you at the same time.

Usage

  1. Add scheduled tasks via the command line api (defined below) or by inserting entries in the yii-scheduler table in the database
  2. Set up a cron task to regularly run the run command and let the scheduler handle the rest

Command line API

add

Adds a new task, creating the table yii-scheduler if it doesn't exist. You can only created scheduled tasks in the future. Trying to create tasks in the past will throw and error

Flags

  • --name (required)
  • --time (required)
  • --url (optional)
  • --command (optional)
  • --once (optional)
  • --hourly (optional)
  • --daily (optional)
  • --weekly (optional)
  • --monthly (optional)

Examples:

./yiic scheduler add --name="some name" --time=2013-12-12 --url="http://google.com"

./yiic scheduler add --name="some name" --time=2013-12-12_13:00:00 --url="http://google.com"

./yiic scheduler add --name="some name" --time=2013-12-12 --url="http://google.com" --once

./yiic scheduler add --name="some name" --time=2013-12-12 --command="php /path/to/yiic.php myothercommand" --daily

./yiic scheduler add --name="some name" --time=2013-12-12 --url="http://google.com" --weekly

./yiic scheduler add --name="some name" --time=2013-12-12 --url="http://google.com" --monthly

removeall

Soft deletes all scheduled tasks by setting the deleted column to '1' in the database

Examples:

./yiic scheduler removeall

list

Lists all currently scheduled tasks

Examples:

./yiic scheduler list

run

Runs all scheduled tasks that are in the past. If the --name flag is not passed, all matching tasks will be executed. If the flag is passed in, only tasks with matching 'name' field in the database will be run.

Flags

  • --name (optional)

Examples:

./yiic scheduler run

./yiic scheduler run --name="some name"

help

List information and example usages of the various api commands

Examples:

./yiic scheduler help

Dependencies

  • php cURL extension

Notes/Caveats

  • This extension is built and tested on mac and linux. It hasn't been tested on windows. From the comments below it seems like windows users are having difficulties. (Any fixes can be submitted via pull requests on github)
  • You need to have php curl working to be able to use the extension fully. I suspect this may be the main issue windows users are experiencing. Please make sure you have php curl properly installed and tested on your machine.

Resources

...external resources for this extension...

Total 12 comments

#14059 report it
Richard Walker at 2013/07/16 04:32am
linux servers

@Sheillendra can you be more specific? What problem are you having exactly?

#14038 report it
Sheillendra at 2013/07/12 07:35pm
linux server

how create in linux server??

#14037 report it
Sheillendra at 2013/07/12 07:10pm
user authenticate

not work, windows 7

#14026 report it
Richard Walker at 2013/07/12 10:36am
cURL issues

Trần, from your screenshot, it looks like theres a problem with using cURL on windows. Do you have the php cURL extension enabled? Heres a link to a tutorial on how to set up cURL with php and apache on windows in case you don't: curl windows tutorial

Also, you can try running a 'command' instead of a 'url' (that way cURL won't be used) and see if that works. ie. enter a command into the 'command' column in your database table and delete the 'url' column.

#14025 report it
Trần Minh Quang at 2013/07/12 09:34am
Kaka. Run ok but not run when running sheduler :)

1./ Copy the file SchedulerCommand.php into your protected/commands directory.

2./ Go to wamp/www/myproject/protected/yiic scheduler add --name="somename" --time=2013-12-12_13:00:00 --url="http://google.com". [Note: i eidt time early 1/2 day]

3./ Go to wamp/www/myproject/protected/yiic scheduler run --name="somename"

==> Bug: My bug

#14022 report it
Trần Minh Quang at 2013/07/12 08:33am
I try but not work....

Thanks to @Yiivgeny for extension phpdoc-crontab. But do you test this extension?. It's not work for me [The following commands are available: - message - migrate - shell - webapp]. Not yet register command!

#14018 report it
Yiivgeny at 2013/07/12 04:32am
commandMap

Use commandMap and put command to extension folder, so separate application and 3-rd party extension for your users.

#14004 report it
Trần Minh Quang at 2013/07/11 07:54am
I try but not work....

My operating system: Windows 8 pro 32 bit

It doesn't work. Any solution for me, please!

Thanks you!

#14001 report it
Richard Walker at 2013/07/11 07:10am
Hmm

I don't think you need to define 'scheduler' inside your commandPath=>array() in console.php and main.php. It should just work without them. Have you tried not having a commandPath defined? Or perhaps commandPath => 'application.commands'

For me personally, I don't have any entry for commandPath in console.php or main.php and it works fine.

What operating system are you running?

#14000 report it
Trần Minh Quang at 2013/07/11 06:26am
I try but not work....

File SchedulerCommand

My console.php

My main.php

Bug: My bug

Please help me fix this bug! Thanks you very much!

#13996 report it
Richard Walker at 2013/07/11 04:18am
Some things to try...

Couple things to try...

  1. Did you put SchedulerCommand.php in protected/commands? Generally, you just need to drop the file into the commands directory and things should work

  2. Alternatively, has your command path been changed from the default of protected/commands? Perhaps it is set in main.php or console.php to something else? You can set it in config/main.php and config/console.php 'commandPath' => 'some/path',

#13989 report it
Trần Minh Quang at 2013/07/10 10:09pm
yiic not have command name scheduler!!!

The following commands are available: - message - migrate - shell - webapp

==> How to run command name scheduler?

Leave a comment

Please to leave your comment.

Create extension
Downloads
No downloadable files yet