Page 1 of 1

How to add a CRON job?

Posted: Mon Mar 29, 2010 1:10 pm
by Techit
Hi All,

I'm working on a mod at the moment, and I need to add a cron job to check users expire dates and remove if over the date.

Now looking in CRON.php they all look as if they are to be run from the acp? (all in their own case's)

Now i know in overall_footer it has the {RUN_CRON_TASK}, but what does it run exactly? does it run the whole cron.php file?

So if i want to add a new cron task, do i just create a new case for my cron, if so, how do i call it to work as the forum prune works (when a user visits the site, it runs the cron and prunes the forum?)

Cheers

James

Re: How to add a CRON job?

Posted: Tue Mar 30, 2010 4:35 am
by tbackoff
RUN_CRON_TASK is defined in includes/functions.php. It appends the cron type, which is determined a few lines up, and then the appropriate case is called in cron.php. Hope that helps.

Re: How to add a CRON job?

Posted: Tue Apr 20, 2010 8:12 pm
by Techit
Right i need a bit of help with a cron job ive made...

Ive just put this into cron.php

Code: Select all

	//Membership Cron - Check if still in date
	case 'membership_check':
	if (!function_exists('membership_check'))
		{
		$sql = 'DELETE
			FROM ' . USER_GROUP_TABLE . '
			WHERE expire > 0 AND expire <= ' . time();
		$db->sql_query($sql);
		}

		if ($use_shutdown_function)
		{
			register_shutdown_function('membership_check');
		}
		else
		{
			membership_check();
		}
	break;
I dunno if it works right (well the sql code works fine, but not sure if the cron job works) but i do get an error message in the acp
Error while creating image
ยป Error in /cron.php on line 87: register_shutdown_function() [function.register-shutdown-function]: Invalid shutdown callback 'membership_check' passed
Can anyone shed some light on the error message? I take i need to add the function membership_check() somewhere? but where?

Re: How to add a CRON job?

Posted: Tue Apr 20, 2010 8:16 pm
by Techit
In fact, do I even need to use the shutdown function? Im not even 100% sure what it does, is it to stop the function if its already running?

Re: How to add a CRON job?

Posted: Tue Apr 20, 2010 10:43 pm
by RMcGirr83
Techit wrote:In fact, do I even need to use the shutdown function? Im not even 100% sure what it does, is it to stop the function if its already running?
PHP register_shutdown_function

Re: How to add a CRON job?

Posted: Wed Apr 21, 2010 12:11 am
by tbackoff
Techit wrote:Right i need a bit of help with a cron job ive made...

Ive just put this into cron.php
You need to add an "esleif" statement to functions.php to set $cron_type to 'membership_check'. Look around line 4359.

Re: How to add a CRON job?

Posted: Sun Aug 08, 2010 12:57 am
by Techit
Right sorry for the late response

This is the cron part of the functions.php

Code: Select all

// Call cron job?
	if ($call_cron)
	{
		$cron_type = '';

		if ($time_now - $config['queue_interval'] > $config['last_queue_run'] && !defined('IN_ADMIN') && file_exists($phpbb_root_path . 'cache/queue.' . $phpEx))
		{
			// Process email queue
			$cron_type = 'queue';
		}
		else if (method_exists($cache, 'tidy') && $time_now - $config['cache_gc'] > $config['cache_last_gc'])
		{
			// Tidy the cache
			$cron_type = 'tidy_cache';
		}
		else if ($config['warnings_expire_days'] && ($time_now - $config['warnings_gc'] > $config['warnings_last_gc']))
		{
			$cron_type = 'tidy_warnings';
		}
		else if ($time_now - $config['database_gc'] > $config['database_last_gc'])
		{
			// Tidy the database
			$cron_type = 'tidy_database';
		}
		else if ($time_now - $config['search_gc'] > $config['search_last_gc'])
		{
			// Tidy the search
			$cron_type = 'tidy_search';
		}
		else if ($time_now - $config['session_gc'] > $config['session_last_gc'])
		{
			$cron_type = 'tidy_sessions';
		}

		if ($cron_type)
		{
			$template->assign_var('RUN_CRON_TASK', '<img src="' . append_sid($phpbb_root_path . 'cron.' . $phpEx, 'cron_type=' . $cron_type) . '" width="1" height="1" alt="cron" />');
		}
	}

	$template->display('body');

	garbage_collection();
	exit_handler();
}
Right, now where and what should i add into here to get my cron working?

Re: How to add a CRON job?

Posted: Sun Aug 08, 2010 4:34 am
by tbackoff
FIND:

Code: Select all

    else if ($time_now - $config['session_gc'] > $config['session_last_gc'])
    {
        $cron_type = 'tidy_sessions';
    }
AFTER, ADD:

Code: Select all

    else if (some_condition_here)
    {
        $cron_type = 'membership_check';
    }
Just add whatever condition you want.