Creating cronjobs

  • Cronjobs or "time controlled tasks" are known from the unix/linux world and can be used to schedule reoccurring tasks on a given date and time. Sometimes web-hosting companies do not offer support for real unix/linux cronjobs, and this is where the Woltlab Community Framework comes into play.


    Woltlab has incorporated support for cronjobs in the WCF with the use of AJAX and PHP. When someone visits your website the WCF will check if any registered cronjobs have to be executed. When this is the case, cronjobs are executed asynchronously which means that your users won't even notice it.


    Adding a cronjob to your WCF package is very simply and straightforward.
    We start by creating an XML file to define the cronjob, let's name it cronjobs.xml.



    The classpath element holds the path to the cronjob PHP class file. All of the elements that begin with "start" control the date and time of the cronjob execution. Some characters have a special meaning: the asterisk (*) is a wildcard, a comma (,) can be used to concatenate values, a hyphen (-) can be used to enter a range and a forward slash (/) can be used for intervals.


    The following range of values can also be entered:
    startminute: 0 - 59
    starthour: 0 - 23
    startdom (day of month): 1 - 31
    startmonth: 1 - 12
    startdow (day of week): 0 - 7


    This means that the above cronjob is executed every hour every single day.


    The WCF cronjob definition has one more important element, which is the "execmultiple" element. Let's assume that we have no visitors on our website between 2:00 pm and 5:00 pm.

    • If execmultiple is set to 0, the cronjob will be executed only once when the website is visited at 5:01 pm.
    • If execmultiple is set to 1, the cronjob will be executed 4 times (2pm, 3pm, 4pm, 5pm) when the website is visited at 5:01 pm.


    Now we still have to create the PHP cronjob class. We name it OurFirstCronjob.class.php and place it in the system/cronjob folder.


    The only thing left to do now, is to register the cronjobs.xml file with our package (in the package.xml file):

    Code
    1. <instructions type="install">
    2. ...
    3. <cronjobs>cronjobs.xml</cronjobs>
    4. </instructions>

Share