Cron Design: Cron Script Part 1

From The Uniform Server Wiki
Revision as of 16:47, 9 October 2009 by Ric (talk | contribs) (New page: {{Nav Cron Design}} Previous page covered how Cron integrates into Uniforms Servers control architecture and the selective nature of binary coding. If Cron is not running any requests to r...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

MPG UniCenter

MPG UniCenter

Uniform Server Portable Cron Design

Previous page covered how Cron integrates into Uniforms Servers control architecture and the selective nature of binary coding. If Cron is not running any requests to run Cron produces a new hidden detached process that runs script run_cron.php .

This page covers the fundamental building block of this script.

Overview

Cron script and support files are located in folder UniServer\unicon\main

Description

Cron consists of the main script run_cron.php and two support files cron.ini and cron_tracker.txt

  • run_cron.php Runs an infinite while loop which periodically scans the configuration file cron.ini
  • cron.ini Contains configuration settings for each script to be run at a defined time.
  • cron_tracker.txt Tracks Cron status.
    • Before Cron script is called server_start.php sets this to a value of run
    • A user (other scripts) sets this to stop, forces exit from infinite loop.
  • There are two timing loops explained below.

Files

run_cron.php

cron.ini

cron_tracker.txt

Top

Timing loops

Description

Script run_cron.php executes an infinite while loop.

On initial entry the tracker file cron_tracker.txt will have been set to run by server_start.php. The tracker value is periodically checked. Period defined by the base timer, currently set to one second. When value changes to stop (set by another script) the while loop and script are terminated.

Note: A base time of one second was chosen to minimise latency when a user initiates Cron stop, giving user a more responsive feel.

Cron timer has been set to one minute this provides the basic Cron tick. Implementation is via a loop counter its value being incremented at the base timer rate.

Every time around the while-loop this loop counter is compared to Cron time. If its value is smaller than Cron time the loop counter is incremented a delay of one second (base time) follows before the while loop is executed again.

When the loop counter equals Cron time it runs the main foreach loop code (covered on next page) this runs any scripts that match the current time. On completion the loop counter is reset to zero then incremented followed by a delay of one second before the while loop is executed again.

Note: To suite individual requirements both base and Cron time can be changed. However one second and one minute are a good compromise.

Top

Code snippet

Description:

For completeness I have show how the above flow diagram maps to real code there is almost a one-to-one mapping.

  • An infinite loop is created using while(TRUE)
  • Tracker is checked if not equal to run the script is terminated using break
  • Loop counter and cron time are checked. If equal main foreach loop is executed. Otherwise is skipped and loop counter incremented wait for one second before repeating while loop.

Note: Initial value of $loop is set equal to $cron_time

  • Forces main foreach loop to be executed when Cron script is first run.
  • This also initialises the loop counter to zero.
//== Variables
$cron_time = 60;             // Set cron time (tick) in seconds. 
$loop      = $cron_time;     // Set equal allows immediate first time run

//== Main loop
while(TRUE){                       // Infinite loop.
 if(get_cron_tracker() != "run"){  // Check every second for user cron stop
   break;                          // reqest. Breaks out of while loop and 
 }                                 // kills script.
 else{
   if($loop == $cron_time){        // True for first entry hence immediately
                                   // runs scriptss. There after run at
                                   // cron time.

     //############# To be added foreach Loop #####################

     $loop =0;           // Reset loop counter
   }
   $loop = $loop+1;      // Increment loop counter
  }
  sleep(1);              // Base delay one second allows stop to be checked 
}

Top

Summary

That covers the fundamental building block it provides a periodic tick that runs a foeach loop covered on the next page.

Top