Module Update: Task Scheduler

danneh3826
danneh3826's picture

Joined: 2007-08-18
Posts: 290
Posted: Sun, 2010-12-19 22:15

I'm releasing a beta of this Task Scheduler module, originally written by talmdal. This version incorporates the use of a crontab to run tasks as opposed to when the gallery process "shuts down" when a client is browsing. In the time now where certain modules are being written/released that run tasks that can be pretty length and where you don't want to sit there watching it all the time, this module is now ideal. It also implements a one-time interval, mainly for use in other modules so they can schedule tasks to run later that only need running once.

There's currently no docs/codex page for it yet, but you can download it here.

Use this thread to report problems/bugs with it. Note this this is BETA, so make sure you're running it on an installation that won't matter if things foul up :)

To use:
Enter a job in the crontab to call the cron.php script every so often. There's no right or wrong answer to how often you run it. I run every 15 minutes. Once the cron.php has been hit once, this module will STOP running jobs on gallery shutdown permanently. This will obviously speed up performance on installations where tasks (sometimes lengthy ones) are executed regularly. To do this (via shell); enter

*/15 * * * * /usr/bin/wget http://www.yourdomain.com/gallery3/modules/scheduler/cron.php

*/15 means every 15 minutes. */5 means every 5. @hourly is hourly, so on and so forth. Read more about cron expressions if you need more customisation, or don't know where to start/what to do next.

If you're a module developer and want to use this module to execute tasks with, follow this simple procedure. I'll create some tidier docs later for it. For example, my Amazon S3 module schedules item uploads as follows;

First, create a task definition:

$task_def =
    Task_Definition::factory()
        ->callback("aws_s3_task::upload_item")
        ->name("Amazon S3 item upload (ID: " . $item_id . ")")
        ->severity(log::SUCCESS);

$task = task::create($task_def, array("item_id" => $item_id));

Now you have a Task_Model object. We can now use this to create a schedule entry:

$schedule = ORM::factory("schedule");
$schedule->add_task($task);

And that's it. The task is now in the scheduler table waiting for the next time the cron.php file is hit. There's currently no way for a module to schedule a task to run periodically, as I've not found the need for this. However, it's easily implementable if necessary.

Hope this all helps. If it's any good, I'll tidy up the code, shift it out of beta and push it into github, or if talmdal spies this thread and wants to tweak it, he's more than welcome to :)

Dan

 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 27300
Posted: Tue, 2010-12-28 17:31

I have created a skeletal page: http://codex.gallery2.org/Gallery3:Modules:scheduler
Feel free to flesh it out a bit. Looking forward to seeing the community use this module to extend other modules to add extra functionality. I think one use would be to delete some old files or items as one user brought this up some time ago.

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team

 
danneh3826
danneh3826's picture

Joined: 2007-08-18
Posts: 290
Posted: Tue, 2010-12-28 23:45

Awesome, thanks Dave. However, scheduler is spelt wrong, so I made a new page and copied the contents across. I don't know how to delete pages though, so I just had to link to it from the incorrectly spelt one. I'll get round to populating that page with details for other developers to use this module in the same way I have done with aws_s3.

In the meantime, I've released a new version which I've started beta numbering to keep track of what's going on.

Download here.

Changelog:
- Extended the Task_Definition library to add flags to allow tasks to only be run there-and-then or only scheduled.
- Added "scheduled" flag to the Task model, which prevents tasks from showing as "running" and eventually "stalled" when the crontab hasn't executed yet. Tasks do not appear as "running" unless they are now actually running.

Bug reports on a postcode. Developer docs coming shortly - I promise! :)

Dan

danneh.org :: Gallery3

 
floridave
floridave's picture

Joined: 2003-12-22
Posts: 27300
Posted: Wed, 2010-12-29 00:15

Thanks! I will fix the spelling dupe thing.

Dave
_____________________________________________
Blog & G2 || floridave - Gallery Team

 
dmolavi
dmolavi's picture

Joined: 2002-12-05
Posts: 573
Posted: Wed, 2011-01-12 19:20

Dan-
This module would be perfect to use with my ecard module for the scheduling of ecards. However, I would need the functionality that other modules be able to have tasks run periodically, as opposed to once. I was about to code this all from scratch until I saw this module...

--
For Gallery 3 modules and themes, visit GalleryModules.com.
For PHPNuke, phpBB2, and phpBB3 integration help, visit NukedGallery.net.

 
danneh3826
danneh3826's picture

Joined: 2007-08-18
Posts: 290
Posted: Sat, 2011-01-22 22:39

fixed a quick issue with this module and the cron not running properly, and thinking it's still running when it's not.

fixed copy in the same place as beta1: http://www.danneh.org/files/scheduler1.zip

dmolavi; i do have plans to increase the functionality of this for other modules. i've only gone so far at present for my requirements, but knowing that there's other people wanting the same functionality, i'm more inclined to make it more flexible. so i shall :) i'll post something in a couple days i think.

and sorry for the delayed reply lol

Dan

danneh.org :: Gallery3

 
blackhole86

Joined: 2010-08-06
Posts: 16
Posted: Sat, 2011-02-19 19:48
danneh3826 wrote:
fixed a quick issue with this module and the cron not running properly, and thinking it's still running when it's not.

fixed copy in the same place as beta1: http://www.danneh.org/files/scheduler1.zip

dmolavi; i do have plans to increase the functionality of this for other modules. i've only gone so far at present for my requirements, but knowing that there's other people wanting the same functionality, i'm more inclined to make it more flexible. so i shall :) i'll post something in a couple days i think.

and sorry for the delayed reply lol

Dan

danneh.org :: Gallery3

The link posted don't work

 
wise_mike

Joined: 2008-10-21
Posts: 158
Posted: Sun, 2011-02-20 04:49

Using that module to help in the eCard module would be a great option.. Waiting.. and thanks..

 
tatster

Joined: 2005-02-14
Posts: 2
Posted: Thu, 2011-04-28 21:42

Download Link not working for me either I'm afraid.
Tried all of the links to the scheduler download in the thread and they all don't work - "Download does not exist!"

 
danneh3826
danneh3826's picture

Joined: 2007-08-18
Posts: 290
Posted: Thu, 2011-04-28 21:47
 
talmdal

Joined: 2006-12-06
Posts: 358
Posted: Mon, 2011-10-03 03:04

I just noticed, and it is an interesting concept. I would suggest that it might be worth renaming your version to a separate module (i.e. cron_scheduler) just in case someone goes and updates the version in -contrib. I think your functionality is different enough that it deserves to be in a separate module.

Personally, having an unsecured exposed URL such as "http://www.yourdomain.com/gallery3/modules/scheduler/cron.php", is not something I would consider on my installation. What you might want to consider as well is use the PHP CLI (*/15 * * * * php module/cron_scheduler/cron.php) and then put checks in the cron.php to insure that it is only run from the command line.

However, using the url approach does have the advantage of being able to schedule tasks on a different server then have it send the request to the Gallery installation. What you might consider doing is provide a unique key that is passed as a parameter. (Look at the REST api as example of creating an authorization key, which can be used to authenticate a remote user) So the url would look something like: http://www.yourdomain.com/gallery3/modules/scheduler/cron.php?key=<md5hashkey>.

Hmm, taking that the next step forward, just turn it into a controller and then let the framework handle it as opposed to loading the framework yourself.

On another note, how do you handle that most gallery3 tasks are set up to only run 1 or 2 iterations per invocation. Typically when a task is run from the UI, there is jquery code to submit multiple requests until the task is complete. So in the original version, it doesn't run the entire task on a given request, but 1 or more iterations. I didn't see how you handled this and was just wondering.

Now you got me thinking... No guarantees (other life priorities are imposing on my gallery time), but I might go tinker with this a bit.

Tim
http://www.timalmdal.com

 
prosumer

Joined: 2008-01-05
Posts: 6
Posted: Sat, 2011-12-10 16:49

Hi,

Can I use this module to schedule the "server_add" module? How?

Thanks
Hans