Jump to:

23486 Posts in 18995 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » Implementing a Scheduled Task

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

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

Page: 1 2
Go to End
Author Topic: 6243 Views
  • pingu
    Avatar
    Community Member
    75 Posts

    Implementing a Scheduled Task Link to this post

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Implementing a Scheduled Task Link to this post

    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)

  • pingu
    Avatar
    Community Member
    75 Posts

    Re: Implementing a Scheduled Task Link to this post

    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.

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Implementing a Scheduled Task Link to this post

    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.

  • pingu
    Avatar
    Community Member
    75 Posts

    Re: Implementing a Scheduled Task Link to this post

    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

  • banal
    Avatar
    Community Member
    901 Posts

    Re: Implementing a Scheduled Task Link to this post

    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://

  • jhirm
    Avatar
    Community Member
    21 Posts

    Re: Implementing a Scheduled Task Link to this post

    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?

  • jhirm
    Avatar
    Community Member
    21 Posts

    Re: Implementing a Scheduled Task Link to this post

    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.

    6243 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.