Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Signal dispathing after end GearmanWorker::wait #67

Closed
borro opened this issue Feb 26, 2019 · 1 comment
Closed

Signal dispathing after end GearmanWorker::wait #67

borro opened this issue Feb 26, 2019 · 1 comment

Comments

@borro
Copy link

borro commented Feb 26, 2019

  1. Set pcntl_async_signals in true
  2. Register handler for pcntl_signal
  3. Start wait for Gearamn work (GearmanWorker::wait)
  4. Send signal when GearmanWorker::wait work.

Result:
Signal handler call after end work GearmanWorker::wait.

Expected Result:
Signal dispatched whet GearmanWorker::wait work.

Run in cli (require php 7.1+, pcntl, pecl-gearman):

#!/usr/bin/env php

<?php

function debug($string, ...$args)
{
    printf('%s: %s', date('Y-m-d H:i:s'),sprintf($string, ...$args));
    echo PHP_EOL;
}

$quit = false;

pcntl_async_signals(true);

pcntl_alarm(1);

pcntl_signal(SIGINT, function ($signo, $signinfo) use (&$quit) {
    $quit = true;
    debug('Signo: %s, Signinfo: %s', $signo, print_r($signinfo, 1));
});
pcntl_signal(SIGALRM, function ($signo, $signinfo) use (&$quit) {
    $quit = true;
    debug('Signo: %s, Signinfo: %s', $signo, print_r($signinfo, 1));
});
debug('SIGINT, SIGALRM inited');

$client = new \GearmanClient();
$client->addServer();

$worker = new \GearmanWorker();
$worker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
$worker->addServer();
$worker->setTimeout(5000);

$worker->addFunction('test', function (\GearmanJob $arg) use ($client) {
    debug('Gearman worker do %s with %s', $arg->functionName(), $arg->workload());
    return 1;
});

debug('Start while worker');
while (!$quit) {
    $worker->work();
    $returnCode = $worker->returnCode();
    if ($returnCode === GEARMAN_NO_JOBS) {
        debug('Wait for jobs');
        $worker->wait();
        continue;
    }
    if ($returnCode === GEARMAN_IO_WAIT) {
        continue;
    }
    if ($returnCode !== GEARMAN_SUCCESS) {
        debug('return_code: %s', $returnCode);
    }
}
debug('Stop while worker');

If $worker->wait(); replace on sleep(10); then script stops almost instantly.

@borro
Copy link
Author

borro commented Feb 28, 2019

I found out that the problem is in the libgearman. gearman/gearmand#230

@borro borro closed this as completed Feb 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant