GithubHelp home page GithubHelp logo

PHP7 signal handler about yii2-beanstalk HOT 4 CLOSED

udokmeci avatar udokmeci commented on August 12, 2024
PHP7 signal handler

from yii2-beanstalk.

Comments (4)

rtilte-reol avatar rtilte-reol commented on August 12, 2024 2

I still didn't figure out what the issue is with PHP7, but we can use reserve-with-timeout instead of reserve in your code, in BeanstalkController, #307

$job = $bean->reserve(1);

from yii2-beanstalk.

udokmeci avatar udokmeci commented on August 12, 2024

Hi, I have checkout the document but there is nothing about the change of behavior in ticks other than using constants.

So what is the exact issue can you post an output here?
Bests,

from yii2-beanstalk.

rtilte-reol avatar rtilte-reol commented on August 12, 2024

Thanks for the reply,

There's actually no output at all, Ctrl+C does not stop the worker at all, it's "frozen" in the reserve() mode and waits for tube to be populated before returning to the script. It looks like the issue is really coming from PHP7 and the way they deal with signals. By adding a timeout to reserve, script goes back into the while loop after the timeout to check what happened and actually "sees" the signal only at that moment.

I cannot really explain more what happens, but I can give steps to reproduce the issue (or similar issue actually):

  1. Get a VM with whatever distro you want and a working PHP7 setup (Ubuntu 16.04 is using PHP7 by default)
  2. Copy / paste the following script:
<?php
declare(ticks = 1);

pcntl_signal(SIGTERM, 'signal_handler');
pcntl_signal(SIGINT, 'signal_handler');


function signal_handler($signal)
{
    switch ($signal) {
        case SIGTERM:
            print "Caught SIGTERM\n";
            exit;
        case SIGKILL:
            print "Caught SIGKILL\n";
            exit;
        case SIGINT:
            print "Caught SIGINT\n";
            exit;
    }
}

while (1) {
    //usleep(100);
}

Try to kill the script with Ctrl+C and see that it does not work, even with ticks declared. Process gets stucked in the while loop forever. To kill it, you have to

ps aux | grep SCRIPT_NAME
kill -9 PROCESS_PID

Note: You need to kill -9, even a single kill PROCESS_PID will not get rid of it.

Now if you uncomment the usleep() portion, Ctrl+C will strop the script because it checks what it has to do before the next loop, and catches the signal at that moment. You can add pretty much anything there ($a = 1) but the script needs to do something in the loop. This behavior does not exists in PHP5.

With reserve(), it just waits for something to come in the tube, and will hang forever in case nothing comes in.

In any case, I think it's fairly easy to setup some params in the config to indicate the default timeout and use reserve with timeout, just in case. If you think this issue is not relevant, I'll just override the function as I see the need, and will appreciate your feedback.

from yii2-beanstalk.

michaelarnauts avatar michaelarnauts commented on August 12, 2024

I can confirm this behaviour since the upgrade to PHP7.

from yii2-beanstalk.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.