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
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
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
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
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
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
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
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