I made a cron file that calls different jobs asynchronous and ends up with calling the sleeper asynchronous. The sleeper sleeps for a 1 minute and then call the cron scripts asynchronous, etc.
But I want to rebuild the system to store the last 'file pointer' in a file and use it later to stop the process. Is that something that can be made?
Code sample:
sleeper.php
sleep(60);
call($_SERVER['HTTP_HOST'], dirname($_SERVER['REQUEST_URI']).'/cron.php', ( DEBUG ? 'write_log' : '' ) );
cron.php
foreach ($jobs as $job)
{
$job = parse_url($job);
call(dispatch_generic_job($job['port'], $job['path'], $job['port'], ( LOG_JOBS ? 'write_job' : '' ) );
}
call($_SERVER['HTTP_HOST'], dirname($_SERVER['REQUEST_URI']).'/sleeper.php', ( DEBUG ? 'write_log' : '' ) );
object.php:
function call($server, $url, $port = 80, $log)
{
@set_time_limit(0);
$timeout = 10;
$rw_timeout = 86400;
$error_number = '';
$error_string = '';
$con = @fsockopen($server, $port, $error_number, $error_string, $timeout);
if (!$con)
{
$log($error_string.' ('.$error_number.') Ref: '.$server.':'.$port.$url);
return false;
}
$qry = 'GET '.$url.' HTTP/1.1'."\r\n";
$qry .= 'Host: '.$server."\r\n";
$qry .= 'User-Agent: test'."\r\n";
$qry .= 'Connection: Close'."\r\n\r\n";
stream_set_blocking($con, false);
stream_set_timeout($con, $rw_timeout);
fwrite($con, $qry);
if ( $log )
{
while ( !feof($con) )
{
$line = fgets($con, 128);
if ( !empty($line) )
{
$log('Status: '.rtrim($line).' Ref: '.$server.':'.$port.$url);
break;
}
}
}
return True;
}
*UPDATE*** Normally you close a persistent connections when you are finished with use it. But because I use an asynchronous (stream_set_blocking) persistent connections (fsockopen) without closing so the script can continues to loop every minute without a parent. (Cron.php calls Slepper.php calls Cron.php etc.)
I will like to rebuild my scripts to store the last 'file pointer' so I can use it later to close the persistent connections.
I have read a lot but have not found a way. PHP: socket_import_stream - Manual
Example:
object.php
function call($server, $url, $port = 80, $log)
{
......
$con = @fsockopen($server, $port, $error_number, $error_string, $timeout);
if (!$con)
{
$log($error_string.' ('.$error_number.') Ref: '.$server.':'.$port.$url);
return false;
}
file_put_contents(__DIR__.'/steam', $con); // Obviously can not be performed
......
}
stop.php
$lats_con = file_get_contents(__DIR__.'/steam');
fclose($lats_con);
echo 'Cron is stopped';