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 ;-).
Users of Drushd 7.x-1.x: The new 7.x-2.x version is drastically different to accommodate the changes to Drush, as well as, to simplify the creation of custom daemons. Unfortunately this means it is not backwards compatible and existing daemons will need to be re-written to take advantage of Drush 5.x and the many new features provided by this module. See the Custom Daemon Development section below for more information on how to upgrade your existing daemons.
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. Note that in the example below you want to change all the values to match your custom daemon including the key of the $daemon array which should match the machine_name of your daemon.
/**
* 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['mymodule_daemon'] = array(
// The machine name of the daemon (same as key above).
'machine_name' => 'mymodule_daemon',
// A human-readable name for your daemon.
'name' => 'My Daemon',
// This module (ie: the module implementing the daemon).
'module' => 'mymodule',
// The class extending DrushDaemon and implementing your daemon-specific
// functionality. This class should be in a [classname].inc file in your
// modules base directory.
'class' => 'MyModuleDaemon',
// 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' => 'MyModuleDaemon.inc',
);
return $daemon;
}
2. Implement a DrushDaemon 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 DrushDaemon {
/**
* Implements DrushDaemon::executeTask().
*
* This gets executed once per loop iteration.
*/
protected function executeTask($iteration_number) {
// 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!
Special Thanks: I would like to specifically thank q0rban, the original creator & maintainer of this module, for his work in the early days of this module. Your original version did a lot to inspire 7.x-2.x. ~LaceySanderson
Project information
- 98 sites report using this module
- Created by q0rban on , updated
- Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.
Releases
General update including more control to other modules to provide options during daemon startup.
Development version: 7.x-2.x-dev updated 28 Jun 2017 at 03:44 UTC