Your SlideShare is downloading. ×
0
1

DAEMONS IN PHP
Zoltán Németh
Core Systems Manager @ Ustream
We. Are.
NOW PLAYING

DAEMONS IN PHP

1

Mar 26, 2013

.
CONTENTS







NOW PLAYING

WHY????
The core: daemonizing
Signal handling
Init scripts
Easy building of daemons
Th...
WHY????
 The need for daemons
– Frequently running scheduled tasks
– Async processing
– Cron drawbacks:
 Can do only min...
WHY????
 PHP had some problems in the early days
– Hard to manage long running scripts
– Memory leaks
– Problems with for...
WHY????
 Why daemons in PHP?
– Mostly PHP codebase
– Duplication of business logic leads to hell
 Lot of the logic codes...
DAEMONIZING – THE CONCEPT
 At startup, fork a child and exit from the
parent
– Start a new background session for the chi...
DAEMONIZING – THE CODE

NOW PLAYING

DAEMONS IN PHP

7

Mar 26, 2013
DAEMONIZING – THE CODE

NOW PLAYING

DAEMONS IN PHP

8

Mar 26, 2013
DAEMONIZING – THE CODE

NOW PLAYING

DAEMONS IN PHP

9

Mar 26, 2013
THE MAIN LOOP

NOW PLAYING

DAEMONS IN PHP

10

Mar 26, 2013
TIMING

NOW PLAYING

DAEMONS IN PHP

11

Mar 26, 2013
MEMORY LEAK CHECKING

NOW PLAYING

DAEMONS IN PHP

12

Mar 26, 2013
SIGNAL HANDLING
 Before PHP 5.3
– declare(ticks=1);
– Signal dispatch by php system, at every tick
– Unreliable
– Precise...
SIGNAL HANDLING
 PHP 5.3
– declare(ticks=1); is deprecated
– Signal dispatch by code:
pcntl_signal_dispatch
– More reliab...
SIGNAL HANDLING

NOW PLAYING

DAEMONS IN PHP

15

Mar 26, 2013
SIGNAL HANDLING

NOW PLAYING

DAEMONS IN PHP

16

Mar 26, 2013
INIT SCRIPTS
 Needed for monit, run daemons on startup,
etc
 In the beginning: separate init script for every
daemon
 L...
INIT SCRIPTS

NOW PLAYING

DAEMONS IN PHP

18

Mar 26, 2013
INIT SCRIPTS
 Conclusion: let’s have a common script in
the repo
 Take the name of the daemon as a
parameter
 Required:...
DAEMON RUNNER AND BUILDER
 Original setup: one php file for each daemon
 Each has its own class definition
– All variabl...
THE BOILERPLATE

NOW PLAYING

DAEMONS IN PHP

21

Mar 26, 2013
DAEMON RUNNER AND BUILDER
 Concept: a generic Runner class to run a
daemon based with a given task
 Task: the repeatedly...
DAEMON RUNNER AND BUILDER
 Builder: a factory which creates the daemon
object
 All configuration options are handed over...
INI FILE EXAMPLE

NOW PLAYING

DAEMONS IN PHP

24

Mar 26, 2013
BUILDER CALL, LIST OF OPTIONS

NOW PLAYING

DAEMONS IN PHP

25

Mar 26, 2013
BUILDER CODE

NOW PLAYING

DAEMONS IN PHP

26

Mar 26, 2013
AVAILABLE OPTIONS
 Sleep: period length (running time is
substracted)
 Minimum sleep: if running time is too long
 Fact...
THE PACKAGE





Ustream_Daemon_Daemon
Ustream_Daemon_Builder
Ustream_Daemon_Runner
Ustream_Daemon_PreconfiguredTaskDe...
THE PACKAGE - INTERFACES
 Ustream_Daemon_Task
– doTask()

 Ustream_Daemon_TaskFactory
– createTaskFor(Ustream_Daemon_Dae...
THE DAEMON MONITOR







Centralized status monitor system
Every daemon reports status on UDP
Server in node.js
Dis...
THE DAEMON MONITOR

NOW PLAYING

DAEMONS IN PHP

31

Mar 26, 2013
OPEN SOURCING
 On github: https://github.com/ustream
 Some prerequisites:
– Remove remaining Ustream codebase
specific r...
FURTHER PLANS
 Debug mode:
– remotely enabled
– collect output and dump when stopped

 Runtime setting of loglevel
– Pos...
34

QUESTIONS?

We. Are.
NOW PLAYING

DAEMONS IN PHP

34

Mar 26, 2013

.
Upcoming SlideShare
Loading in...5
×

Daemons in PHP

191

Published on

Talk @ PHP meetup Budapest, 2013.03.26.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
191
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
1
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Transcript of "Daemons in PHP"

  1. 1. 1 DAEMONS IN PHP Zoltán Németh Core Systems Manager @ Ustream We. Are. NOW PLAYING DAEMONS IN PHP 1 Mar 26, 2013 .
  2. 2. CONTENTS        NOW PLAYING WHY???? The core: daemonizing Signal handling Init scripts Easy building of daemons The current package Open sourcing DAEMONS IN PHP 2 Mar 26, 2013
  3. 3. WHY????  The need for daemons – Frequently running scheduled tasks – Async processing – Cron drawbacks:  Can do only minute level  Does not guarantee exact timing  Cron jobs cannot be monitored NOW PLAYING DAEMONS IN PHP 3 Mar 26, 2013
  4. 4. WHY????  PHP had some problems in the early days – Hard to manage long running scripts – Memory leaks – Problems with forking – Handling code changes NOW PLAYING DAEMONS IN PHP 4 Mar 26, 2013
  5. 5. WHY????  Why daemons in PHP? – Mostly PHP codebase – Duplication of business logic leads to hell  Lot of the logic codes used in daemons were already used on the site – PHP experience – Quick and easy development NOW PLAYING DAEMONS IN PHP 5 Mar 26, 2013
  6. 6. DAEMONIZING – THE CONCEPT  At startup, fork a child and exit from the parent – Start a new background session for the child  In the child, have a loop which does the task  Calculate timing after each loop and use usleep for precision  Have a pidfile (for monit etc) NOW PLAYING DAEMONS IN PHP 6 Mar 26, 2013
  7. 7. DAEMONIZING – THE CODE NOW PLAYING DAEMONS IN PHP 7 Mar 26, 2013
  8. 8. DAEMONIZING – THE CODE NOW PLAYING DAEMONS IN PHP 8 Mar 26, 2013
  9. 9. DAEMONIZING – THE CODE NOW PLAYING DAEMONS IN PHP 9 Mar 26, 2013
  10. 10. THE MAIN LOOP NOW PLAYING DAEMONS IN PHP 10 Mar 26, 2013
  11. 11. TIMING NOW PLAYING DAEMONS IN PHP 11 Mar 26, 2013
  12. 12. MEMORY LEAK CHECKING NOW PLAYING DAEMONS IN PHP 12 Mar 26, 2013
  13. 13. SIGNAL HANDLING  Before PHP 5.3 – declare(ticks=1); – Signal dispatch by php system, at every tick – Unreliable – Precise, fixed intervals  Handler functions with pcntl_signal NOW PLAYING DAEMONS IN PHP 13 Mar 26, 2013
  14. 14. SIGNAL HANDLING  PHP 5.3 – declare(ticks=1); is deprecated – Signal dispatch by code: pcntl_signal_dispatch – More reliable – Not precise, changing intervals (code execution determines timing) NOW PLAYING DAEMONS IN PHP 14 Mar 26, 2013
  15. 15. SIGNAL HANDLING NOW PLAYING DAEMONS IN PHP 15 Mar 26, 2013
  16. 16. SIGNAL HANDLING NOW PLAYING DAEMONS IN PHP 16 Mar 26, 2013
  17. 17. INIT SCRIPTS  Needed for monit, run daemons on startup, etc  In the beginning: separate init script for every daemon  Lot of copy-paste with minimal changing information  Hard to keep consistent: changes do not propagate NOW PLAYING DAEMONS IN PHP 17 Mar 26, 2013
  18. 18. INIT SCRIPTS NOW PLAYING DAEMONS IN PHP 18 Mar 26, 2013
  19. 19. INIT SCRIPTS  Conclusion: let’s have a common script in the repo  Take the name of the daemon as a parameter  Required: a class to run a daemon by its name – Daemon_Runner NOW PLAYING DAEMONS IN PHP 19 Mar 26, 2013
  20. 20. DAEMON RUNNER AND BUILDER  Original setup: one php file for each daemon  Each has its own class definition – All variable initializations – All configurations  Constructing the object and all dependencies  Starting the daemon  Lot of boilerplate code  Daemon_Runner NOW PLAYING DAEMONS IN PHP 20 Mar 26, 2013
  21. 21. THE BOILERPLATE NOW PLAYING DAEMONS IN PHP 21 Mar 26, 2013
  22. 22. DAEMON RUNNER AND BUILDER  Concept: a generic Runner class to run a daemon based with a given task  Task: the repeatedly running part of the daemon  Ustream_Daemon_Task interface  Independent of the daemon configuration NOW PLAYING DAEMONS IN PHP 22 Mar 26, 2013
  23. 23. DAEMON RUNNER AND BUILDER  Builder: a factory which creates the daemon object  All configuration options are handed over by the builder  Runner: call the builder and then run the resulting class  Configuration in .ini files NOW PLAYING DAEMONS IN PHP 23 Mar 26, 2013
  24. 24. INI FILE EXAMPLE NOW PLAYING DAEMONS IN PHP 24 Mar 26, 2013
  25. 25. BUILDER CALL, LIST OF OPTIONS NOW PLAYING DAEMONS IN PHP 25 Mar 26, 2013
  26. 26. BUILDER CODE NOW PLAYING DAEMONS IN PHP 26 Mar 26, 2013
  27. 27. AVAILABLE OPTIONS  Sleep: period length (running time is substracted)  Minimum sleep: if running time is too long  Factory: Task factory class  Instance: for multi instance daemons NOW PLAYING DAEMONS IN PHP 27 Mar 26, 2013
  28. 28. THE PACKAGE     Ustream_Daemon_Daemon Ustream_Daemon_Builder Ustream_Daemon_Runner Ustream_Daemon_PreconfiguredTaskDelegator: a generic daemon class used by the builder. Runs a task  Ustream_Daemon_Logger: a file based logger for the daemon package. Should be PSR3 NOW PLAYING DAEMONS IN PHP 28 Mar 26, 2013
  29. 29. THE PACKAGE - INTERFACES  Ustream_Daemon_Task – doTask()  Ustream_Daemon_TaskFactory – createTaskFor(Ustream_Daemon_Daemon $daemon)  Ustream_Daemon_Starter – start() NOW PLAYING DAEMONS IN PHP 29 Mar 26, 2013
  30. 30. THE DAEMON MONITOR       Centralized status monitor system Every daemon reports status on UDP Server in node.js Displays info Sends commands: e.g. Stop Will be moved to a separate package NOW PLAYING DAEMONS IN PHP 30 Mar 26, 2013
  31. 31. THE DAEMON MONITOR NOW PLAYING DAEMONS IN PHP 31 Mar 26, 2013
  32. 32. OPEN SOURCING  On github: https://github.com/ustream  Some prerequisites: – Remove remaining Ustream codebase specific references – Move Daemon Monitor related stuff to separate package  Planned ETA in April NOW PLAYING DAEMONS IN PHP 32 Mar 26, 2013
  33. 33. FURTHER PLANS  Debug mode: – remotely enabled – collect output and dump when stopped  Runtime setting of loglevel – Possibly from remote daemon monitor  Code coverage collecting NOW PLAYING DAEMONS IN PHP 33 Mar 26, 2013
  34. 34. 34 QUESTIONS? We. Are. NOW PLAYING DAEMONS IN PHP 34 Mar 26, 2013 .
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×