2. Random facts about me:
● CTO in Codesushi since 2012,
● Symfony enthusiast since 2011,
● Summoning daemons in PHP since 2010,
● Graduate of Computer science & Philosophy,
● PHP developer since 2007.
About me
3. Plan of the talk
● Daemons - do we need them?
● Usual way & Tools for creating daemons
● Symfony Daemons in PHP
● Daemons with Symfony Console Component
● Some things to consider
4. Couple of use cases for daemons:
● server:
- WebSockets,
- Socket protocols like SMPP.
● running processes more frequent than 1 minute.
Daemon (/ˈdiːmən/ or /ˈdeɪmən/) is a computer program that runs as a background
process, rather than being under the direct control of an interactive user. [wikipedia]
Daemons - why do we need them?
5. When creating a daemon PHP is not first thing that comes to you.
Usual way & tools for creating a daemon
6. ● Single stack
● Reuse of components
● * We are still inside of Symfony *
Creating daemons in PHP
7. Simplest example in PHP:
while (true) { // your code };
problem with that solution: operating system will eventually kill that process.
Solution for that:
Heartbeat check, each cycle daemon puts timestamp in some file, then cron script reads that file
if timestamp is too much in the past it restarts our daemon.
Basically, this is hacking the operating system
instead of using its features.
Creating daemons in PHP
8. Creating daemons in PHP
There are 2 ways of doing it correctly:
● Forking - outdated:
http://kvz.io/blog/2009/01/09/create-daemons-in-php/
● Using upstart script - recommended
● Or other OS tool similar to upstart
9. Let’s look at some example daemon in pure PHP and upstart script.
Creating daemons in PHP
10. Let’s look at the same example of daemon but this time let’s use Symfony.
Wait, there’s bundle for that
https://github.com/mac-cain13/daemonizable-command
bundle provide some nice interface and gives us some debugging tools, but it is most
useful when we want to address the “cron scenario”
Daemons with
Symfony Console Component
11. Daemon is a process that is running constantly, so few things to consider:
● Connections to other services, like databases,
● Closing opened resources and reopening them,
● Memory leaks.
Some things to consider