Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

12
Inner Symfony’s Daemons Krzysztof Ożóg - Symfony CAMP UA 2015 Kiev, 24.10.2015

Transcript of Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

Page 1: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

Inner Symfony’s DaemonsKrzysztof Ożóg - Symfony CAMP UA 2015 Kiev, 24.10.2015

Page 2: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

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

Page 3: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

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

Page 4: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

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?

Page 5: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

When creating a daemon PHP is not first thing that comes to you.

Usual way & tools for creating a daemon

Page 6: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

Single stack

Reuse of components

* We are still inside of Symfony *

Creating daemons in PHP

Page 7: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

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

Page 8: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

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

Page 9: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

Let’s look at some example daemon in pure PHP and upstart script.

Creating daemons in PHP

Page 10: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

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

Page 11: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

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

Page 12: Inner Symfony’s Daemons - Symfony CAMP UA 2015 Kiev, 24.10.2015 - Krzysztof Ożóg

Krzysztof

OżógCTO at

Codesushihello

@codesushi.cowww.codesushi.co

Questions

Yes, we are hiring with relocate to Poland.