15. Setup Cron jobs - ihofmann/open-websoccer GitHub Wiki

Execute jobs via Cron

The recurring background tasks, such as the match simulation or updating statistics, are triggered by jobs. By default, you start these jobs in the AdminCenter at Jobs. This page will send infinite requests which usually stop only once the server is restarted.

However, besides the fact that you should monitor the jobs state at least daily, many servers are configured in a way that they do not allow infinite requests. In this case, the jobs will run only for a couple of minutes. The only reliable way of running the jobs is setting up Cron jobs. These are processes which are started by the operating system at configured time intervals. Even when the server is restarted, it will automatically restart the jobs as well.

Most webhosters provide the possibility to setup Cron jobs, however often times only at a premium. Some providers allow Cron jobs, but not those which run every minute (as we would need for the simulation). Please get in touch with your provider in order to find out about the possibilities.

Create a Cron job script

You need to create a separate script file for every job. We recommend to set up such a script at least for the match simulation.

You need to know the ID of the job first. You will find all IDs in the file /admin/config/jobs.xml. For example, the ID of the simulation job is sim.

As next step, we create a new PHP file, e.g. "executeSimJob.php". Due to security, it is recommended that the file should not be accessible via a web browser. Hence, upload the file one level above your public folders.

Note: The public folder is usually called /html or /htdocs or /httpdocs. There you will also find your index.html or index.php files.

Copy the following content into your new file:

<?php 
// Path to the websoccer installation
define("BASE_FOLDER", __DIR__ ."/path/to/websoccer");

// ID of the Job
$jobId = "sim";

// Code below can remain without changes
define("JOBS_CONFIG_FILE", BASE_FOLDER . "/admin/config/jobs.xml");
include(BASE_FOLDER . "/admin/config/global.inc.php");
$xml = simplexml_load_file(JOBS_CONFIG_FILE);
$jobConfig = $xml->xpath("//job[@id = '". $jobId . "']");
$jobClass = (string) $jobConfig[0]->attributes()->class;
$i18n = I18n::getInstance($website->getConfig("supported_languages"));
$job = new $jobClass($website, $db, $i18n, $jobId);
$job->execute();
?>

Note about that path that you need to specify in the very beginning of the script: Assuming you have the following folder structure on your server:

  • htdocs/
    • websoccer/
      • admin/
      • index.html
  • executeSimJob.php

Then the first line would look like this:

define("BASE_FOLDER", __DIR__ ."/htdocs/websoccer");

Execute Cron jobs

You talk about "Cron jobs" usually only in the Unix/Linux world. Most of the web servers use such an operating system. If you use a Windows machine, you need to create a "Scheduled Task". In the following, we explain how you would set up a Cron job under Unix/Linux.

Usually your web hoster will provide a web form for configuring Cron jobs. Depending on how user friendly they are designed, you can specify a point of time of execution in a special format. You need to specify in a particular order at which minute, hour, day, month and day of week the job shall be executed. You can use a wildcard sign * which means "irrelevant" and the characters combination */ indicates an interval. For executing our simulation job every minute, the configuration would look as follows:

  • Minute: */1
  • Hour: *
  • Day: *
  • Day of week: *
  • Month: *

In addition, you need to specify a command for this job. For the example above, this command would look like below:

/usr/bin/php ~/executeSimJob.php

Whereas the path /usr/bin/php depends on your server setup. The ~ represents the folder to which you have access with your common FTP account. Again, all paths depend on how the server is configured.