Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Implementing a Scheduled Task


Go to End


10 Posts   10079 Views

Avatar
pingu

Community Member, 75 Posts

18 May 2009 at 12:14pm

Edited: 18/05/2009 12:19pm

Hi,

I'm wondering if someone can give me some pointers on implementing a scheduled task in SS. I've never worked with scheduled tasks before so I am very new to this.

The scenario is that I have a product class like so:

<?php

class Product extends Page {
    static $db = array(
        'Price' => 'Currency',
    );
    static $has_one = array();

    function updatePrice(){
        loader = new Product_Updater();
        etc.
    }
 
}

class Product_Controller extends Page_Controller {
	
}

class Product_Updater extends CSVBulkLoader {

}

?>

I have an updatePrice() method that goes through and updates price of products in the database from a CSV. This is all working fine. Now what I need to do is implement a scheduled task so that this process will run once a day at a specific time (say 2am).

How would I go about doing this? Would it be something like:

class MyTask extends DailyTask {
    
    function process() {
        Product::updatePrice();
    }
}

I'm not sure how exactly the task scheduler in SS works, how I would register tasks, set times, and check the output. I would appreciate any help and guidance with this.

Avatar
bummzack

Community Member, 904 Posts

18 May 2009 at 6:12pm

Hi.
Did you read the documentation about scheduled-task? http://doc.silverstripe.com/doku.php?id=scheduledtask
Your class looks fine, you just need to call the URL or cli-script.php at the desired interval using a cron-job (or similar). Some hosting providers allow you to setup your own cron-jobs. If that's not possible, you could use a service like http://www.setcronjob.com/ (disclaimer: I never used that service before, it just popped up after a google search)

Avatar
pingu

Community Member, 75 Posts

18 May 2009 at 8:04pm

Hi banal,

Thanks for the reply - yes I did look through the documentation but as I've never implemented something like this before it makes me nervous. It wasn't clear to me how the scheduling would work.

Avatar
bummzack

Community Member, 904 Posts

18 May 2009 at 8:31pm

Hi pingu

In that case, you best get familiar with cron jobs first. Just search for "cronjob tutorials" or anything similar and you'll find lots of information on that behalf. If I were you, I'd start with a simple script to test and then later move to the SilverStripe way of doing things.

Avatar
pingu

Community Member, 75 Posts

26 May 2009 at 3:38pm

Edited: 26/05/2009 11:06pm

Okay so I've done my research and implemented my scheduled task which is working fine if I test it manually.
However when I try to run it using crontab I'm having some issues:

1. If I try using wget (wget domain.com/DailyTask), it's failing at login. Do I need to pass in login parameters somewhere??
2. If I try using cli-script.php (cli-script.php /DailyTask) I'm getting a bunch of warnings and errors

What's the best way to call the scheduled task?
Would appreciate any guidance with this

Avatar
bummzack

Community Member, 904 Posts

26 May 2009 at 6:24pm

Hi

I'd say wget is the easiest way. I never used the SilverStripe tasks myself, but requiring a login for it seems to be quite a bad decision by the developers... that won't work with a cron-job.
Maybe you just put the wrong URL in the cron-job? The docs (http://doc.silverstripe.com/doku.php?id=scheduledtask) says:
http://mydomain.com/silverstripe/DailyTask

Even with your wget call, I'd probably prefix it with http://

Avatar
jhirm

Community Member, 21 Posts

24 July 2009 at 9:13pm

I'm having the same problem(s) trying to implement the scheduled tasks with a cron job. I wish this were better documented. Anyone make any progress on this?

Avatar
jhirm

Community Member, 21 Posts

25 July 2009 at 4:37am

Edited: 25/07/2009 4:40am

Finally figured it out. For the benefit of anyone trying to use scheduled tasks with cron jobs, here's a fairly complete step-by-step:

1) Create whatever tasks you want to run by extending the appropriate class (see http://doc.silverstripe.com/doku.php?id=scheduledtask). Test your scheduled task via the browser as explained in the docs.

2) Manually create a cron job to execute the task at the desired interval. As mentioned in the thread above, it's wise to get familiar with cron jobs via some tutorials. To create the cron job on the server, you'll need command line access. Assuming you can SSH into the server, do so, and at the prompt type

 crontab -e 

to open the crontab file. Enter the cron job. Here is an example for the DailyTask:

MAILTO:test@yourdomain.com
00 12 * * * /usr/bin/php5-cgi /full/path/to/yoursiteroot/sapphire/cli-script.php DailyTask

The "00 12 * * *" portion tells cron to run the script at noon everyday (change the 12 to whatever hour you want). Notice that I needed to specify the full path to the php executable on the server in order to run cli-script.php as php. You may or may not need this depending on your server settings. Save this file. You should get a message saying something like "installing new crontab file."

3) Finally, I needed to add the _ss_environment.php file to the root of my site in order for the script to execute properly. This wasn't necessary on my local dev server, only on remote dev and live server... not sure why. A difference in the php settings, I guess. You can read about this file at http://doc.silverstripe.com/doku.php?id=sake. Here is an example of what it looked like in order to work for me:

<?php
global $_FILE_TO_URL_MAPPING;
$_FILE_TO_URL_MAPPING['/path/to/yoursiteroot'] = 'http://your.domain.com';
?>

I think that's it. Good luck.

Go to Top