Inner Symfony’s DaemonsKrzysztof Ożóg - Symfony CAMP UA 2015 Kiev, 24.10.2015
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
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
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?
When creating a daemon PHP is not first thing that comes to you.
Usual way & tools for creating a daemon
Single stack
Reuse of components
* We are still inside of Symfony *
Creating daemons in PHP
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 fileif timestamp is too much in the past it restarts our daemon.
Basically, this is hacking the operating systeminstead of using its features.
Creating daemons in PHP
Creating daemons in PHPThere 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
Let’s look at some example daemon in pure PHP and upstart script.
Creating daemons in PHP
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”
Symfony Console Component
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
Krzysztof
OżógCTO at
Codesushihello
@codesushi.cowww.codesushi.co
Questions
Yes, we are hiring with relocate to Poland.