[ALPHA] Pseudocron

A place for MOD Authors to post and receive feedback on MODs still in development. No MODs within this forum should be used within a live environment! No new topics are allowed in this forum.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

IMPORTANT: MOD Development Forum rules

On February 1, 2009 this forum will be set to read only as part of retiring of phpBB2.
Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

[ALPHA] Pseudocron

Post by Xore » Wed Aug 13, 2003 7:01 am

Pseudocron

What is Pseudocron?

For those of you who are familiar, it's a phpbb approximation (no, don't groan, hear me out) to crontab

For those of you who aren't familiar, crontab is a program that allows you to automatically run scripts on a timely basis

Now, this creates a 'pseudo' crontab, which takes user-even driven actions (ie, clicking on any phpbb link) and runs the pseudocron if it's within the next time slot (this can be split up into several different sub-actions to reduce load on any one user click)

Why am i doing this? well, i'm developing a mod or two that could really use automated actions. (ie, giving out allowances with Cash Mod) I'm sure there are other mod authors who would like this functionality.

The nice thing, is that if you (or the people who run your mods) actually _have_ crontab, you can transfer the cron load off your forum users and onto the actual cron process via another file that i provide which is built to tie right into the crontab system.

So, i'm pretty sure that the pseudocron half of the functionality is working properly, but until my development server is back up, i won't be able to test the actual crontab half

SO, what i need is testers for pseudocron, if you're interested in helping develop this mod :-)

I hope you're interested.

As an example of it's working order, there is a value "crontest" in your config table. With pseudocron installed, it will increment every hour. You can view this from your adminCP

in summary:
  • Crontab approximation
  • allows automated forum actions/scripts
  • user-event driven
  • can be run by crontab (if you have it) for better results
  • platform for mods that require this functionality
Pseudocron 1.0.0 b2 files:
pseudocron100beta2.txt (installer)
sql_install.php (sql installer)
includes/pseudocron.php (main functionality)
admin/admin_cron.php and templates/subSilver/admin_cron.tpl (admin control panel)
phpbbcron.php (if you actually have crontab)

pseudocron100b2.zip (package)

Small fix to pc100b2 here
Last edited by Xore on Tue May 25, 2004 5:22 am, edited 9 times in total.
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

pseudocron100b2.txt

Post by Xore » Wed Aug 13, 2003 7:03 am

pseudocron100beta2.txt

Code: Select all

############################################################## 
## MOD Title: Pseudocron
## MOD Author: Xore < xore@azuriah.com > (Xore) http://forums.azuriah.com
## MOD Description: Cron Approximation for phpbb
## MOD Version: 1.0.0 beta 2
## 
## Installation Level: Easy
## Installation Time: 1 Minute 
## Files To Edit: common.php
##                language/lang_english/lang_admin.php
## Included Files: includes/pseudocron.php
##                 admin/admin_cron.php
##                 templates/subSilver/admin_cron.tpl
############################################################## 
## For Security Purposes, Please Check: http://www.phpbb.com/mods/ for the 
## latest version of this MOD. Downloading this MOD from other sites could cause malicious code 
## to enter into your phpBB Forum. As such, phpBB will not offer support for MOD's not offered 
## in our MOD-Database, located at: http://www.phpbb.com/mods/ 
############################################################## 
## Author Notes: We all love cron.
############################################################## 
## MOD History: 
## v1.0.0 First version release. no bugs, i hope :P
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
##############################################################
# 
# You need to run sql_install.php before or after completing these instructions
# (it needs to be run from your root phpbb directory)
# - 4 insert queries into your config table
# 
#-----[ COPY ]------------------------------------------ 
#
copy pseudocron.php to includes/pseudocron.php
copy admin_cron.php to admin/admin_cron.php
copy admin_cron.tpl to templates/subSilver/admin_cron.tpl

#
#-----[ OPEN ]------------------------------------------ 
#
common.php

# 
#-----[ FIND ]------------------------------------------ 
# 
?>

# 
#-----[ BEFORE, ADD ]------------------------------------------ 
#
include($phpbb_root_path . 'includes/pseudocron.'.$phpEx);

#
#-----[ OPEN ]------------------------------------------ 
#
language/lang_english/lang_admin.php

# 
#-----[ FIND ]------------------------------------------ 
# 
// That's all Folks!

# 
#-----[ BEFORE, ADD ]------------------------------------------ 
#
// Pseudocron functions
//
$lang['Pseudocron'] = 'Pseudocron';
$lang['Pseudocron_explain'] = 'The form below will allow you to manage your pseudocron settings.';
$lang['Enable_pseudocron'] = 'Enable Pseudocron';
$lang['Enable_pseudocron_explain'] = 'Enable this to allow your forum to handle automated MOD actions<br />Note: if you have crontab installed, you should disable this and use the phpbbcron.php crontab file';
$lang['Nextcron'] = 'Next cron action';
$lang['Nextcron_explain'] = 'When Pseudcron is set to activate next';
$lang['Crontest'] = 'Cron Test Value';
$lang['Pseudocron_config_updated'] = 'Pseudcron configuration updated';
$lang['Click_return_pseudocron_config'] = 'Click %sHere%s to return to Pseudocron Configuration';

//

# 
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------ 
# 
# EoM
Last edited by Xore on Wed Aug 13, 2003 4:47 pm, edited 1 time in total.
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

includes/pseudocron.php

Post by Xore » Wed Aug 13, 2003 7:05 am

includes/pseudocron.php

Code: Select all

<?php
/*************************************************************************** 
 *                              pseudocron.php 
 *                            -------------------
 *   begin                : Tuesday, Aug 12, 2003
 *   copyright            : (C) 2003 Xore
 *   email                : xore@azuriah.com
 *
 *   $Id: pseudocron.php,v 1.1.0.0 2003/08/13 09:47:37 Xore $
 *
 *
 ***************************************************************************/

/*************************************************************************** 
 *
 * Note: if you're a mod author and want to add stuff to this file in your
 *       mods, make sure your new content doesn't contain switch statements
 *       with a default clause, since i use the 'default:' in the main
 *       function as a unique line to specify entry points for queue
 *       additions.
 *
 ***************************************************************************/

if ( ($board_config['nextcron'] < $current_time) && $board_config['pseudocron'] )
{
	$current_time = time();
	ignore_user_abort();
	cronunit($board_config['cronstatus'],$current_time,"pseudocron");
}

function cron()
{
	ignore_user_abort();
	$current_time = time();
	$i = 0;
	while( !cronunit($i,$current_time,"cron") );
}

function cronunit(&$cronstatus,$current_time,$mode = "cron")
{
	$i = 0;
	switch ($cronstatus)
	{
		//
		// Cron test
		//
		case cron_next($i):
			if ( cron_test() )
			{
				if ( $mode == "pseudocron")
				{
					updatepseudocronstatus();
				}
				$cronstatus++;
			}
			return false;
			break;
		//
		// End Cron Test
		//
		default:
			if ( $mode == "pseudocron")
			{
				updatepseudocronstatus("reset");
			}
			$cronstatus = 0;
			return true;
			break;
	}
}

function cron_next(&$item)
{
	$item++;
	return ($item - 1);
}

function updatepseudocronstatus($value = "increment")
{
	global $db;

	switch ( $value )
	{
		case "increment":
			$sql = "UPDATE " . CONFIG_TABLE . "
					SET config_value = config_value + 1
					WHERE config_name = 'cronstatus'";
			if ( !($db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Error updating cron status', '', __LINE__, __FILE__, $sql);
			}
			break;
		case "reset":
			$sql = "UPDATE " . CONFIG_TABLE . "
					SET config_value = 0
					WHERE config_name = 'cronstatus'";
			if ( !($db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Error updating cron status', '', __LINE__, __FILE__, $sql);
			}
			$sql = "UPDATE " . CONFIG_TABLE . "
					SET config_value = config_value + 3600
					WHERE config_name = 'nextcron'";
			if ( !($db->sql_query($sql)) )
			{
				message_die(GENERAL_ERROR, 'Error updating cron status', '', __LINE__, __FILE__, $sql);
			}
			break;
	}
}

function cron_test()
{
	global $db;
	$sql = "UPDATE " . CONFIG_TABLE . "
			SET config_value = config_value + 1
			WHERE config_name = 'crontest'";
	if ( !($db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Error updating cron test', '', __LINE__, __FILE__, $sql);
	}
	return true;
}

?>
Last edited by Xore on Wed Aug 13, 2003 4:45 pm, edited 1 time in total.
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

admin/admin_cron.php and templates/subSilver/admin_cron.tpl

Post by Xore » Wed Aug 13, 2003 7:06 am

admin/admin_cron.php

Code: Select all

<?php
/***************************************************************************
 *                              admin_cron.php
 *                            -------------------
 *   begin                : Thursday, Apr 17, 2003
 *   copyright            : (C) 2003 Xore
 *   email                : xore@azuriah.com
 *
 *   $Id: admin_cron.php,v 1.0.0.0 2003/08/12 20:26:52 Xore $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

define('IN_PHPBB', 1);

if ( !empty($setmodules) )
{
	$file = basename(__FILE__);
	$module['Pseudocron']['Cron Configuration'] = "$file?mode=config";
	return;
}

//
// Let's set the root dir for phpBB
//
$phpbb_root_path = "./../";
require($phpbb_root_path . 'extension.inc');
require('./pagestart.' . $phpEx);
include($phpbb_root_path . 'includes/functions_selects.'.$phpEx);

$new = array();
//
// Pull all config data
//
$sql = "SELECT *
	FROM " . CONFIG_TABLE;
if ( !$result = $db->sql_query($sql) )
{
	message_die(CRITICAL_ERROR, "Could not query config information", "", __LINE__, __FILE__, $sql);
}
$allowed_array = array( 'pseudocron' => true);
while ( $row = $db->sql_fetchrow($result) )
{
	$config_name = $row['config_name'];
	$config_value = $row['config_value'];
	$default_config[$config_name] = $config_value;
	
	$new[$config_name] = $default_config[$config_name];

	if ( $allowed_array[$config_name] &&
		 isset($HTTP_POST_VARS['submit']) &&
		 isset($HTTP_POST_VARS[$config_name]) )
	{
		$new[$config_name] = stripslashes($HTTP_POST_VARS[$config_name]);
		$sql = "UPDATE " . CONFIG_TABLE . " SET
			config_value = '" . addslashes($new[$config_name]) . "'
			WHERE config_name = '$config_name'";
		if ( !$db->sql_query($sql) )
		{
			message_die(GENERAL_ERROR, "Failed to update Cron configuration for $config_name", "", __LINE__, __FILE__, $sql);
		}
	}
}
	
if ( isset($HTTP_POST_VARS['submit']) )
{
	$message = $lang['Pseudocron_config_updated'] . "<br /><br />" . sprintf($lang['Click_return_pseudocron_config'], "<a href=\"" . append_sid("admin_cron.$phpEx") . "\">", "</a>") . "<br /><br />" . sprintf($lang['Click_return_admin_index'], "<a href=\"" . append_sid("index.$phpEx?pane=right") . "\">", "</a>");

	message_die(GENERAL_MESSAGE, $message);
}

$enable_pseudocron_yes = ( $new['pseudocron'] ) ? "checked=\"checked\"" : "";
$enable_pseudocron_no = ( !$new['pseudocron'] ) ? "checked=\"checked\"" : "";

// nextcron stuff
$nc = intval($new['nextcron']) - time();
$nc_string = "";

$nc_days = floor($nc / 86400 );
$nc = $nc - ($nc_days * 86400);

$nc_hours = floor($nc / 3600 );
$nc = $nc - ($nc_hours * 3600);

$nc_minutes = floor($nc / 60 );
$nc_seconds = $nc - ($nc_minutes * 60);

$nc_string = (($nc_seconds > 9)?("$nc_seconds"):("0$nc_seconds"));
$nc_string = (($nc_minutes > 9)?("$nc_minutes"):("0$nc_minutes")) . ":" . $nc_string;
if($nc_days)
{
	$nc_string = $nc_days . ", " . $nc_hours . ":" . $nc_string;
}
else if($nc_hours)
{
	$nc_string = $nc_hours . ":" . $nc_string;
}


$template->set_filenames(array(
	"body" => "admin/admin_cron.tpl")
);

$template->assign_vars(array(
	"S_PSEUDOCRON_ACTION" => append_sid("admin_cron.$phpEx"),

	"L_YES" => $lang['Yes'],
	"L_NO" => $lang['No'],
	"L_PSEUDOCRON_TITLE" => $lang['Pseudocron'],
	"L_PSEUDOCRON_EXPLAIN" => $lang['Pseudocron_explain'],

	"L_ENABLE_PSEUDOCRON" => $lang['Enable_pseudocron'],
	"L_ENABLE_PSEUDOCRON_EXPLAIN" => $lang['Enable_pseudocron_explain'],

	"L_NEXTCRON" => $lang['Nextcron'],
	"L_NEXTCRON_EXPLAIN" => $lang['Nextcron_explain'],
	"L_CRONTEST" => $lang['Crontest'],

	"L_SUBMIT" => $lang['Submit'], 
	"L_RESET" => $lang['Reset'],
	
	"ENABLE_PSEUDOCRON_YES" => $enable_pseudocron_yes,
	"ENABLE_PSEUDOCRON_NO" => $enable_pseudocron_no,
	
	"NEXTCRON_NUMBER" => $new['nextcron'],
	"NEXTCRON_MINUTES" => $nc_string,
	"CRONTEST" => $new['crontest']
	)
);

$template->pparse("body");

include('./page_footer_admin.'.$phpEx);

?>
templates/subSilver/admin_cron.tpl

Code: Select all

<h1>{L_PSEUDOCRON_TITLE}</h1>

<p>{L_PSEUDOCRON_EXPLAIN}</p>

<form action="{S_PSEUDOCRON_ACTION}" method="post"><input type="hidden" name="set" value="general"><table width="99%" cellpadding="4" cellspacing="1" border="0" align="center" class="forumline">
	<tr>
	  <th class="thHead" colspan="2">{L_PSEUDOCRON_TITLE}</th>
	</tr>

	<tr>
		<td class="row1">{L_ENABLE_PSEUDOCRON}<br /><span class="gensmall">{L_ENABLE_PSEUDOCRON_EXPLAIN}</span></td>
		<td class="row2"><input type="radio" name="pseudocron" value="1" {ENABLE_PSEUDOCRON_YES} /> {L_YES}&&<input type="radio" name="pseudocron" value="0" {ENABLE_PSEUDOCRON_NO} /> {L_NO}</td>
	</tr>
	<tr>
		<td class="row1">{L_NEXTCRON}<br /><span class="gensmall">{L_NEXTCRON_EXPLAIN}</span></td>
		<td class="row2">{NEXTCRON_NUMBER}<br />{NEXTCRON_MINUTES}</td>
	</tr>
	<tr>
		<td class="row1">{L_CRONTEST}</td>
		<td class="row2">{CRONTEST}</td>
	</tr>
	<tr>
		<td class="catBottom" colspan="2" align="center">{S_HIDDEN_FIELDS}<input type="submit" name="submit" value="{L_SUBMIT}" class="mainoption" />&&<input type="reset" value="{L_RESET}" class="liteoption" />
		</td>
	</tr>
</table></form>

<br clear="all" />
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

phpbbcron.php

Post by Xore » Wed Aug 13, 2003 7:08 am

phpbbcron.php

Code: Select all

#!/usr/local/bin/php
<?php
/*************************************************************************** 
 *                              phpbbcron.php 
 *                            -------------------
 *   begin                : Tuesday, Aug 12, 2003
 *   copyright            : (C) 2003 Xore
 *   email                : xore@azuriah.com
 *
 *   $Id: phpbbcron.php,v 1.0.0.0 2003/08/12 19:07:09 Xore $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 * Note: you WILL have to update your php path (#!/usr/local/bin/php)
 *       if it is not the same as the one i provided (See the top of the script)
 * Also: it is necessary that you change the value of $phpbb_root_path
 *       so that it is valid relative to the location of this file.
 * Also: this file should not be placed in a directory that is accessible from
 *       the web.
 * Disclaimer: If you leave this file in a directory that is accessible from the
 *       web, i am in no way responsible for what happens because of it.
 *
 ***************************************************************************/

define('IN_PHPBB', true);
$phpbb_root_path = './';
include($phpbb_root_path . 'extension.inc');
include($phpbb_root_path . 'common.'.$phpEx);

cron();

?>
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

sql_install.php

Post by Xore » Wed Aug 13, 2003 7:09 am

sql_install.php

Code: Select all

<?php 
/*************************************************************************** 
 *                              sql_install.php 
 *                            -------------------
 *   begin                : Thursday, Apr 17, 2003
 *   copyright            : (C) 2003 Xore
 *   email                : xore@azuriah.com
 *
 *   $Id: sql_install.php,v 1.3.0.0 2003/08/12 18:23:09 Xore $
 *
 *
 ***************************************************************************/

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

define('IN_PHPBB', true); 
$phpbb_root_path = './'; 
include($phpbb_root_path . 'extension.inc'); 
include($phpbb_root_path . 'common.'.$phpEx); 

// 
// Start session management 
// 
$userdata = session_pagestart($user_ip, PAGE_INDEX); 
init_userprefs($userdata); 
// 
// End session management 
// 

if( !$userdata['session_logged_in'] ) 
{ 
	redirect(append_sid("login.$phpEx?redirect=mysql_install.$phpEx", true));
} 

if( $userdata['user_level'] != ADMIN ) 
{ 
   message_die(GENERAL_MESSAGE, 'You are not authorised to access this page'); 
} 

$page_title = 'Installing Cash Mod v 2.0.0 Tables, Updating Configuration settings'; 
include($phpbb_root_path . 'includes/page_header.'.$phpEx); 

?>
<table width="100%" cellspacing="1" cellpadding="2" border="0" class="forumline">
  <tr>
    <th class="thHead">Updating the database</th>
  </tr>
  <tr>
    <td>
      <span class="genmed">
        <ul type="circle">

<?php

$current_time = time();

// no sql layer required, since we're just inserting

	$sql = array( 
		"INSERT INTO {$table_prefix}config (config_name, config_value) VALUES ('nextcron','" . $current_time . "');", 
		"INSERT INTO {$table_prefix}config (config_name, config_value) VALUES ('pseudocron','1');", 
		"INSERT INTO {$table_prefix}config (config_name, config_value) VALUES ('cronstatus','0');", 
		"INSERT INTO {$table_prefix}config (config_name, config_value) VALUES ('crontest','0');", 
	);

foreach ( $sql AS $query ) 
{ 
   if ( !($result = $db->sql_query($query)) )
   { 
      $error = $db->sql_error();
      print('<li>' . nl2br($query) . '<br /> +++ <font color="#FF0000"><b>Error:</b></font> ' . $error['message'] . '</li><br />');
   } 
   else 
   { 
      print('<li>' . nl2br($query) . '<br /> +++ <font color="#00AA00"><b>Successfull</b></font></li><br />');
   } 
}

$forum_url = append_sid("index.$phpEx");
$phpbb_url = "http://www.phpbb.com/phpBB/viewtopic.php?t=122654";

?>
        </ul>
      </span>
    </td>
  </tr>
  <tr>
    <td class="catBottom" height="28">&</td>
  </tr>
  <tr>
    <td class="catBottom" colspan="2" align="center">Finished</td>
  </tr>
</table>

<br />
<br />

<table width="100%" cellspacing="1" cellpadding="2" border="0" class="forumline">
  <tr>
    <th class="thHead">SQL Installation complete</th>
  </tr>
  <tr>
    <td>
      <span class="genmed">Please delete this file (sql_install.php).<br />
      Also, be sure to complete the install instructions in SiteDescription110.txt (if you haven't already)<br />
      If you have any problems, please visit <a href="<?php echo($phpbb_url); ?>" target="_new">phpbb.com (SiteDescription Support Thread)</a> and ask for help.</span>
    </td>
  </tr>
  <tr>
    <td class="catBottom" height="28" align="center">
      <span class="genmed"><a href="<?php echo($forum_url); ?>">Click Here to return to your forum.</a>
      </span>
    </td>
  </tr>
</table>
<?php

include($phpbb_root_path . 'includes/page_tail.'.$phpEx); 

?>
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

chickeneater
Registered User
Posts: 400
Joined: Wed May 21, 2003 3:16 pm

Post by chickeneater » Wed Aug 13, 2003 7:09 am

what is pseudocron?

does it synchronize time and all?

User avatar
Draegonis
Former Team Member
Posts: 3950
Joined: Mon Apr 22, 2002 3:12 pm
Location: Kµlt øƒ Ø
Contact:

Post by Draegonis » Wed Aug 13, 2003 7:27 am

chickeneater wrote: what is pseudocron?

does it synchronize time and all?

See first post.
Last edited by Draegonis on Wed Aug 13, 2003 7:28 am, edited 1 time in total.

Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

Post by Xore » Wed Aug 13, 2003 7:28 am

chickeneater wrote: what is pseudocron?

does it synchronize time and all?

Xore wrote: What is Pseudocron?
For those of you who are familiar, it's a phpbb approximation (no, don't groan, hear me out) to crontab

For those of you who aren't familiar, crontab is a program that allows you to automatically run scripts on a timely basis

Now, this creates a 'pseudo' crontab, which takes user-even driven actions (ie, clicking on any phpbb link) and runs the pseudocron if it's within the next time slot (this can be split up into several different sub-actions to reduce load on any one user click)

Why am i doing this? well, i'm developing a mod or two that could really use automated actions. (ie, giving out allowances with Cash Mod) I'm sure there are other mod authors who would like this functionality.

The nice thing, is that if you (or the people who run your mods) actually _have_ crontab, you can transfer the cron load off your forum users and onto the actual cron process via another file that i provide which is built to tie right into the crontab system.
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

Post by Xore » Wed Aug 13, 2003 7:42 am

basically, in summary ( i added this to the first post also )
  • Crontab approximation
  • allows automated forum actions/scripts
  • user-event driven
  • can be run by crontab (if you have it) for better results
  • platform for mods that require this functionality
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

User avatar
Ptirhiik
Registered User
Posts: 7411
Joined: Mon Jan 06, 2003 10:36 pm
Contact:

Post by Ptirhiik » Wed Aug 13, 2003 7:53 am

You should restrict this to only the mods and admin, and move the launch to page_tail . Note that a process can be interrupted by closing the browser or going on to another page.

Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

Post by Xore » Wed Aug 13, 2003 8:21 am

Ptirhiik - RPGnet-fr wrote: You should restrict this to only the mods and admin, and move the launch to page_tail . Note that a process can be interrupted by closing the browser or going on to another page.


Good points... i chose to make it this way for a couple reasons...

I'll see if i can address them...

I wanted to ensure that this gets called from every page in phpbb, so that the load gets distributed as evenly as possible, so i chose to include it into common.php. perhaps page_tail is also a good solution, i don't know, i haven't looked into it.

If there's only 1 admin, and he/she decides to go on holiday, then this becomes a problem, so it needed to be something that got activated on all users.

The process interruption thing is something that spent a lot of time thinking about. ( i hope i won't bore people with my thought process )...

so, basically, yes, a user can terminate the process, but there are a few good reasons why they wouldn't...

First, the cron is distributed. I created a little cron queue. (see function cronunit in includes/pseudocron.php --> this is where cron items get added ) Only one item gets run per user.

In addition, each item can be similarly split up by the author of the called function into smaller jobs, so that if the function returns false for each of the smaller jobs, and then returns true on the last, you can run lots of smaller-sized jobs.

If you make these smaller jobs small enough, then the time it takes to execute them is unnoticeable for each user, and they won't even have the opportunity to change links or stop the page.

Now, in the unlikely case that they _do_ manage this, then the worst case possibility is that they terminate between the cron mini-process update, and the the cronstatus update. This would mean that the the pseudocron doesn't realize the update has occurred, and just runs the same code again on the next page load. thus... worst case... one of the mini-updates gets run twice.

Is this something to be worried about? that's up to the mod author, and the owner of the forum who runs the mod. I can't think of any mod where a small portion of an update getting run twice would be a serious problem.


But i thank you for your interested and pointing these things out, i've thought about them quite a bit. Maybe you and other people will see why i made the design decisions i made for this mod. Likewise, if more mod authors suggest that moving the include into page_tail is a good idea, then i will consider this, as well as making options for this to be only activated on admins and mods.
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

netclectic
Former Team Member
Posts: 4439
Joined: Wed Mar 13, 2002 3:08 pm
Location: Omnipresent
Contact:

Post by netclectic » Wed Aug 13, 2003 9:24 am

Sounds very interesting, i'll definitly be taking a look at this.

Could you possibly provide a single .zip of the files?

Can you explain the process a mod author would go through to get some jobs into the 'cron queue'?


Wineknow had something like this for his NewsSync mod, is this related in anyway?
Defend the game:
Image

Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

Post by Xore » Wed Aug 13, 2003 9:44 am

zip is available here here

give me a sec while i get the info together on queue inserts
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

Xore
Former Team Member
Posts: 2594
Joined: Wed Apr 16, 2003 7:42 pm
Location: the wastelands between insomnia and clairvoyance
Contact:

Post by Xore » Wed Aug 13, 2003 9:56 am

Ok, here it is

Code: Select all

function cronunit(&$cronstatus,$current_time,$mode = "cron") 
{ 
   $i = 0; 
   switch ($cronstatus) 
   { 
      // 
      // Cron test 
      // 
      case cron_next($i): 
         if ( cron_test() ) 
         { 
            if ( $mode == "pseudocron") 
            { 
               updatepseudocronstatus(); 
            } 
            $cronstatus++; 
         } 
         return false; 
         break; 
      // 
      // End Cron Test 
      // 
      default: 
         if ( $mode == "pseudocron") 
         { 
            updatepseudocronstatus("reset"); 
         } 
         $cronstatus = 0; 
         return true; 
         break; 
   } 
} 
this is the main cron queue

i wanted to make it really easy for mod authors to be able to insert into the queue, (or rather... make it easy for them to get users to insert into the queue...).
with a normal select, this isn't possible... which is why i created the cron_next() function:

Code: Select all

function cron_next(&$item) 
{ 
   $item++; 
   return ($item - 1); 
}
what this basically does, is state that (starting from 0) each time the function is evaluated, it returns an incremented integer. So each case is unique. That means that mod authors don't need to reserve concrete spots on the queue, and that missing spots won't cause the incrementer to look for a case that isn't in existance.

Net result: yay!

so, how do you insert?

#
#-----[ FIND ]--------------------
#

Code: Select all

      default: 
if it's a one-time function (ie, once an hour, simple function)
#
#-----[ Before, add ]--------------------
#

Code: Select all

      // 
      // My cron 
      // 
      case cron_next($i): 
         my_cron_func();         { 
         if ( $mode == "pseudocron") 
         { 
            updatepseudocronstatus(); 
         } 
         $cronstatus++; 
         return false; 
         break; 
      // 
      // End my cron
      // 
If you want to have it possibly run multiple times (mini sub-crons)

Code: Select all

      // 
      // My cron 
      // 
      case cron_next($i): 
         if( my_cron_func() )
         { 
            if ( $mode == "pseudocron") 
            { 
               updatepseudocronstatus(); 
            } 
         }
         $cronstatus++; 
         return false; 
         break; 
      // 
      // End my cron
      // 
where my_cron_func() returns false while it's not done, and true on it's last occurance

if this needs more explanation (ie, why make the system so complicated, what's the point in having mutliple executions, etc), please let me know
Wineknow had something like this for his NewsSync mod, is this related in anyway?


I developed this code on my own, i wasn't aware anything of this nature existed, or if NewsSync uses a similar system at all or not


Edit: (Before, add, not After, add)
Camels in the Mist
<noise type="random" source="camel" />
Cash Mod

Post Reply

Return to “[2.0.x] MODs in Development”