Download the PHP package wapmorgan/system-daemon without Composer

On this page you can find all versions of the php package wapmorgan/system-daemon. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package system-daemon

SystemDaemon

Simple base for system daemons.

Latest Stable Version Latest Unstable Version License

Why do you need SystemDaemon?

  1. You want to create a daemon (the process that works in the background) and be able to manage it
  2. You want to get rid of unnecessary dependencies in the project
  3. You do not want to write such a base for a daemon

If all three rules are fit for you, SystemDaemon will satisfy all your needs.

What you need to create your own daemon:

  1. Create a class that inherits AbstractDaemon and implement methods for working in the background
  2. Run an instance of this class or use DaemonManager

In fact, you can avoid creating a new class and use the base class to create a daemon to see it's work. Also we will use DaemonManager not to write the code for processing user commands for starting / stopping the daemon.

DaemonManager processes input in the command line and runs the necessary methods of the AbstractDaemon class to start, stop, check the status of the daemon and etc.

The simplest daemon that simply outputs simple messages to the log:

Save this file as daemon and run it using php:

You will see a simple help listing the valid commands for starting / stopping / checking the status of the job:

To start the daemon, simply call the same script with the start command. If successful, the process ID after the start will be displayed, which can be used to view the statistics of this process.

Since logging was enabled, you can open the file /tmp/daemon-example.log (or /var/log/daemon-example.log if permissions are allowed) and see what the daemon writes:

This is a standard stub message that is written to the log, unless the standard daemon methods in which all the work is done have been redefined.

You can not use DaemonManager, if it is not necessary. Then, to control the daemon, use the following methods of AbstractDaemon:

All these methods can throw exceptions if something goes wrong (for example, there is not enough permissions to remove the lock file, if the unprivileged user is trying to stop the daemon started by root), so take care of handling such situations.

To resume: the main features that AbstractDaemon provides:

  1. Start the daemon in the background. The ban on running multiple copies of the daemon using a lock file.
  2. Create a log file and write messages to it.
  3. The daemon can be of two types: a normal daemon (in which the onStart() method is started once and all the work is done) and a daemon waking up at certain intervals (called "ticks") and executing the onTick() method.

Types of daemons

The created daemon can work in two modes:

  1. Normal. When one method is started and it processes some external data (for example, listening on a socket). This mode is used by default. To create your daemon, you need to override the onStart() method, which will be called when the daemon starts. For example, this:

  2. Ticking. When a daemon wakes up at regular intervals, it gets some information, does its job and falls asleep until the next "tick". To use this mode, you need to transfer the AbstractDaemon::TICKABLE value and the time that the daemon will wake up (in seconds) when creating a class instance. For example. So:

    To create such a daemon, you need to override the onTick() method, in which specify operations of the daemon within one tick:

    Stopping

    Stop processing for each type of its own:

  3. A normal daemon processes a stop event (called by the stop() method) in the handleStop() method. You can override this method and make closing some sockets or setting a completion flag there, and in onStart() do a check: if the flag was set, finish the data processing and exit the method. In the base class, handleStop() sets the $running property of the daemon to false, so you can just check sometimes if that value is set there and shut down. For example, you can do this:

  4. The ticking daemon is easier to use: it continues its ticks until the stop command comes. Thus, you do not need to manually process this operation. The daemon will end after the tick, at the time of processing of which this command came.

Other signals

You have the ability to process other signals sent by the kill command. At the moment, it is possible to process two signals, which can be used as buffer reset commands, clearing the cache, or something else. These commands are: SIGUSR1 and SIGUSR2. To process them in the daemon, reimplement the onSigUsr1() and onSigUsr2() methods, respectively.

To reboot the daemon settings using the USR1 command, you can implement such a daemon:

Logging

You can use message logging. To enable logging, you must call the setLogger($logger) method after creating the daemon object. Two types of logging are supported:

  1. Logging to syslog. Example of use:

  2. Logging to a separate file. To enable logging in a separate file (/var/log/daemon-daemon name.log, if permissions allow, or /tmp/daemon-name.log):

  3. Logging to a terminal. Daemon just prints all log messages right on the terminal:

To send messages to the log in the daemon use log($level, $message) method, where $level is one of the predefined message severity levels and $message messages.

Predefined levels of importance are:


All versions of system-daemon with dependencies

PHP Build Version
Package Version
Requires ext-posix Version *
ext-pcntl Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package wapmorgan/system-daemon contains the following files

Loading the files please wait ....