Table Design

RecordId

Auto Increment
int (unisigned)
Primary Key

SettingName

varchar

SettingValue

text

Is A Setting Enabled?


	//----- CHECK IF UPLOADS ARE ENABLED CURRENTLY -----
	$QueryString = @mysql_query("SELECT SettingValue FROM tblApiSettings WHERE SettingName = 'ArchiveDataUploadsEnabled'");
	$result = @mysql_fetch_array($QueryString);
	$UploadsEnabled = $result['SettingValue'];
	$UploadsEnabled = filter_var($UploadsEnabled, FILTER_VALIDATE_BOOLEAN);
	if (!$UploadsEnabled)
		die("Quitting - Uploads import is currently disabled");

Is A Background Task Already Being Done (With timeout check)


	//----- CHECK IF THIS TASK IS ALREADY RUNNING CURRENTLY -----
	//(Started from an earlier CRON call)
	$QueryString = @mysql_query("SELECT SettingValue FROM tblApiSettings WHERE SettingName = 'ArchiveDataUploadsImportActive'");
	$result = @mysql_fetch_array($QueryString);
	$ArchiveDataUploadsImportActive = $result['SettingValue'];
	
	if ($ArchiveDataUploadsImportActive != "0")
	{
		$end = strtotime(date("Y-m-d H:i:s"));		//Now
		$start = strtotime($ArchiveDataUploadsImportActive);
		if ($start <= $end)
		{
			$no_of_minutes = ceil(abs($end - $start) / 60);   //ceil rounds the amount of days up to the next full day. Use floor if you want to get the amount of full days between the two dates.
			if ($no_of_minutes < 120)			//<<<Timeout, if longer than this many minutes has passed ignore the setting as old task must have died for some reason without clearing this
			{
				//----- UPLOADS IMPORT IS ALREADY ACTIVE -----
				die("Quitting - Uploads import is already active (started: " .$ArchiveDataUploadsImportActive . ")");
			}
		}
	}

	//----- FLAG THAT WE ARE ACTIVE CARRYING OUT IMPORT -----
	//(Stop other cron calls also doing this until we quit.  We use a timestamp to allow them to detect if something has gone wrong and too much time has passed)
	$TimeNow = date("Y-m-d H:i:s");
	$result = @mysql_query("UPDATE tblApiSettings SET
														SettingValue = '$TimeNow'
													WHERE SettingName = 'ArchiveDataUploadsImportActive'
														");


	//----- FLAG THAT IMPORT IS COMPLETE -----
	//(Remove block on other cron calls doing this task)
	$TimeNow = date("Y-m-d H:i:s");
	$result = @mysql_query("UPDATE tblApiSettings SET
														SettingValue = '0'
													WHERE SettingName = 'ArchiveDataUploadsImportActive'
														");

 

 

Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.

Comments

Your email address will not be published.