https://wiki.uniformserver.com/index.php?title=LCC-win32:_Drupal_Cron&feed=atom&action=historyLCC-win32: Drupal Cron - Revision history2024-03-28T15:09:16ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=LCC-win32:_Drupal_Cron&diff=4931&oldid=prevOlajideolaolorun: Reverted edits by Upazixorys (Talk); changed back to last version by Ric2010-11-24T17:38:02Z<p>Reverted edits by <a href="/Special:Contributions/Upazixorys" title="Special:Contributions/Upazixorys">Upazixorys</a> (<a href="/index.php?title=User_talk:Upazixorys&action=edit&redlink=1" class="new" title="User talk:Upazixorys (page does not exist)">Talk</a>); changed back to last version by <a href="/User:Ric" title="User:Ric">Ric</a></p>
<a href="https://wiki.uniformserver.com/index.php?title=LCC-win32:_Drupal_Cron&diff=4931&oldid=4869">Show changes</a>Olajideolaolorunhttps://wiki.uniformserver.com/index.php?title=LCC-win32:_Drupal_Cron&diff=4869&oldid=prevUpazixorys at 10:21, 24 November 20102010-11-24T10:21:52Z<p></p>
<a href="https://wiki.uniformserver.com/index.php?title=LCC-win32:_Drupal_Cron&diff=4869&oldid=3492">Show changes</a>Upazixoryshttps://wiki.uniformserver.com/index.php?title=LCC-win32:_Drupal_Cron&diff=3492&oldid=prevRic: New page: {{Uc nav lcc-win32}} '''LCC Drupal Cron''' (Portable Cron) On the previous page I covered a simple delay program this page expands on that to produce a portable cron application targeted ...2008-10-04T12:42:12Z<p>New page: {{Uc nav lcc-win32}} '''LCC Drupal Cron''' (Portable Cron) On the previous page I covered a simple delay program this page expands on that to produce a portable cron application targeted ...</p>
<p><b>New page</b></p><div>{{Uc nav lcc-win32}}<br />
'''LCC Drupal Cron''' (Portable Cron)<br />
<br />
On the previous page I covered a simple delay program this page expands on that to produce a portable cron application targeted at Drupal. I covered how to use unidelay.exe to produce a generic cron application on [[Portable Cron | this page]] it is worth reading for background information.<br />
<br />
'''''[[#top | Top]]'''''<br />
== Requirement ==<br />
Our program will periodically run the following system command line:<br />
<pre><br />
udrive\usr\local\php\php.exe -n udrive\www\run_cron.php<br />
</pre><br />
It runs '''php.exe''' in CLI mode (switch '''-n''' prevents php.ini file being read), which in turn runs script '''run_cron.php''' located in the server web root folder www. This script contains a single line which runs Drupal’s '''cron.php''' script.<br />
<br />
'''''[[#top | Top]]'''''<br />
== Proposed solution ==<br />
The above line can be hard-coded within a delay loop. Assumes a fixed time; a user will always install Drupal in the root folder www and location of the PHP executable will never change.<br />
<br />
=== Restrictive ===<br />
Hard coding makes the program very restrictive, suppose a user wishes to have a different delay or decides to install Drupal in a sub-folder named drupal or some other folder. The location of PHP executable is unlikely to change but is possible.<br />
<br />
=== Flexibility ===<br />
Remember we have the ability to pass parameters to our program making it much more flexible. If the above hard-coded variables are made '''defaults''' they can be '''overridden using parameters'''.<br />
<br />
Order of parameters passed to our program is important, what will a user most likely want to change. I would go for '''delay''' followed by '''path to Drupal''' the least likely will be the '''path to PHP''' unless a user has redesigned Uniform Server. However including it as a parameter increases flexibility.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Implementation ==<br />
The command line to run our program looks similar to the following:<br />
<br />
drupal_cron.exe delay path_to_drupal path_to_php<br />
<br />
You will notice I have given the program and parameters a name these can be used later within the program as variables. <br />
<br />
I will place one restriction; our program shall be located in folder “'''Uniform Server'''” or any folder above both php and drupal folders. This means the '''paths are relative''' to the location of our program. Makes it both easier to program and for a user to correctly name the paths.<br />
<br />
'''''[[#top | Top]]'''''<br />
== Code flow ==<br />
This should be a flow diagram however a few words can express the intended idea and logic. It can be a pain, keep it short. <br />
<br />
*Start<br />
*Set defaults<br />
*If no delay parameter use default<br />
*If no path_to_drupal parameter use default<br />
*If no path_to_php parameter use default<br />
*Start loop<br />
*Delay<br />
*Run system command path_to_php\php.exe -n path_to_drupal\run_cron.php<br />
*Go to Start loop<br />
*End<br />
<br />
'''''[[#top | Top]]'''''<br />
== Coding ==<br />
From the above you can start coding. I tend to add loads of comments in the hope when re-read in the future I will understand it. Its all a mater of personal style the following is one coding solution:<br />
<pre><br />
#include <stdheaders.h><br />
#include <windows.h><br />
#include <io.h><br />
<br />
int main(int argc,char *argv[])<br />
{<br />
//== Variables<br />
int delay; // Delay required<br />
char path_to_drupal[80]=""; // relative pat to Drupal<br />
char path_to_php[80]=""; // relative path to php<br />
char error_message[200]=""; // error message string<br />
char cmd_string[200]=""; // command line string<br />
<br />
//== Default values<br />
int default_delay=600000; // 1000ms gives 1s delay<br />
// 600000 = 10 mins<br />
char default_path_to_drupal[80]="udrive\\www\\run_cron.php";<br />
char default_path_to_php[80]="udrive\\usr\\local\\php\\php.exe";<br />
<br />
//== Set variables according to parameter supplied ===================<br />
if(argc==1){ // No parameters<br />
delay = default_delay; // hence use defaults<br />
strcat(path_to_drupal, default_path_to_drupal);<br />
strcat(path_to_php, default_path_to_php);<br />
}<br />
<br />
if(argc==2){<br />
delay = default_delay * (int)atoi(argv[1]); // New delay required<br />
strcat(path_to_drupal, default_path_to_drupal); // All other use<br />
strcat(path_to_php, default_path_to_php); // defaults<br />
}<br />
<br />
if(argc==3){<br />
delay = default_delay * (int)atoi(argv[1]); // New delay required<br />
strcat(path_to_drupal, argv[2]); // New drupal path<br />
strcat(path_to_php, default_path_to_php); // Use default<br />
}<br />
<br />
if(argc==4){<br />
delay = default_delay * (int)atoi(argv[1]); // New delay required<br />
strcat(path_to_drupal, argv[2]); // New drupal path<br />
strcat(path_to_php, argv[3]); // New php path<br />
}<br />
//== End set vars =====================================================<br />
<br />
//== Check if files exists ============================================<br />
if( _access(path_to_drupal,0) !=0 ) // File not found alert user<br />
{<br />
strcat(error_message, path_to_drupal);<br />
strcat(error_message, "\n\n File not found\n\n No action taken");<br />
MessageBox (NULL, error_message , "Path to Drupal", 0);<br />
return 1; // Give up its not going to work<br />
}<br />
<br />
if( _access(path_to_php,0) !=0 ) // File not found alert user<br />
{<br />
strcat(error_message, path_to_php);<br />
strcat(error_message, "\n\n File not found\n\n Noaction taken");<br />
MessageBox (NULL, error_message , "Path to PHP", 0);<br />
return 1; // Give up its not going to work<br />
}<br />
<br />
//== End Check if files exists ============================================<br />
<br />
//== Build command string ==============================<br />
strcat(cmd_string, path_to_php);<br />
strcat(cmd_string, " -n "); // Prevents ini read<br />
strcat(cmd_string, path_to_drupal);<br />
//== End Build command string ==========================<br />
<br />
//== Infinite loop, run process hidden =====================<br />
while (1){ //loop forever<br />
_System(cmd_string, SW_HIDE); // Run hidden child process<br />
sleep(delay); // Delay for specified time<br />
}<br />
//== End Infinite loop, run process hidden ===================<br />
<br />
return 0; // Exit! Will never reach this point beause<br />
// process exrernally killed.<br />
}<br />
</pre><br />
<br />
'''''Note'':''' If you are looking for particular functions check out LCC standard library. Click Help link (top right ) from the drop down menu you will find various help files including the standard library.<br />
<br />
The code is self-explanatory however there are a few things worthy of note.<br />
<br />
'''''[[#top | Top]]'''''<br />
=== File existence ===<br />
<br />
It is possible to create your own function to check the existence of a file the code would look similar to this:<br />
<pre><br />
bool file_exists(const char * filename)<br />
{<br />
FILE *fp;<br />
if (fp = fopen(filename, "r"))<br />
{<br />
fclose(fp);<br />
return true;<br />
}<br />
return false;<br />
}<br />
</pre><br />
This may in certain situations produce unreliable results hence its far better to use a function specifically designed for this task hence the reason for using this function:<br />
<pre><br />
_access(path_to_drupal,0)<br />
</pre><br />
It returns "0" if the file exists otherwise it returns "-1" <br />
<br />
'''''[[#top | Top]]'''''<br />
=== Command Window ===<br />
If you use the standard system command to run php.exe<br />
<pre><br />
system(cmd_string)<br />
</pre><br />
a command window opens until the program executions ends. Although relatively fast it produces an annoying black screen flash.<br />
<br />
Ideally the program should be run in a hidden window hence the use of this function:<br />
<pre><br />
_System(cmd_string, SW_HIDE)<br />
</pre><br />
<br />
'''''[[#top | Top]]'''''<br />
== Creating a new LCC project ==<br />
On the previous page I used different names for the project folder and executable it makes it slightly easier to create a new project if the names are identical. I will be creating the following project drupal_cron<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Create Project ===<br />
# Start LCC<br />
# Close any open projects: '''Project > Close''' click '''Yes''' to confirm<br />
# Create project: '''Project > Create''' opens '''Definition of new project''' window<br />
## Name of the new project: Enter '''drupal_cron'''<br />
## Sources: Click '''Browse''' navigate to E:'''\lcc\projects''' click '''OK''' add '''\drupal_cron''' to give '''E:\lcc\projects\drupal_cron'''<br />
## Output directory: '''Click in''' this window '''automatically updates''' to give '''E:\lcc\projects\drupal_cron\lcc'''<br />
## Type of project: Radio button defaults to Console Application leave selected<br />
## Click '''Create''' (button top right) <br />
# Do you want to use the wizard to generate the application skeleton: Click '''Yes'''<br />
# Application characteristics displayed: Click '''OK''' click '''OK''' to Lcc wizard pop-up<br />
# Compiler settings displayed: Click '''Next'''<br />
# Linker settings displayed: Click '''Next'''<br />
# Debugger settings displayed: Click '''Finish'''<br />
<br />
Project page drupal_cron.c will be displayed<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Edit Code ===<br />
# Delete everything in '''drupal_cron.c'''<br />
# '''Copy''' the above code into '''drupal_cron.c'''<br />
# '''File > Save'''<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Test for errors ===<br />
# '''Compiler > Make''' There should be no errors<br />
# '''Compiler > Execute drupal_cron.exe''' Path to Drupal pop file not found - Indicates its working<br />
# Create the following folder chain: E:\lcc\projects\drupal_cron\lcc\'''udrive\www'''<br />
# In folder www create a blank file named '''run_cron.php'''<br />
# '''Compiler > Execute drupal_cron.exe''' Path to PHP pop file not found - Indicates its working <br />
<br />
Note: You can create a similar path to php and use a dummy file. <br />
The program is working lets create the final code.<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Configuration ===<br />
# '''Project > Configuration''' opens configuration window.<br />
# Click Compiler tab and set the following:<br />
## '''Check''' Optimise<br />
## '''Check''' Eliminate unused assignments<br />
## '''UnCheck''' Generate debug info<br />
# Click Linker tab and set the following:<br />
## Type of output: Click in '''Windows appl radio button'''<br />
## Click '''OK''' Allow to rebuild '''OK''', Ignore any errors click '''OK'''<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Compile ===<br />
# Compiler > Make<br />
# Compiler > Compile drupal_cron.c<br />
# Pick-up the finished program '''drupal_cron.exe''' in '''E:\lcc\projects\drupal_cron\lcc'''<br />
# Copy this file to "Uniform Server"<br />
<br />
'''''[[#top | Top]]'''''<br />
== Other files ==<br />
To recap I have assumed Drupal has been installed in the root folder www.<br />
<br />
=== Uniform Server ===<br />
The folder "Uuiform Server" contains the following files:<br />
<br />
*'''drupal_cron.exe'''<br />
<br />
You can directly run this file if you have set the defaults to meet your requirements.<br />
<br />
*'''drupal_cron.bat'''<br />
<br />
Alternatively you can start '''drupal_cron.exe''' using a batch file and pass parameters to it.<br />
For example create '''drupal_cron.bat''' and add this line:<br />
<br />
<pre><br />
start drupal_cron.exe 1 udrive\www\run_cron.php udrive\usr\local\php\php.exe<br />
</pre> <br />
The above would have no effect since the parameters passed are the defaults.<br />
<br />
If you want to change the delay to 1 hour and installed Drupal to sub-folder www\drupal the batch file now looks like this:<br />
<pre><br />
start drupal_cron.exe 6 udrive\www\drupal\run_cron.php <br />
</pre><br />
<br />
*'''stop_drupal_cron.bat'''<br />
<br />
If you want to manually stop drupal_cron.exe without relaying on turning your PC off create a batch file named '''stop_drupal_cron.bat''' and add the following line:<br />
<br />
<pre><br />
udrive\home\admin\program\pskill.exe drupal_cron.exe c<br />
</pre><br />
<br />
=== Drupal root folder ===<br />
Drupal's root folder (where file cron.php is located) must contain the following PHP script file.<br />
Create a new file '''run_cron.php''' and add the following line:<br />
<pre><br />
<? $dummy = file("http://localhost/cron.php"); ?><br />
</pre><br />
<br />
'''''[[#top | Top]]'''''<br />
== Alternative start and stop ==<br />
An alternative to using separate batch files to start and stop drupal_cron.exe is to add the appriate line of code to the end of Uniform Server's start and stop files.<br />
<br />
Note: At the bottom of each file you will see ''':END''' add the one liner below this.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Summary ==<br />
If you have never considered using Drupal now you have a good excuse to try it. You can install it on a separate Uniform Server fire it up and have a play. Add your new compiled script and check it works.<br />
<br />
Real point of the above is to provide real working code that can be compiled with lcc. At first sight it looks difficult to use, this invariably is down to knowing where to start. I hope this page and the pervious ones have pointed you in the right direction. I have noticed over the years fancy programming language come and go but C remains there in a prominent position.<br />
<br />
'''''[[#top | Top]]'''''<br />
----<br />
<br />
{|<br />
| [[Image:uc_small_logo.gif]] || [[User:Ric|Ric]]<br />
|}<br />
<br />
<br />
[[Category: UniCenter]]<br />
[[Category: Oily Rag]]</div>Ric