PageRenderTime 23ms CodeModel.GetById 12ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/upload/includes/cron/cron_init.php

http://torrentpier2.googlecode.com/
PHP | 67 lines | 46 code | 13 blank | 8 comment | 8 complexity | a7ad820fda9f9804fb61284a00401a18 MD5 | raw file
 1<?php
 2
 3if (!defined('BB_ROOT')) die(basename(__FILE__));
 4
 5//
 6// Functions
 7//
 8function cron_get_file_lock ()
 9{
10	$lock_obtained = false;
11
12	if (file_exists(CRON_ALLOWED))
13	{
14#		bb_log(date('H:i:s - ') . getmypid() .' -x-- FILE-LOCK try'. LOG_LF, CRON_LOG_DIR .'cron_check');
15
16		$lock_obtained = @rename(CRON_ALLOWED, CRON_RUNNING);
17	}
18	elseif (file_exists(CRON_RUNNING))
19	{
20		cron_release_deadlock();
21	}
22	elseif (!file_exists(CRON_ALLOWED) && !file_exists(CRON_RUNNING))
23	{
24		file_write('', CRON_ALLOWED);
25		$lock_obtained = @rename(CRON_ALLOWED, CRON_RUNNING);
26	}
27
28	return $lock_obtained;
29}
30
31function cron_track_running ($mode)
32{
33	@define('CRON_STARTMARK', TRIGGERS_DIR .'cron_started_at_'. date('Y-m-d_H-i-s') .'_by_pid_'. getmypid());
34
35	if ($mode == 'start')
36	{
37		cron_touch_lock_file(CRON_RUNNING);
38		file_write('', CRON_STARTMARK);
39	}
40	elseif ($mode == 'end')
41	{
42		@unlink(CRON_STARTMARK);
43	}
44}
45
46//
47// Run cron
48//
49if (cron_get_file_lock())
50{
51	ignore_user_abort(true);
52	register_shutdown_function('cron_release_file_lock');
53	register_shutdown_function('cron_enable_board');
54
55#	bb_log(date('H:i:s - ') . getmypid() .' --x- FILE-LOCK OBTAINED ###############'. LOG_LF, CRON_LOG_DIR .'cron_check');
56
57	cron_track_running('start');
58
59	require(CRON_DIR .'cron_check.php');
60
61	cron_track_running('end');
62}
63
64if (defined('IN_CRON'))
65{
66	bb_log(date('H:i:s - ') . getmypid() .' --x- ALL jobs FINISHED *************************************************'. LOG_LF, CRON_LOG_DIR .'cron_check');
67}