Exemple #1
0
        $this->log(__CLASS__, 'Initialise processor');
        srand(rand(1, 1000000000) * microtime(true));
    }
    /**
     * Process a job, just set the result to a message indicating when the job
     * was processed.
     *
     * @param Naph\PTask\Job $job
     */
    public function process(Naph\PTask\Job $job)
    {
        $result = 'Started at ' . date('H:i:s');
        $sleep = rand(1, 3);
        $this->log(__CLASS__, "Processing job id " . $job->getParam('id'));
        $this->log(__CLASS__, "Sleeping for {$sleep} seconds");
        sleep($sleep);
        $result .= ', and finished at ' . date('H:i:s');
        $job->setResult($result);
    }
}
// create our class which will process the jobs, this will be an instance of
// your application with all your data source connections and stuff set up so
// your jobs can do some real useful work.
$processor = new ExampleProcessor();
$processor->setLoggingEnabled(true);
// start the server and listen for submitted jobs.  this will continue forever
// until the server is stopped.  You can specify the number of worker threads
// to use.  If these die because of fatal errors then they will be restarted (@todo)
$server = new Naph\PTask\Server\Standard($processor);
$server->setLoggingEnabled(true);
$server->listen();
 * TODO: note that you need to comment out require_once line
 * for Example_types on Example/Example.php
 */
require_once $GEN_DIR . "/Example/Example.php";
require_once $GEN_DIR . "/Example/Example_types.php";
// Server implementation
class ExampleHandler implements ExampleIf
{
    protected $log = array();
    //this is the showTime function of server which returns current timestamp
    public function showCurrentTimestamp()
    {
        $now = time();
        return $now;
    }
    // FIXME : oneway methods doesnt work for PHP HTTP server
    public function asynchronousJob()
    {
        sleep(10);
        file_put_contents("/tmp/thrift", "foo", FILE_APPEND);
    }
}
header('Content-Type', 'application/x-thrift');
$handler = new ExampleHandler();
$processor = new ExampleProcessor($handler);
$transport = new TBufferedTransport(new TPhpStream(TPhpStream::MODE_R | TPhpStream::MODE_W));
$protocol = new TBinaryProtocol($transport, true, true);
//start server
$transport->open();
$processor->process($protocol, $protocol);
$transport->close();