<commit_before><?php

require_once 'vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();

const EXIT_OK = 0;
const EXIT_MISCONFIGURATION = 2;
const EXIT_RUNTIME = 3;
const EXIT_TIMEOUT = 4;
/**
 * Output usage / help.
 */
function help() {
    fwrite(STDERR, "Usage information:\n");
    fwrite(STDERR,  "-h host:port Wait for host:port\n");
    fwrite(STDERR,  "-f path Wait for file existence\n");
    fwrite(STDERR,  "-t integer[10] Timeout\n");
}

function error($message, $code)
{
    fwrite(STDERR, $message . "\n");

    if ($code == EXIT_MISCONFIGURATION) {
        help();
    }
    exit($code);
}

function validateConfiguration() {
    // Parse arguments.
    $defaults = [
        'h' => [],
        'f' => [],
        't' => [
            10
        ]
    ];
    $options = array_merge_recursive($defaults, getopt('t:h:f:', [
        'help'
    ]));

    if (isset($options['help'])) {
        help();
        exit(0);
    }

    if (empty($options['h']) && empty($options['f'])) {
        error("At least one host or path must be specified.", EXIT_MISCONFIGURATION);
    }

    foreach($options['h'] as $target) {
        if (!preg_match('/^.*:(\d+)$/', $target, $matches)) {
            error("Invalid host specification: $target", EXIT_MISCONFIGURATION);
        } elseif (intval($matches[1]) > 65535) {
            error("Invalid port: {$matches[1]}", EXIT_MISCONFIGURATION);
        }
    }

    $pos = array_search('--', $_SERVER['argv']);
    if ($pos !== false) {
        $args = array_slice($_SERVER['argv'], $pos + 2);
        $cmd = $_SERVER['argv'][$pos + 1];
        $options['runner'] = function() use ($args, $cmd) {
            echo "Running: $cmd [" . implode(' ', $args) . "]\n";
            pcntl_exec($cmd, $args, $_ENV);
        };
    }

    $options['t'] = intval(array_pop($options['t']));
    return $options;
}
function setup($loop) {
    $options = validateConfiguration();
    $promise = \React\Promise\all([
        waitForServers($options['h'], $loop),
        waitForFiles($options['f'], $loop)
    ]);

    $timer = $loop->addTimer($options['t'], function() {
        error("Timeout", EXIT_TIMEOUT);
    });


    $promise->then(function() use ($options) {
        if (isset($options['runner'])) {
            $options['runner']();
        } else {
            error("All good.", EXIT_OK);
        }<commit_msg>Cancel timer<commit_after><?php

require_once 'vendor/autoload.php';

$loop = \React\EventLoop\Factory::create();

const EXIT_OK = 0;
const EXIT_MISCONFIGURATION = 2;
const EXIT_RUNTIME = 3;
const EXIT_TIMEOUT = 4;
/**
 * Output usage / help.
 */
function help() {
    fwrite(STDERR, "Usage information:\n");
    fwrite(STDERR,  "-h host:port Wait for host:port\n");
    fwrite(STDERR,  "-f path Wait for file existence\n");
    fwrite(STDERR,  "-t integer[10] Timeout\n");
}

function error($message, $code)
{
    fwrite(STDERR, $message . "\n");

    if ($code == EXIT_MISCONFIGURATION) {
        help();
    }
    exit($code);
}

function validateConfiguration() {
    // Parse arguments.
    $defaults = [
        'h' => [],
        'f' => [],
        't' => [
            10
        ]
    ];
    $options = array_merge_recursive($defaults, getopt('t:h:f:', [
        'help'
    ]));

    if (isset($options['help'])) {
        help();
        exit(0);
    }

    if (empty($options['h']) && empty($options['f'])) {
        error("At least one host or path must be specified.", EXIT_MISCONFIGURATION);
    }

    foreach($options['h'] as $target) {
        if (!preg_match('/^.*:(\d+)$/', $target, $matches)) {
            error("Invalid host specification: $target", EXIT_MISCONFIGURATION);
        } elseif (intval($matches[1]) > 65535) {
            error("Invalid port: {$matches[1]}", EXIT_MISCONFIGURATION);
        }
    }

    $pos = array_search('--', $_SERVER['argv']);
    if ($pos !== false) {
        $args = array_slice($_SERVER['argv'], $pos + 2);
        $cmd = $_SERVER['argv'][$pos + 1];
        $options['runner'] = function() use ($args, $cmd) {
            echo "Running: $cmd [" . implode(' ', $args) . "]\n";
            pcntl_exec($cmd, $args, $_ENV);
        };
    }

    $options['t'] = intval(array_pop($options['t']));
    return $options;
}
function setup($loop) {
    $options = validateConfiguration();
    $promise = \React\Promise\all([
        waitForServers($options['h'], $loop),
        waitForFiles($options['f'], $loop)
    ]);

    $timer = $loop->addTimer($options['t'], function() {
        error("Timeout", EXIT_TIMEOUT);
    });


    $promise->then(function() use ($options, $timer) {
        $timer->cancel();
        if (isset($options['runner'])) {
            $options['runner']();
        } else {
            error("All good.", EXIT_OK);
        }
{'fraction_non_alphanumeric': 0.1998742928975487, 'fraction_numerical': 0.00754242614707731, 'mean_word_length': 2.5494423791821563, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, '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': '298573', 'n_tokens_mistral': 1620, 'n_tokens_neox': 1572, 'n_words': 319}