SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.
SlideShare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.
Successfully reported this slideshow.
Activate your 14 day free trial to unlock unlimited reading.
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.
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.
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.
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.
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.
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.
DAEMONIZING – THE CODE
NOW PLAYING
DAEMONS IN PHP
7
Mar 26, 2013
8.
DAEMONIZING – THE CODE
NOW PLAYING
DAEMONS IN PHP
8
Mar 26, 2013
9.
DAEMONIZING – THE CODE
NOW PLAYING
DAEMONS IN PHP
9
Mar 26, 2013
10.
THE MAIN LOOP
NOW PLAYING
DAEMONS IN PHP
10
Mar 26, 2013
11.
TIMING
NOW PLAYING
DAEMONS IN PHP
11
Mar 26, 2013
12.
MEMORY LEAK CHECKING
NOW PLAYING
DAEMONS IN PHP
12
Mar 26, 2013
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.
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.
SIGNAL HANDLING
NOW PLAYING
DAEMONS IN PHP
15
Mar 26, 2013
16.
SIGNAL HANDLING
NOW PLAYING
DAEMONS IN PHP
16
Mar 26, 2013
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.
INIT SCRIPTS
NOW PLAYING
DAEMONS IN PHP
18
Mar 26, 2013
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.
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.
THE BOILERPLATE
NOW PLAYING
DAEMONS IN PHP
21
Mar 26, 2013
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.
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.
INI FILE EXAMPLE
NOW PLAYING
DAEMONS IN PHP
24
Mar 26, 2013
25.
BUILDER CALL, LIST OF OPTIONS
NOW PLAYING
DAEMONS IN PHP
25
Mar 26, 2013
26.
BUILDER CODE
NOW PLAYING
DAEMONS IN PHP
26
Mar 26, 2013
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.
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.
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.
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.
THE DAEMON MONITOR
NOW PLAYING
DAEMONS IN PHP
31
Mar 26, 2013
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.
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
QUESTIONS?
We. Are.
NOW PLAYING
DAEMONS IN PHP
34
Mar 26, 2013
.