Daemon API
Categories
Component ID
Component name
Component type
Maintenance status
Development status
Component security advisory coverage
Component created
Component changed
Component body
This project has been moved to 7.x-2.x of the Drush Daemon Project.
This module is meant to provide a simple means of creating a robust command-line-driven, fully bootstrapped PHP Daemon. It uses the PHP-Daemon (https://github.com/shaneharter/PHP-Daemon) Library to create the Daemon (via the Libraries API) in order to not re-invent the wheel ;-).
Features
- Provides a Drush interface to start/stop your Daemon.
- Your daemon starts in the background and is detached from the current terminal.
- Daemon is fully bootstrapped so you can use any Drupal function when designing your task/job.
- Easy, two-step process to help you develop a custom bootstrapped daemon with Drush support.
- Add advanced functionality and override default functionality easily due to the class-based structure of the Daemon API.
- Lock Files, Automatic restart (8hrs default) and Built-in Signal Handling & Event Logging are only a few of the features provided by the PHP-Daemon Library making this a fully featured & robust Daemon.
Custom Daemon Development
Using this API to create your own daemon takes only two steps:
1. Register your Daemon with the Daemon API:
This is done by implementing hook_daemon_api_info() in your .module file.
/**
* Implements hook_daemon_api_info().
* Registers our Daemon with the Daemon API
*/
function mymodule_daemon_api_info() {
$daemon = array();
// This is an example daemon which just sleeps for random amounts of time.
$daemon['example_daemon'] = array(
// The machine name of the daemon (same as key above).
'machine_name' => 'example_daemon',
// A human-readable name for your daemon.
'name' => 'Example Daemon',
// This module (ie: the module implementing the daemon).
'module' => 'daemon_api',
// The class extending DrupalDaemon and implementing your daemon-specific
// functionality. This class should be in a [classname].inc file in your
// modules base directory.
'class' => 'DaemonApiExampleDaemon',
// OPTIONAL: Define this if your module doesn't follow the rule mentioned
// in the above comment. The name and path to the file containing the
// daemon class assuming your module folder as the root.
'class_file' => 'DaemonApiExampleDaemon.inc',
);
return $daemon;
}
2. Implement a DaemonApiDaemon class:
Simply create a class as shown in the code snippet below. This should be located in a [classname].inc file in your module directory and only needs to implement one method executeTask().
/**
* My Custom Daemon
*/
class MyModuleDaemon extends DaemonApiDaemon {
/**
* Implements DrupalDaemon::executeTask().
*
* This gets executed once per loop iteration.
*/
protected function execute_task() {
// Do all of your task/job logic here.
// This is a fully Drupal Bootstrapped environment meaning all your module
// functions and really any function available to your site.
// Log any output
$this->log("Tell my users what I'm doing");
}
}
Finally run your daemon on the command-line using drush daemon start [daemon machine name]. That really is it!
Custom Daemon Usage
Start Daemon
drush daemon start [daemon-machine-name]
Stop Daemon
drush daemon stop [daemon-machine-name]
Check the Status
drush daemon status [daemon-machine-name]
Show the Log
- List the last 10 lines of the log file:
drush daemon show-log [daemon-machine-name] - List the last N lines of the log file:
drush daemon show-log [daemon-machine-name] --num_lines=N
Requirements
- Libraries API
- PHP-Daemon Library version 2.0
- Drush 5.x
Installation
- Install all required modules as per their instructions.
- Install this module as you would normally install a contributed drupal
module. See:https://drupal.org/documentation/install/modules-themes/modules-7
for further information. - Download the PHP-Daemon Library and extract it in your sites/all/libraries
directory. The folder must be named "PHP-Daemon".
Future Development
- Report Page (Administration > Reports > Daemons) showing the current status of all registered daemons and linking to log files so you can monitor your daemon through the web interface!
