-
Notifications
You must be signed in to change notification settings - Fork 138
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
Continue after interrupt by signal #230
Comments
@borro - For the laymen that use libgearman, but who are not familiar with its inner-workings, etc., if you have the time, would you please consider adding a little high-level context, so all can understand the benefit of your proposal? |
@bmeynell
Result: Expected Result: 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');
$worker = new \GearmanWorker();
$worker->addOptions(GEARMAN_WORKER_NON_BLOCKING);
$worker->addServer();
$worker->setTimeout(10000);
$worker->addFunction('test', function (\GearmanJob $arg) {
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 |
@borro - i might be putting my foot in my mouth, but fwiw, long, long ago i was using PHP's signal handlers in tandem with @brianlmoon @wcgallego - any thoughts? |
Now if you use signals and sleep, then sleep is interrupted by a signal. See block "Return Values" http://php.net/manual/en/function.sleep.php#refsect1-function.sleep-returnvalues For example: #!/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;
}
pcntl_async_signals(true); // ←if remove this line, then sleep exit by signal, but handler don't executed.
pcntl_alarm(1);
pcntl_signal(SIGINT, function ($signo, $signinfo) {
debug('Signo: %s, Signinfo: %s', $signo, print_r($signinfo, 1));
});
pcntl_signal(SIGALRM, function ($signo, $signinfo) {
debug('Signo: %s, Signinfo: %s', $signo, print_r($signinfo, 1));
});
debug('SIGINT, SIGALRM inited');
debug('Start sleep');
sleep(10);
debug('End of file'); Output:
I hope that you will make the possibility of interruption by a signal in |
If you want to improve the signal handling here, your best bet would be to come up with a patch and submit a pull request (PR). Be sure to include a test case (or extend an existing test) in the tests subdirectory that fails without your patch and passes with your patch. Then someone will do a a code review on your PR.... |
@esabol I'm afraid, but I'm very bad C++ developer. |
borro, this is tricky stuff to do in a library because while your way may be more correct, there may also be people relying on this behavior. Ideally we introduce a mode flag that can be used to conditionally return the right thing, and then after some broad testing, maybe drop the old "broken" way. I believe this can be a condition on the universal structure, but I have'nt looked at that code in quite some time. Trust your instincts and submit a PR. We'll try to get it into generally consumable shape, and release it some day. :) |
I create PR #231 |
In function
gearman_wait
you usepoll
(libgearman/universal.cc:316).Why don’t you interrupt the
gearman_wait
job after signal arrives, and again start waitpoll
?https://github.com/gearman/gearmand/blob/master/libgearman/universal.cc#L322
The text was updated successfully, but these errors were encountered: