5

I'd like to use redis pub/sub in PHP, but I'm afraid PHP can't be the only tool: a subscriber need to be always callable, since php isn't built for running as a daemon, I can't trust it to reliably be always "on".

So what is the solution for the PHP world?

  1. don't use pub/sub, use other redis' storages with a crontask launching php every x minutes
  2. use a broker which will call php?
  3. other?

With the "2." I mean : use a nodejs/java/fooBar server which is the daemonized subscriber and call back the php (using http/cli or whatever).

I can't find a better idea than the "2." , but it seem so ineffective at the same way...

What is your opinion?


EDIT : How would you do this using a cloud platform like platform.sh which do not give the opportunity to have a supervisor.d alike?

9
  • 1
    Just run php daemons work fine for me. Laravel has support for redis pub/sub Commented Dec 28, 2016 at 7:59
  • 1
    I don't know php tech, but I think there were something for daemonizing php code. Commented Dec 28, 2016 at 7:59
  • @MikeMiller does it work well? Is it long term stable or do you have to make workaround like relaunching the daemon every X time? I'm concerned it would end has using the bad tool for the task : PHP is built as a short life script language, I know it had memleaks with cross referenced array for example. Commented Dec 28, 2016 at 8:06
  • 1
    @bruno you need to run it with supervisor.d or similar to keep it up if the script errors. You should probably look at some php CLI library like symfony.com/doc/current/components/console.html. I have stable projects been around for a couple of years and had no issues Commented Dec 28, 2016 at 11:39
  • 1
    @bruno you could do. Before switching to the symfony component I ran them in infinite loops with a second sleep so it didn't go too crazy. Not sure what symfony is up to but I reckon it's the same thing. Only thing to remember of you go that way is you need to restart after a code change or it will keep running your old code Commented Dec 28, 2016 at 16:08

1 Answer 1

2

Thanks to the comments, I found a satisfying way to go : use of supervisor.d which will relaunch a symfony Command script with :

  • set_time_limit(0)
  • an infinite loop
  • a blocking call to redis (a BRPOP with a max way of 1 sec. lower than the read_write_timeout)
    • it is important to do a blocking command, in order to not consume all the CPU time
    • I would event go to a real pub/sub, but for now, I only have one listener so it don't matter

what I can tell from an early point of view :

  • supervisor.d is really easy to install/configure, the doc is complete, I didn't run in any problem, it's very rare + satisfying!
  • it seems to works well
  • logs are written, so it may be more easy to understand futur crashes
  • in case of X successive and near crashes, the service is stopped, I didn't find a way to be notified of this, it is really a problem, I think I'll go to this solution(doc)
  • like @Mike Miller said : "Only thing to remember of you go that way is you need to restart after a code change or it will keep running your old code"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.