Git Product home page Git Product logo

daemon's Introduction

Sweelix\daemon for PHP

About

Sweelix\daemon is a PHP 5.3+ library which is used to create long running php scripts The latest class API documentation is available online.

Requirements

This tiny script depends on

  • ext-pcntl : Process control PHP extension
  • ext-posix : PHP Posix extension

It's also recommended to install

  • ext-proctitle : Extension which allow the proecss name to be changed

Examples

The most useless daemon :

<?php
/**
 * File Writed.php
 *
 * PHP version 5.3+
 *
 * @author    Philippe Gaultier <[email protected]>
 * @copyright 2010-2014 Sweelix
 * @license   http://www.sweelix.net/license license
 * @version   1.0.0
 * @link      http://www.sweelix.net
 * @category  demo
 * @package   sweelix.demo
 */

use sweelix\daemon\Daemon;

/**
 * Writed is a useless daemon which write dots in the CLI.
 * This daemon is just here to demonstrate how it can work.
 * @see sweelix/daemon
 *
 * @author    Philippe Gaultier <[email protected]>
 * @copyright 2010-2014 Sweelix
 * @license   http://www.sweelix.net/license license
 * @version   1.0.0
 * @link      http://www.sweelix.net
 * @category  demo
 * @package   sweelix.demo
 */
class Writed extends Daemon {

	/**
	 * @var integer var used to count the number of dots to display
	 */
	private $_loopNum=0;

	/**
	 * Init our daemon. Here we do nothing except showing we are starting
	 * @see \sweelix\daemon\Daemon::setUp()
	 *
	 * @return void
	 * @since  1.0.0
	 */
	public function setUp() {
		$this->write('Warming up engine'."\n");
	}

	/**
	 * Clean up our daemon. Here we do nothing except showing we are stopping
	 * @see \sweelix\daemon\Daemon::tearDown()
	 *
	 * @return void
	 * @since  1.0.0
	 */
	public function tearDown() {
		$this->write('Cool down the engine'."\n");
	}

	/**
	 * The task we are performing on each loop
	 * We are only writing dots on the screen
	 * @see \sweelix\daemon\Daemon::task()
	 *
	 * @return void
	 * @since  1.0.0
	 */
	public function task() {
		$this->_loopNum++;
		sleep(1);
		$this->write('.');
		if($this->_loopNum >= 10) {
			$this->_loopNum = 0;
			$this->write("\n");
		}
	}
}

Installation

The preferred method of installation is via [Packagist][] and [Composer][]. Run the following command to install the package and add it as a requirement to composer.json:

composer.phar require sweelix/daemon=1.0.0

Running

Now we can run it

<?php
/**
 * File test.php
 *
 * PHP version 5.3+
 *
 * @author    Philippe Gaultier <[email protected]>
 * @copyright 2010-2014 Sweelix
 * @license   http://www.sweelix.net/license license
 * @version   1.0.0
 * @link      http://www.sweelix.net
 * @category  demo
 * @package   sweelix.demo
 */

/**
 * include composer autoloader
 */
require('vendor/autoload.php');
require('Writed.php');

$daemon = new Writed();
// Run it
$daemon->run();

daemon's People

Contributors

pgaultier avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.