0

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';

PHP: List of Resource Types - Manual

PHP: Persistent Database Connections - Manual

Diblo Dk
  • 585
  • 10
  • 26
  • I read it now about 5 times, and I am not sure what you are trying to achieve – dan-lee Mar 24 '12 at 12:53
  • Normally you close a persistent connections when you are finished with use it. But because I use an asynchronous persistent connections without closing it the script continues to loop every minute without a parent. (Cron.ph calls Slepper.php calls Cron.ph 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 edit the topic with a example. – Diblo Dk Mar 24 '12 at 13:36
  • Well at first, for persistent connections you'd need pfsockopen(), that is the right one. And why don't you give it a reasonable timeout, so it will automatically end? – dan-lee Mar 24 '12 at 13:58
  • You are right, but scripts should be able to continue to loop without a parent. I have another way to stop it, but to save codes I would like to be able to stop it with fclose(); – Diblo Dk Mar 24 '12 at 14:06
  • So there is not a major problem. – Diblo Dk Mar 24 '12 at 14:08
  • but scripts continue to loop without a parent. ***** – Diblo Dk Mar 24 '12 at 14:15
  • If you open your connection again with `pfsockopen()`, with the exact same parameters, you'll get the same connection again and you can close it with `fclose()`. I am not sure if this covers all systems but as far as I know this keeps true. Check it out with dumping the resource where you can get the resource id – dan-lee Mar 24 '12 at 14:15
  • Sorry first time I misunderstood you a bit. But how to save the 'file pointer' or give it a name and call it back - etc – Diblo Dk Mar 24 '12 at 14:23
  • Ooh I see, pfsockopen is a persistent connection and fsockopen die with the parent, stream_set_blocking is only use to if the scripts wait or not. But can I store a 'file pointer' to a file? – Diblo Dk Mar 24 '12 at 17:14
  • 1
    You cannot store a file with just a pointer to that socket connection, what do you think this file would contain as plain text? As I said you need to recreate that connection and then you are able close it. – dan-lee Mar 25 '12 at 03:14

0 Answers0