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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Bruno
  • 1,088
  • 1
  • 13
  • 31
  • 1
    Just run php daemons work fine for me. Laravel has support for redis pub/sub – Mike Miller Dec 28 '16 at 07:59
  • 1
    I don't know php tech, but I think there _were_ something for daemonizing php code. – Sergio Tulentsev Dec 28 '16 at 07: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. – Bruno Dec 28 '16 at 08:06
  • https://redis.io/clients#php could give some ideas... – Konstantin A. Magg Dec 28 '16 at 08:29
  • I found this project : https://github.com/michelsalib/BCCResqueBundle wich can work in conjunction with http://supervisord.org/ in order to have background workers. From now I started to code a more humble code wich will rely on the cron tab I'll see if I feel the need of a more robust solution (wich seem also so far much more complex) – Bruno Dec 28 '16 at 11:23
  • 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 http://symfony.com/doc/current/components/console.html. I have stable projects been around for a couple of years and had no issues – Mike Miller Dec 28 '16 at 11:39
  • @MikeMiller I've installed supervisor.d, it's surprisingly simple to configure. I just have a question : I created my php CLI script to end after 60 seconds (I had in mind to relaunch it every minutes with a cron job). Will supervisor.d relaunch my program each time it end by itself (without an error), or do I have to git it an infinite duration? – Bruno Dec 28 '16 at 14:06
  • 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 – Mike Miller Dec 28 '16 at 16:08
  • I have a new question : how would you handle this if you use platform.sh alike, wich seem to not permit a superviror.d alike? PS : do you think that I should ask a new question on stackoverflow – Bruno Jan 10 '17 at 09:46

1 Answers1

1

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"
Community
  • 1
  • 1
Bruno
  • 1,088
  • 1
  • 13
  • 31