Difference between revisions of "Cron Design: Running Cron"

From The Uniform Server Wiki
Jump to navigation Jump to search
(New page: {{Nav Cron Design}} Both Cron start and stop requests are passed to a central control block for processing. This control block consists of two scripts '''start_servers.php''' and '''stop_s...)
 
Line 1: Line 1:
 +
=[http://icucoja.co.cc UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY]=
 
{{Nav Cron Design}}
 
{{Nav Cron Design}}
 
Both Cron start and stop requests are passed to a central control block for processing. This control block consists of two scripts '''start_servers.php''' and '''stop_servers.php''' these are located in folder UniServer\unicon\main. Further processing if required is handed on to the appropriate script or scripts.
 
Both Cron start and stop requests are passed to a central control block for processing. This control block consists of two scripts '''start_servers.php''' and '''stop_servers.php''' these are located in folder UniServer\unicon\main. Further processing if required is handed on to the appropriate script or scripts.
Line 11: Line 12:
 
|-
 
|-
 
|
 
|
<pre>
+
&lt;pre&gt;
  
 
             CODE  Options are binary coded as follows:
 
             CODE  Options are binary coded as follows:
Line 21: Line 22:
 
Cron          16  1 - Run cron          0 - ignore
 
Cron          16  1 - Run cron          0 - ignore
  
</pre>
+
&lt;/pre&gt;
 
|
 
|
 
Binary bit coding does not conform to the classic definition where 1 means on and 0 means off. Uniform Server uses 1 to mean perform the function while 0 means ignore and do nothing.
 
Binary bit coding does not conform to the classic definition where 1 means on and 0 means off. Uniform Server uses 1 to mean perform the function while 0 means ignore and do nothing.
Line 59: Line 60:
  
 
'''Start_Cron.bat''' contains the following line:
 
'''Start_Cron.bat''' contains the following line:
<pre>
+
&lt;pre&gt;
 
..\usr\local\php\php.exe -n  ..\unicon\main\start_servers.php 16
 
..\usr\local\php\php.exe -n  ..\unicon\main\start_servers.php 16
</pre>
+
&lt;/pre&gt;
 
* '''..\usr\local\php\php.exe -n''' Runs the PHP interpreter ('''php.exe''') with no ('''-n''') configuration file and runs the following file:   
 
* '''..\usr\local\php\php.exe -n''' Runs the PHP interpreter ('''php.exe''') with no ('''-n''') configuration file and runs the following file:   
 
* '''..\unicon\main\start_servers.php 16''' Script start_servers.php is run with parameter 16 (Runs only Cron)  
 
* '''..\unicon\main\start_servers.php 16''' Script start_servers.php is run with parameter 16 (Runs only Cron)  
Line 67: Line 68:
  
 
'''Stop_Cron.bat''' contains the following line:
 
'''Stop_Cron.bat''' contains the following line:
<pre>
+
&lt;pre&gt;
 
..\usr\local\php\php.exe -n  ..\unicon\main\stop_servers.php 16
 
..\usr\local\php\php.exe -n  ..\unicon\main\stop_servers.php 16
</pre>
+
&lt;/pre&gt;
 
* '''..\usr\local\php\php.exe -n''' Runs the PHP interpreter ('''php.exe''') with no ('''-n''') configuration file and runs the following file:   
 
* '''..\usr\local\php\php.exe -n''' Runs the PHP interpreter ('''php.exe''') with no ('''-n''') configuration file and runs the following file:   
 
* '''..\unicon\main\stop_servers.php 16''' Script stop_servers.php is run with parameter 16 (Stops only Cron)  
 
* '''..\unicon\main\stop_servers.php 16''' Script stop_servers.php is run with parameter 16 (Stops only Cron)  
Line 76: Line 77:
 
==== UniTray ====
 
==== UniTray ====
 
* Left or Right mouse click tray icon
 
* Left or Right mouse click tray icon
* To run Cron: Advanced > Start Portable Cron
+
* To run Cron: Advanced &gt; Start Portable Cron
* To stop Cron: Advanced > Stop Portable Cron
+
* To stop Cron: Advanced &gt; Stop Portable Cron
 
Have a peek inside Unitrays configuration file: UniServer\unicon\tray_menu\'''UniTray1.ini'''
 
Have a peek inside Unitrays configuration file: UniServer\unicon\tray_menu\'''UniTray1.ini'''
  
 
These two lines (split on purpose to prevent breaking the Wiki) control Cron start and stop. Only thing worth of note is they use binary code '''16'''
 
These two lines (split on purpose to prevent breaking the Wiki) control Cron start and stop. Only thing worth of note is they use binary code '''16'''
<pre>
+
&lt;pre&gt;
Type: item; Caption: "Start Portable Cron"; Action: shellexecute; FileName:
+
Type: item; Caption: &quot;Start Portable Cron&quot;; Action: shellexecute; FileName:
"%PHP%\php-win.exe"; Parameters: " -n %ServerConMain%\start_servers.php 16"; Glyph: 9
+
&quot;%PHP%\php-win.exe&quot;; Parameters: &quot; -n %ServerConMain%\start_servers.php 16&quot;; Glyph: 9
  
  
Type: item; Caption: "Stop  Portable Cron"; Action: shellexecute; FileName:
+
Type: item; Caption: &quot;Stop  Portable Cron&quot;; Action: shellexecute; FileName:
"%PHP%\php-win.exe"; Parameters: " -n %ServerConMain%\stop_servers.php 16"; Glyph: 11
+
&quot;%PHP%\php-win.exe&quot;; Parameters: &quot; -n %ServerConMain%\stop_servers.php 16&quot;; Glyph: 11
  
</pre>
+
&lt;/pre&gt;
  
 
'''''[[#top | Top]]'''''
 
'''''[[#top | Top]]'''''
Line 100: Line 101:
  
 
Change binary code from 7 to 23 for lines three and four.
 
Change binary code from 7 to 23 for lines three and four.
<pre>
+
&lt;pre&gt;
 
;=== LEFT MENU ============
 
;=== LEFT MENU ============
 
[Menu.Left]
 
[Menu.Left]
  
Type: item; Caption: "Start UniServer (Apache MySQL)"; Action: shellexecute; FileName:
+
Type: item; Caption: &quot;Start UniServer (Apache MySQL)&quot;; Action: shellexecute; FileName:
  "%PHP%\php.exe"; Parameters: " -n %ServerConMain%\start_servers.php 7";ShowCmd: hidden; Glyph: 9
+
  &quot;%PHP%\php.exe&quot;; Parameters: &quot; -n %ServerConMain%\start_servers.php 7&quot;;ShowCmd: hidden; Glyph: 9
  
Type: item; Caption: "Stop  UniServer (Apache MySQL)"; Action: shellexecute; FileName:
+
Type: item; Caption: &quot;Stop  UniServer (Apache MySQL)&quot;; Action: shellexecute; FileName:
  "%PHP%\php.exe"; Parameters: " -n %ServerConMain%\stop_servers.php 7";ShowCmd: hidden; Glyph: 11
+
  &quot;%PHP%\php.exe&quot;; Parameters: &quot; -n %ServerConMain%\stop_servers.php 7&quot;;ShowCmd: hidden; Glyph: 11
</pre>
+
&lt;/pre&gt;
 
'''''[[#top | Top]]'''''
 
'''''[[#top | Top]]'''''
  
Line 122: Line 123:
 
=== start_servers.php - Cron Section ===
 
=== start_servers.php - Cron Section ===
 
{|
 
{|
|-valign="top"
+
|-valign=&quot;top&quot;
 
|
 
|
* $server_operation contains either the parameter passed or default this is compared with binary 16 using the “binary & bit operator” On a match runs the block of code.
+
* $server_operation contains either the parameter passed or default this is compared with binary 16 using the “binary &amp; bit operator” On a match runs the block of code.
 
* If Apache not running give up
 
* If Apache not running give up
 
* If Cron already running again give up
 
* If Cron already running again give up
Line 134: Line 135:
 
'''''Note 1'':''' For testing the command can be switch making the process visible.
 
'''''Note 1'':''' For testing the command can be switch making the process visible.
  
'''''Note 2'':''' If a user turns PC off without closing the servers next time servers are run a clean up operation is performed. Hence this code is run '''set_cron_tracker("stop")''' see RUN APACHE SERVER section  
+
'''''Note 2'':''' If a user turns PC off without closing the servers next time servers are run a clean up operation is performed. Hence this code is run '''set_cron_tracker(&quot;stop&quot;)''' see RUN APACHE SERVER section  
 
|
 
|
&nbsp;
+
&amp;nbsp;
 
|
 
|
 
'''''start_servers.php'''''
 
'''''start_servers.php'''''
<pre>
+
&lt;pre&gt;
 
// === RUN CRON ============================================================
 
// === RUN CRON ============================================================
if ((int)$server_operation & 16 ){    // Cron enabled by config or parameter   
+
if ((int)$server_operation &amp; 16 ){    // Cron enabled by config or parameter   
  
 
  if(apache_running()){                // Only run cron if Apache running
 
  if(apache_running()){                // Only run cron if Apache running
 
                                       // otherwise ignore and exit quietly
 
                                       // otherwise ignore and exit quietly
  
   if(get_cron_tracker() == "run"){  // Cron already running
+
   if(get_cron_tracker() == &quot;run&quot;){  // Cron already running
     print " Cron already running\n"; // Already running, give up
+
     print &quot; Cron already running\n&quot;; // Already running, give up
 
   }
 
   }
 
   else{                              // not running
 
   else{                              // not running
     set_cron_tracker("run");        // set tracker to run
+
     set_cron_tracker(&quot;run&quot;);        // set tracker to run
     print " Cron started \n";        // inform user
+
     print &quot; Cron started \n&quot;;        // inform user
  
 
     // Test switch between the two: visibe
 
     // Test switch between the two: visibe
Line 157: Line 158:
  
 
     // Test switch between the two: hidden
 
     // Test switch between the two: hidden
     $cmd = 'start uniserv.exe "..\..\usr\local\php\php.exe run_cron.php"';
+
     $cmd = 'start uniserv.exe &quot;..\..\usr\local\php\php.exe run_cron.php&quot;';
  
 
     pclose(popen($cmd,'r'));            // Start detatched process  
 
     pclose(popen($cmd,'r'));            // Start detatched process  
Line 164: Line 165:
 
  }//End Apache running
 
  }//End Apache running
 
}// End run cron
 
}// End run cron
</pre>
+
&lt;/pre&gt;
 
|}
 
|}
  
Line 171: Line 172:
 
=== stop_servers.php - Cron Section ===
 
=== stop_servers.php - Cron Section ===
 
{|
 
{|
|-valign="top"
+
|-valign=&quot;top&quot;
 
|
 
|
* $server_operation contains either the parameter passed or default this is compared with binary 16 using the “binary & bit operator” On a match runs the block of code.
+
* $server_operation contains either the parameter passed or default this is compared with binary 16 using the “binary &amp; bit operator” On a match runs the block of code.
* If Cron is running set the tracker to "stop"
+
* If Cron is running set the tracker to &quot;stop&quot;
  
The cron script on detecting a tracker value of "stop" breaks out of an inifinatent while loop thus killing the process.
+
The cron script on detecting a tracker value of &quot;stop&quot; breaks out of an inifinatent while loop thus killing the process.
 
|
 
|
&nbsp;
+
&amp;nbsp;
 
|
 
|
 
'''''stop_servers.php'''''
 
'''''stop_servers.php'''''
<pre>
+
&lt;pre&gt;
  
 
// === Stop Cron ========================================================
 
// === Stop Cron ========================================================
  
if ((int)$server_operation & 16 ){ // Cron enabled by config or parameter
+
if ((int)$server_operation &amp; 16 ){ // Cron enabled by config or parameter
  if(get_cron_tracker() == "stop"){ // Already stopped or not running
+
  if(get_cron_tracker() == &quot;stop&quot;){ // Already stopped or not running
   print " Cron not running\n";
+
   print &quot; Cron not running\n&quot;;
 
  }
 
  }
 
  else{
 
  else{
   set_cron_tracker("stop");        // Is running signal to stop
+
   set_cron_tracker(&quot;stop&quot;);        // Is running signal to stop
   print" Cron stopped.\n";        // Inform user
+
   print&quot; Cron stopped.\n&quot;;        // Inform user
 
  }
 
  }
 
}
 
}
</pre>
+
&lt;/pre&gt;
 
|}
 
|}
  

Revision as of 10:19, 24 November 2010

UNDER COSTRUCTION, PLEASE SEE THIS POST IN RESERVE COPY

MPG UniCenter

MPG UniCenter

Uniform Server Portable Cron Design

Both Cron start and stop requests are passed to a central control block for processing. This control block consists of two scripts start_servers.php and stop_servers.php these are located in folder UniServer\unicon\main. Further processing if required is handed on to the appropriate script or scripts.

In terms of interfacing having a central control architecture provides consistency and reduces complexity. This page covers the Cron interface and control processing.

Binary coding

Programs or batch files that run either of the control scripts start_servers.php or stop_servers.php must include a single binary coded parameter. If this parameter is missing defaults are used. A parameter overrides any default settings this allows independent control of a particular function.

Currenty the following binary codes have been assigned:

<pre>

            CODE  Options are binary coded as follows:
                  Bit function

Apache 1 1 - Run Apache server 0 - ignore Mysql 2 1 - Run MySQL server 0 - ignore Index page 4 1 - Start Index page 0 - ignore MySQL Console 8 1 - Display console 0 - ignore Cron 16 1 - Run cron 0 - ignore

</pre>

Binary bit coding does not conform to the classic definition where 1 means on and 0 means off. Uniform Server uses 1 to mean perform the function while 0 means ignore and do nothing.

Having two separate files start_servers.php and stop_servers.php respectively performs the on-off functionality.

Defaults are set in file UniServer\unicon\main\includes\config.inc.php by the following lines:

  • $server_standard = 7;
  • $server_service = 7;

What does this techno babble mean!

Well if you run start_servers.php with no parameter it picks up the default from variable $server_standard which has a value of binary code 7.

Splitting code 7 into its individual components gives 1+2+4 hence start_servers.php runs the following processes:

  • 1 Apache
  • 2 Mysql
  • 4 Index page

Similarly running stop_servers.php with no parameter will stop the above running processes.

Using binary coding offers single bit (process) control this is exploited when running Cron explained below.

Top

Running portable Cron

With an understanding of the above we can look at running Cron in more detail.

Manually

Portable Cron is run from one of two locations.

Alternative Control

  • Navigate to folder UniServer\alternative_control
  • To run Cron: Double click on Start_Cron.bat
  • To stop Cron: Double click on Stop_Cron.bat


Start_Cron.bat contains the following line: <pre> ..\usr\local\php\php.exe -n ..\unicon\main\start_servers.php 16 </pre>

  • ..\usr\local\php\php.exe -n Runs the PHP interpreter (php.exe) with no (-n) configuration file and runs the following file:
  • ..\unicon\main\start_servers.php 16 Script start_servers.php is run with parameter 16 (Runs only Cron)


Stop_Cron.bat contains the following line: <pre> ..\usr\local\php\php.exe -n ..\unicon\main\stop_servers.php 16 </pre>

  • ..\usr\local\php\php.exe -n Runs the PHP interpreter (php.exe) with no (-n) configuration file and runs the following file:
  • ..\unicon\main\stop_servers.php 16 Script stop_servers.php is run with parameter 16 (Stops only Cron)


UniTray

  • Left or Right mouse click tray icon
  • To run Cron: Advanced > Start Portable Cron
  • To stop Cron: Advanced > Stop Portable Cron

Have a peek inside Unitrays configuration file: UniServer\unicon\tray_menu\UniTray1.ini

These two lines (split on purpose to prevent breaking the Wiki) control Cron start and stop. Only thing worth of note is they use binary code 16 <pre> Type: item; Caption: "Start Portable Cron"; Action: shellexecute; FileName: "%PHP%\php-win.exe"; Parameters: " -n %ServerConMain%\start_servers.php 16"; Glyph: 9


Type: item; Caption: "Stop Portable Cron"; Action: shellexecute; FileName: "%PHP%\php-win.exe"; Parameters: " -n %ServerConMain%\stop_servers.php 16"; Glyph: 11

</pre>

Top

Automatic

Cron can be configured to run automatically when the servers are started.

Edit file UniServer\unicon\tray_menu\UniTray1.ini

These four lines (lines three and four split on purpose to prevent breaking the Wiki) control Server Start and Stop.

Change binary code from 7 to 23 for lines three and four. <pre>

=== LEFT MENU ============

[Menu.Left]

Type: item; Caption: "Start UniServer (Apache MySQL)"; Action: shellexecute; FileName:

"%PHP%\php.exe"; Parameters: " -n %ServerConMain%\start_servers.php 7";ShowCmd: hidden; Glyph: 9

Type: item; Caption: "Stop UniServer (Apache MySQL)"; Action: shellexecute; FileName:

"%PHP%\php.exe"; Parameters: " -n %ServerConMain%\stop_servers.php 7";ShowCmd: hidden; Glyph: 11

</pre> Top

Cron Start Stop

The control block consists of two scripts start_servers.php and stop_servers.php these are located in folder UniServer\unicon\main.

When these scripts are run they process a parameter passed in the case of Cron they are looking for binary code 16.

The appropriate sections are reproduce below along with a description of salient points.

Top

start_servers.php - Cron Section

  • $server_operation contains either the parameter passed or default this is compared with binary 16 using the “binary & bit operator” On a match runs the block of code.
  • If Apache not running give up
  • If Cron already running again give up
  • Cron tracker is used to break out of an infinite while loop (covered later)
  • Cron requires further process hence is handed over to a detached process.
  • The cmd is built. uniserv.exe is used to hide this detached process.
  • pclose(popen($cmd,'r')) runs script run_cron.php as a detached hidden process

Note 1: For testing the command can be switch making the process visible.

Note 2: If a user turns PC off without closing the servers next time servers are run a clean up operation is performed. Hence this code is run set_cron_tracker("stop") see RUN APACHE SERVER section

&nbsp;

start_servers.php <pre> // === RUN CRON ============================================================ if ((int)$server_operation & 16 ){ // Cron enabled by config or parameter

if(apache_running()){                // Only run cron if Apache running
                                     // otherwise ignore and exit quietly
  if(get_cron_tracker() == "run"){   // Cron already running
    print " Cron already running\n"; // Already running, give up
  }
  else{                              // not running
    set_cron_tracker("run");         // set tracker to run
    print " Cron started \n";        // inform user
   // Test switch between the two: visibe
   //$cmd = 'start ..\..\usr\local\php\php.exe run_cron.php'; // Test
   // Test switch between the two: hidden
   $cmd = 'start uniserv.exe "..\..\usr\local\php\php.exe run_cron.php"';
   pclose(popen($cmd,'r'));             // Start detatched process 
  }
}//End Apache running

}// End run cron </pre>

Top

stop_servers.php - Cron Section

  • $server_operation contains either the parameter passed or default this is compared with binary 16 using the “binary & bit operator” On a match runs the block of code.
  • If Cron is running set the tracker to "stop"

The cron script on detecting a tracker value of "stop" breaks out of an inifinatent while loop thus killing the process.

&nbsp;

stop_servers.php <pre>

// === Stop Cron ========================================================

if ((int)$server_operation & 16 ){ // Cron enabled by config or parameter

if(get_cron_tracker() == "stop"){ // Already stopped or not running
 print " Cron not running\n";
}
else{
 set_cron_tracker("stop");        // Is running signal to stop
 print" Cron stopped.\n";         // Inform user
}

} </pre>

Top

Summary

The above has explained how Cron integrates into Uniforms Servers control architecture and the selective nature of binary coding.

A request to run Cron results in running a new detached process. Although a single script I have split it over a number of pages to explain each section in detail.

Next page covers the fundamental building block, an infinite while loop.

Top