JApplicationDaemon

Extends \JApplicationCli

Class to turn JCli applications into daemons. It requires CLI and PCNTL support built into PHP.

package

Joomla.Platform

subpackage

Application

see

http://www.php.net/manual/en/book.pcntl.php

http://php.net/manual/en/features.commandline.php

since

11.1

Methods

__construct

Class constructor.

__construct(mixed $input = null, mixed $config = null, mixed $dispatcher = null) 
inherited
see \JApplicationCli::loadDispatcher()
since

11.1

Arguments

$input

mixedAn optional argument to provide dependency injection for the application's input object. If the argument is a JInputCli object that object will become the application's input object, otherwise a default input object is created.

$config

mixedAn optional argument to provide dependency injection for the application's config object. If the argument is a JRegistry object that object will become the application's config object, otherwise a default config object is created.

$dispatcher

mixedAn optional argument to provide dependency injection for the application's event dispatcher. If the argument is a JDispatcher object that object will become the application's event dispatcher, if it is null then the default event dispatcher will be created based on the application's loadDispatcher() method.

changeIdentity

Method to change the identity of the daemon process and resources.

changeIdentity() : boolean
since

11.1

see \posix_setuid()

Response

booleanTrue if identity successfully changed

close

Exit the application.

close(integer $code) : void
inherited
since

11.1

Arguments

$code

integerThe exit code (optional; default is 0).

daemonize

Method to put the application into the background.

daemonize() : boolean
since

11.1

throws

\RuntimeException

Response

boolean

detach

This is truly where the magic happens. This is where we fork the process and kill the parent process, which is essentially what turns the application into a daemon.

detach() : void
since

12.1

throws

\RuntimeException

doExecute

Method to run the application routines. Most likely you will want to instantiate a controller and execute it, or perform some sort of task directly.

doExecute() : void
inherited
since

11.3

execute

Execute the application.

execute() : void
inherited
since

11.1

fetchConfigurationData

Method to load a PHP configuration class file based on convention and return the instantiated data object. You will extend this method in child classes to provide configuration data from whatever data source is relevant for your specific application.

fetchConfigurationData(string $file = '', string $class = 'JConfig') : mixed
inherited
since

11.1

Arguments

$file

stringThe path and filename of the configuration file. If not provided, configuration.php in JPATH_BASE will be used.

$class

stringThe class name to instantiate.

Response

mixedEither an array or object to be loaded into the configuration object.

fork

Method to fork the process.

fork() : integer
since

11.1

throws

\RuntimeException

Response

integerThe child process id to the parent process, zero to the child process.

gc

Method to perform basic garbage collection and memory management in the sense of clearing the stat cache. We will probably call this method pretty regularly in our main loop.

gc() : void
since

11.1

get

Returns a property of the object or the default value if the property is not set.

get(string $key, mixed $default = null) : mixed
inherited
since

11.3

Arguments

$key

stringThe name of the property.

$default

mixedThe default value (optional) if none is set.

Response

mixedThe value of the configuration.

getInstance

Returns a reference to the global JApplicationCli object, only creating it if it doesn't already exist.

getInstance(string $name = null) : \JApplicationCli
inherited static

This method must be invoked as: $cli = JApplicationCli::getInstance();

since

11.1

Arguments

$name

stringThe name (optional) of the JApplicationCli class to instantiate.

Response

\JApplicationCli

in

Get a value from standard input.

in() : string
inherited
since

11.1

Response

stringThe input string from standard input.

isActive

Check to see if the daemon is active. This does not assume that $this daemon is active, but only if an instance of the application is active as a daemon.

isActive() : boolean
since

11.1

Response

booleanTrue if daemon is active.

loadConfiguration

Load an object or array into the application configuration object.

loadConfiguration(mixed $data) : \JApplicationCli
inherited
since

11.1

Arguments

$data

mixedEither an array or object to be loaded into the configuration object.

Response

\JApplicationCliInstance of $this to allow chaining.

loadDispatcher

Method to create an event dispatcher for the application. The logic and options for creating this object are adequately generic for default cases but for many applications it will make sense to override this method and create event dispatchers based on more specific needs.

loadDispatcher() : void
inherited
since

11.3

out

Write a string to standard output.

out(string $text = '', boolean $nl = true) : \JApplicationCli
inherited
since

11.1

Arguments

$text

stringThe text to display.

$nl

booleanTrue (default) to append a new line at the end of the output string.

Response

\JApplicationCliInstance of $this to allow chaining.

pcntlChildExitStatus

Method to return the exit code of a terminated child process.

pcntlChildExitStatus(integer $status) : integer
see \pcntl_wexitstatus()
since

11.3

Arguments

$status

integerThe status parameter is the status parameter supplied to a successful call to pcntl_waitpid().

Response

integerThe child process exit code.

pcntlFork

Method to return the exit code of a terminated child process.

pcntlFork() : integer
see \pcntl_fork()
since

11.3

Response

integerOn success, the PID of the child process is returned in the parent's thread of execution, and a 0 is returned in the child's thread of execution. On failure, a -1 will be returned in the parent's context, no child process will be created, and a PHP error is raised.

pcntlSignal

Method to install a signal handler.

pcntlSignal(integer $signal, callback $handler, boolean $restart = true) : boolean
see \pcntl_signal()
since

11.3

Arguments

$signal

integerThe signal number.

$handler

callbackThe signal handler which may be the name of a user created function, or method, or either of the two global constants SIG_IGN or SIG_DFL.

$restart

booleanSpecifies whether system call restarting should be used when this signal arrives.

Response

booleanTrue on success.

pcntlWait

Method to wait on or return the status of a forked child.

pcntlWait( &$status, integer $options) : integer
see \pcntl_wait()
since

11.3

Arguments

$status

$options

integerIf wait3 is available on your system (mostly BSD-style systems), you can provide the optional options parameter.

Response

integerThe process ID of the child which exited, -1 on error or zero if WNOHANG was provided as an option (on wait3-available systems) and no child was available.

postFork

Method to handle post-fork triggering of the onFork event.

postFork() : void
since

12.1

registerEvent

Registers a handler to a particular event group.

registerEvent(string $event, callback $handler) : \JApplicationCli
inherited
since

11.1

Arguments

$event

stringThe event name.

$handler

callbackThe handler, a function or an instance of a event object.

Response

\JApplicationCliInstance of $this to allow chaining.

restart

Restart daemon process.

restart() : void
since

11.1

set

Modifies a property of the object, creating it if it does not already exist.

set(string $key, mixed $value = null) : mixed
inherited
since

11.3

Arguments

$key

stringThe name of the property.

$value

mixedThe value of the property to set (optional).

Response

mixedPrevious value of the property

setupSignalHandlers

Method to attach the JApplicationDaemon signal handler to the known signals. Applications can override these handlers by using the pcntl_signal() function and attaching a different callback method.

setupSignalHandlers() : boolean
since

11.1

see \pcntl_signal()

Response

boolean

shutdown

Method to shut down the daemon and optionally restart it.

shutdown(boolean $restart = false) : void
since

11.1

Arguments

$restart

booleanTrue to restart the daemon on exit.

signal

Method to handle POSIX signals.

signal(integer $signal) : void
static
since

11.1

see \pcntl_signal()
throws

\RuntimeException

Arguments

$signal

integerThe received POSIX signal.

start

Spawn daemon process.

start() : void
since

11.1

stop

Stop daemon process.

stop() : void
since

11.1

triggerEvent

Calls all handlers associated with an event group.

triggerEvent(string $event, array $args = null) : array
inherited
since

11.1

Arguments

$event

stringThe event name.

$args

arrayAn array of arguments (optional).

Response

arrayAn array of results from each function call, or null if no dispatcher is defined.

writeProcessIdFile

Method to write the process id file out to disk.

writeProcessIdFile() : boolean
since

11.1

Response

boolean

Properties

signals

The available POSIX signals to be caught by default.

static
see

http://php.net/manual/pcntl.constants.php

since

11.1

Type(s)

array

exiting

True if the daemon is in the process of exiting.

since

11.1

Type(s)

boolean

processId

The process id of the daemon.

since

11.1

Type(s)

integer

running

True if the daemon is currently running.

since

11.1

Type(s)

boolean

input

The application input object.

inherited
since

11.1

Type(s)

\JInputCli

config

The application configuration object.

inherited
since

11.1

Type(s)

\JRegistry

dispatcher

The application dispatcher object.

inherited
since

11.1

Type(s)

\JDispatcher

instance

The application instance.

inherited static
since

11.1

Type(s)

\JApplicationCli