<?php
$c = 0;
function check($lid){
	global $c;
	global $db;
	$c++;
	$in_progress = 0;
	if($c<900)
	{
		$query = "SELECT `in_progress` FROM `tms_web_tasks` WHERE `id` = ".$lid.";";
		$stmt = $db->prepare($query);
		$stmt->execute();
		$stmt->bind_result($in_progress);
		$stmt->fetch();
		$stmt->close();
		
		if( $in_progress == 0 )
		{
			sleep(1); check($lid);
		}elseif($in_progress == -1)
		{ 
			$query = "SELECT `result_message` FROM `tms_web_tasks` WHERE `id` = ".$lid.";";
			writeToLogFile( ' -> '.date('Y-m-d H:i:s').' '.$query );
			$stmt = $db->prepare($query);
			$stmt->execute();
			$stmt->bind_result($result_message);
			$stmt->fetch();
			$stmt->close();
			die($result_message);
		}elseif($in_progress == 1)
		{
			//return $in_progress;
			//echo true; exit;
			die('true');
		}else
		{
			die('true');
		}
		}else
		{
		
		$query = "UPDATE `tms_web_tasks` SET `in_progress`=-1 WHERE `id` = ".$lid.";";
		$stmt = $db->prepare($query);
		$stmt->execute();
		$stmt->close();
		
		return false;
		}
		
}
?>
