<commit_before>#!/usr/bin/env php
<?php
include __DIR__ . '/../common.php';
use Gaia\Test\Tap;
use Gaia\Job;
use Gaia\Job\Runner;
use Gaia\Job\Config;
use Gaia\Pheanstalk;
use Gaia\Nonce;
use Gaia\Debugger;
$queue = 'signedtest';

set_time_limit(0);
if( ! @fsockopen('127.0.0.1', '11300')) {
    die("Beanstalkd not running on localhost\n");
}

if( ! @fsockopen('127.0.0.1', '11299')) {
    die("unable to connect to test job url\n");
}






print "\nInstantiating job runner ... \n";
$runner = new Runner();
$debugger = new Debugger;
$nonce = new Nonce('test001');

$config = Job::config();
$config->addConnection( new Pheanstalk('127.0.0.1', '11300' ) );
$config->setQueuePrefix('test');


$config->setBuilder( function($job, array & $opts ) use ($nonce) {
    $parts = new Gaia\Container( @parse_url( $job->url ));
    $uri = isset( $parts->path ) ? $parts->path : '/';
    if( $parts->query ) $uri = $uri . '?' . $parts->query;
    if( $job->id ) $opts[CURLOPT_HTTPHEADER][] = 'X-Job-Id: ' . $job->id;
    $opts[CURLOPT_HTTPHEADER][] = 'X-JOB-NONCE: ' . $nonce->create($uri, time() + 300 );
});

$config->setHandler( function($job, $response ) use ($runner, $debugger) {
    if( $response->headers->{'X-JOB-STATUS'} == 'complete') $job->flag = 1;
    if( $job->task == 'register'){
        Job::config()->registering = FALSE;
        $res = json_decode($response->body);
        if( ! is_array( $res ) ){
            $runner->shutdown();
        }
    }
    $request = $job;
    $info = $response;
    $out = "\nHTTP";
    if( $job->id ) $out .=" - " . $job->id;
    if( $info->http_code != 200 ) $out .= '-ERR';
    $out .= ": " . $info->url;
    if( $info->http_code == 200 ) {
        $debugger->render( $out );
        return;
    }
    
    $post = substr( (is_array( $job->post ) ? http_build_query( $job->post  ) : $job->post ),0, 75);
    $out .= '  ' . $post;
    if( strlen( $post )  >= 75 ) $out .= ' ...';
    if(  strlen( $info->response_header ) < 1 ) {
        $out .= " - NO RESPONSE";
    } else {
        $out .= "\n------\n";
        $out .= "\n" . $info->request_header;
        $out .= "\n" . $info->response_header . $info->body;
        $out .= "\n------\n";
    }
    $debugger->render( $out );
    
});

$config->set('register_url', 'http://127.0.0.1:11299/job.php?register=1');
<commit_msg>fixing a test in the signed job example<commit_after>#!/usr/bin/env php
<?php
include __DIR__ . '/../common.php';
use Gaia\Test\Tap;
use Gaia\Job;
use Gaia\Job\Runner;
use Gaia\Job\Config;
use Gaia\Pheanstalk;
use Gaia\Nonce;
use Gaia\Debugger;
$queue = 'signedtest';

set_time_limit(0);
if( ! @fsockopen('127.0.0.1', '11300')) {
    die("Beanstalkd not running on localhost\n");
}

if( ! @fsockopen('127.0.0.1', '11299')) {
    die("unable to connect to test job url\n");
}






print "\nInstantiating job runner ... \n";
$runner = new Runner();
$debugger = new Debugger;
$nonce = new Nonce('test001');

$config = Job::config();
$config->addConnection( new Pheanstalk('127.0.0.1', '11300' ) );
$config->setQueuePrefix('test');


$config->setBuilder( function($job, array & $opts ) use ($nonce) {
    $parts = new Gaia\Container( @parse_url( $job->url ));
    $uri = isset( $parts->path ) ? $parts->path : '/';
    if( $parts->query ) $uri = $uri . '?' . $parts->query;
    if( $job->id ) $opts[CURLOPT_HTTPHEADER][] = 'X-Job-Id: ' . $job->id;
    $opts[CURLOPT_HTTPHEADER][] = 'X-JOB-NONCE: ' . $nonce->create($uri, time() + 300 );
});

$config->setHandler( function($job, $response ) use ($runner, $debugger) {
    if( $response->headers->{'X-JOB-STATUS'} == 'complete') $job->flag = 1;
    if( $job->task == 'register'){
        Job::config()->registering = FALSE;
        $res = json_decode($response->body, TRUE);
        if( ! is_array( $res ) ){
            $runner->shutdown();
        }
    }
    $request = $job;
    $info = $response;
    $out = "\nHTTP";
    if( $job->id ) $out .=" - " . $job->id;
    if( $info->http_code != 200 ) $out .= '-ERR';
    $out .= ": " . $info->url;
    if( $info->http_code == 200 ) {
        $debugger->render( $out );
        return;
    }
    
    $post = substr( (is_array( $job->post ) ? http_build_query( $job->post  ) : $job->post ),0, 75);
    $out .= '  ' . $post;
    if( strlen( $post )  >= 75 ) $out .= ' ...';
    if(  strlen( $info->response_header ) < 1 ) {
        $out .= " - NO RESPONSE";
    } else {
        $out .= "\n------\n";
        $out .= "\n" . $info->request_header;
        $out .= "\n" . $info->response_header . $info->body;
        $out .= "\n------\n";
    }
    $debugger->render( $out );
    
});
{'fraction_non_alphanumeric': 0.23515336088753536, 'fraction_numerical': 0.025668914509462692, 'mean_word_length': 2.611940298507463, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 72, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 7, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2323432', 'n_tokens_mistral': 1838, 'n_tokens_neox': 1754, 'n_words': 358}