Giter VIP home page Giter VIP logo

backgroundprocess's Introduction

BackgroundProcess

Build Status Coverage Status

BackgroundProcess is a PHP Library to run background processes asynchronously on your system.

How it works

20130812192611

BackgroundProcess is simple. So it doesn't need any specific datastores such as RDBMS. And dosen't use somthing like a queue and daemon process.

When BackgroundProcess runs a process on the background, It creates two special files. One of them is Executable PHP file. Anothe one is Json meta data file.

Executable PHP file is a PHP file executed by BackgroundProcess. It includes the command to run specified by you. It also has some initialization and finalization code. At default, this file name is like the following.

/tmp/php/background_process/process.2494951fd9d147bc3e.php

Json meta data file is a json formmated file. it's created at the preprocess in the Executable PHP file. It includes process id and created timestamp. It is used to inspect background process statuses through BackgroundProcessManager Class API. At default, this file name is like the following

/tmp/php/background_process/process.2494951fd9d147bc3e.json

Executable PHP file and Json meta data file have same name without extentions.

These two files are automatically deleted at the end of the processing. Therefore, you generally don't need to manipulate them.

Installation

User composer installation with below composer.json.

{
  "require": {
    "kohkimakimoto/background-process": "1.1.*"
  }
}

And runs Composer install command.

$ curl -s http://getcomposer.org/installer | php
$ php composer.phar install

Usage

The following code runs a command on the background.

use Kohkimakimoto\BackgroundProcess\BackgroundProcess;

// Creates instance and set command string to run at the background.
$process = new BackgroundProcess("ls -l > /tmp/test.txt");
// Runs command, and it returns immediately.
$process->run();

// Get key identified the process.
$key = $process->getKey();

The following code inspects the background process.

use Kohkimakimoto\BackgroundProcess\BackgroundProcessManager;

$manager = new BackgroundProcessManager();
$process = $manager->loadProcess($key);

// If a process specified by the key dosen't exist, loadProcess method returns null.
if (!$process) {
  echo "Not working process $key";
} else {
  $meta = $process->getMeta();
  echo $meta['created_at'];   // (ex 2013-01-01 10:00:20
  echo $meta['pid'];          // (ex 1234
}

If your command raises error, BackgroundProcess writes logs to error log file.

/tmp/php/background_process/err.log

Configuration Options

You can use configuration options to change behavior of BackgroundProcess.

use Kohkimakimoto\BackgroundProcess\BackgroundProcess;

// Creates instance and set command string to run at the background.
$process = new BackgroundProcess("ls -l > /tmp/test.txt", array(
  'working_directory' => '/path/to/background_process_directroy',
  'key_prefix'        => 'prefix_of_key.',
  'error_log'         => 'your_error.log',
));

License

Apache License 2.0

backgroundprocess's People

Contributors

garagesocial avatar kawahara avatar kohkimakimoto 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.