How to add a CRON job?

Discussion forum for MOD Writers regarding MOD Development.
Locked
User avatar
Techit
Registered User
Posts: 174
Joined: Sun Jun 08, 2003 7:54 pm
Location: Durham - England
Contact:

How to add a CRON job?

Post 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
Name: James Simpson
Websites: Swift Owners :: James Simpson
User avatar
tbackoff
Former Team Member
Posts: 7064
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: How to add a CRON job?

Post 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.
Flying is the second best thrill to cheerleaders; being caught is the first.
User avatar
Techit
Registered User
Posts: 174
Joined: Sun Jun 08, 2003 7:54 pm
Location: Durham - England
Contact:

Re: How to add a CRON job?

Post 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?
Name: James Simpson
Websites: Swift Owners :: James Simpson
User avatar
Techit
Registered User
Posts: 174
Joined: Sun Jun 08, 2003 7:54 pm
Location: Durham - England
Contact:

Re: How to add a CRON job?

Post 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?
Name: James Simpson
Websites: Swift Owners :: James Simpson
User avatar
RMcGirr83
Former Team Member
Posts: 21595
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: How to add a CRON job?

Post 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
Appreciate the extensions/mods/support then buy me a beerImage
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
User avatar
tbackoff
Former Team Member
Posts: 7064
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: How to add a CRON job?

Post 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.
Flying is the second best thrill to cheerleaders; being caught is the first.
User avatar
Techit
Registered User
Posts: 174
Joined: Sun Jun 08, 2003 7:54 pm
Location: Durham - England
Contact:

Re: How to add a CRON job?

Post 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?
Name: James Simpson
Websites: Swift Owners :: James Simpson
User avatar
tbackoff
Former Team Member
Posts: 7064
Joined: Thu Jun 04, 2009 1:41 am
Location: cheerleading practice
Name: Tabitha Backoff

Re: How to add a CRON job?

Post 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.
Flying is the second best thrill to cheerleaders; being caught is the first.
Locked

Return to “[3.0.x] MOD Writers Discussion”