• Stars
    star
    114
  • Rank 306,306 (Top 7 %)
  • Language
    PHP
  • License
    MIT License
  • Created almost 10 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

[Deprecated - This project is no longer maintained] A PHP micro job scheduler framework like cron.

WorkerPHP

Build Status Latest Stable Version License

A PHP micro job scheduler framework like cron.

<?php
require_once __DIR__.'/vendor/autoload.php';

$worker = new \Kohkimakimoto\Worker\Worker();

// job for every minute.
$worker->job("hello", ['cron_time' => '* * * * *', 'command' => function(){
    echo "Hello world\n";
}]);

// job runs a shell command.
$worker->job("uptime", ['cron_time' => '10 * * * *', 'command' => "uptime"]);

$worker->start();

Requirements

Installation

Create composer.json for installing via composer.

{
    "require": {
        "kohkimakimoto/workerphp": "0.*"
    }
}

Run composer install command.

$ composer install

Usage

Bootstrap

To make a job scheduler application like cron, create worker.php file (or other name you want). You need to load composer autoload.php file and create an instance of Kohkimakimoto\Worker\Worker.

// worker.php
<?php
require_once __DIR__.'/vendor/autoload.php';

$worker = new \Kohkimakimoto\Worker\Worker();

// ... job definitions

$worker->start();

Run php worker.php. You will get the following messages and the process keep in your system. But it is not useful at this time. Stop the process using CONTROL-C.

$ php worker.php
Starting WorkerPHP.
Successfully booted. Quit working with CONTROL-C.

Learn about jobs at the next section.

Jobs

Define a job.

$worker->job("hello", ['cron_time' => '* * * * *', 'command' => function(){
    echo "Hello world\n";
}]);

This $worker->job method has two arguments. The first argument is name of job. It must be unique in all jobs. The second argument is an array that has some parameters. cron_time is a schedule when to run the job. It is a "cron expressions" string. command is a closure that is executed by the worker.

You can run it. You will get messages like the below.

$ php worker.php
Starting WorkerPHP.
Initializing job: hello (job_id: 0)
Successfully booted. Quit working with CONTROL-C.
Running job: hello (pid: 36643) at 2014-12-08 14:56:00
Hello world
Finished job: hello (pid: 36643) at 2014-12-08 14:56:00
Running job: hello (pid: 36646) at 2014-12-08 14:57:00
Hello world
Finished job: hello (pid: 36646) at 2014-12-08 14:57:00
Running job: hello (pid: 36647) at 2014-12-08 14:58:00
Hello world
Finished job: hello (pid: 36647) at 2014-12-08 14:58:00

The job you defined runs every minute.

Also you can define command a command string not a closure.

$worker->job("uptime", ['cron_time' => '* * * * *', 'command' => "uptime"]);

The worker runs command uptime every minute.

Running job: uptime (pid: 36650) at 2014-12-04 12:37:00
12:37  up 8 days, 16:06, 6 users, load averages: 1.82 1.74 1.83
Finished job: uptime (pid: 36650) at 2014-12-04 12:37:00

You can set a limit on running processes at the same time. Use max_processes.

$worker->job("hello", ['cron_time' => '* * * * *', 'max_processes' => 1, 'command' => function(){
    echo "Hello world\n";
    sleep(70);
;}]);
$ php worker.php
...
Runs job: hello (pid: 90621) at 2014-12-16 08:03:00
Hello world
Skip the job 'hello' due to limit of max processes: 1 at 2014-12-16 08:04:00

Http Server (Web APIs)

WorkerPHP has a built-in http server. It provides APIs that controls jobs using HTTP requests. Write the following code.

$worker = new \Kohkimakimoto\Worker\Worker();
$worker->httpServer->listen();

// ...

$worker->start();

When WorkerPHP starts, It listens port 8080 (default). You can modify listened port and host.

$worker->httpServer->listen(8888, 'localhost');

Get a worker info

If you started http server, you can get worker infomation using http request.

$ curl -XGET http://localhost:8080/?pretty=1

You will get below json.

{
    "name": "WorkerPHP",
    "number_of_jobs": 2,
    "jobs": [
        {
            "id": 0,
            "name": "hello",
            "max_processes": 1,
            "last_runtime": "2014-12-15 15:55:38",
            "next_runtime": "2014-12-15 15:56:00",
            "arguments": []
        },
        {
            "id": 1,
            "name": "uptime",
            "max_processes": 1,
            "last_runtime": "2014-12-15 15:55:38",
            "next_runtime": "2014-12-15 15:56:00",
            "arguments": []
        }
    ]
}

Get a job info

You can get a job info by specifing job name.

$ curl -XGET http://localhost:8080/hello?pretty=1

You will get below json.

{
    "number_of_running_jobs": 0
}

Run a job

You can run a job using POST request.

$ curl -XPOST http://localhost:8080/hello?pretty=1

You will get below json.

{
    "status": "OK"
}

Service Providers

ServiceProvider allow the user to extend WorkerPHP. Please see the built-in Service Provider StatsServiceProvider.

Author

Kohki Makimoto [email protected]

License

MIT license.

More Repositories

1

altax

[Deprecated - This project is no longer maintained] A deployment tool for PHP
PHP
202
star
2

hq

HQ is a simplistic, language agnostic job queue engine communicated by HTTP messages.
Go
67
star
3

essh

Extended ssh command.
Go
56
star
4

BackgroundProcess

[Deprecated - This project is no longer maintained] BackgroundProcess is a PHP library to run background processes asynchronously on your system.
PHP
36
star
5

cofu

Minimum configuration management tool written in Go.
Go
33
star
6

FMDBx

[Deprecated - This project is no longer maintained] An extension of FMDB to provide ORM and migration functionality.
Objective-C
20
star
7

EArray

EArray is a small PHP class to provide convenient ways to access a PHP Array.
PHP
18
star
8

phpmigrate

PHPMigrate is a minimum database migration tool for MySQL.
PHP
13
star
9

getcomposer.org_doc_jp

Composerのドキュメント日本語訳
HTML
12
star
10

lib-migration

LibMigration is a minimum database migration library and framework for MySQL.
PHP
12
star
11

chef-for-centos

A sample chef repository and documents to setup steps for CentOS6. You don't use chef-server. You only use chef-solo and git.
Ruby
11
star
12

LaravelAdminer

It's a embedded adminer in laravel application.
PHP
10
star
13

inertia-echo

The Inertia.js server-side adapter for Echo Go web framework.
Go
8
star
14

selfsigned-crt

A script to generate self-signed SSL certificate.
Shell
8
star
15

BayesClassifier

This is a Bayes Classifier implementation written in PHP
PHP
7
star
16

gluayaml

Yaml parser for gopher-lua
Go
7
star
17

bash-boilerplate

Bash script boilerplate.
Shell
6
star
18

IntelliJDictionaryPlugin

Integrates OSX Dictionary.app with IntelliJ IDEA.
Java
5
star
19

gluafs

Filesystem utility for gopher-lua.
Go
5
star
20

html2pdf

A CLI tool for generating PDF from HTML by using Lua script.
Go
5
star
21

php-jwt

A php extension to encode and decode JSON Web Tokens (JWT).
C
5
star
22

gluatemplate

Text template for gopher-lua
Go
5
star
23

jitome

Jitome is a simple file watcher.
Go
5
star
24

omnibus-supervisor

Creating full-stack platform-specific packages for supervisor (only RPM).
Ruby
5
star
25

adminer-cli

Command line interface to run adminer on the PHP built-in server.
PHP
4
star
26

LaravelValidatorExtension

An extension of validator for Laravel4.
PHP
4
star
27

atom-dictionary

Integrates OSX Dictionary.app with Atom.
CoffeeScript
4
star
28

graceful

Graceful shutdown and restart for Go
Go
4
star
29

minio-rpmbuild

Script for creating unofficial RPM package of minio.
Shell
3
star
30

StaticProxy

StaticProxy provides static interface to an instance method.
PHP
3
star
31

vscode-mac-dictionary

Integrates Mac Dictionary.app with vscode.
TypeScript
3
star
32

bucketstore

Bucketstore is a pure Go embedded database engine to store JSON structure data.
Go
3
star
33

buildsh

Several script files make it easy to run commands in an isolated docker container for building, testing and deploying softwares.
Python
3
star
34

atom-atomenv

Loads environment variables from .atomenv.json
CoffeeScript
3
star
35

gptx

An extensible command-line utility powered by ChatGPT, designed to enhance your productivity.
Go
3
star
36

phpwebadmin

PHPWebAdmin is a administration tools pot written in PHP.
JavaScript
2
star
37

assetic-extension

My Assetic extention filters
PHP
2
star
38

gluaenv

Utility package for manipulating environment variables for gopher-lua
Go
2
star
39

luster

A command line application framework based on Laravel.
PHP
2
star
40

altax-adminer

Adminer runs on the php built-in web server via altax.
PHP
2
star
41

lightqueue

LightQueue is a simple job queue engine written in PHP.
PHP
2
star
42

CloudSQLProxyMenuBar

CloudSQLProxyMenuBar displays a menu to manage Cloud SQL Proxy processes on macOS menu bar.
Go
2
star
43

pingcrm-echo

A demo application to illustrate how Inertia.js works with inertia-echo.
Go
2
star
44

yumrepo-git-update

Shell
2
star
45

altax-server

Running php built-in web server via altax.
PHP
2
star
46

chatgpt-prompt-snippets-chrome-extension

ChatGPT Prompt Snippets is a Google Chrome Extension that helps you manage and utilize prompt snippets conveniently when using ChatGPT.
TypeScript
2
star
47

SiteGenerator

A simple static site generator.
PHP
2
star
48

symfony-AsyncComponentHelper

This is a symfony helper to load contents on asynchronous in the view.
PHP
1
star
49

gopher-lua-playground

GopherLua Playground is a convenient website where you can play with GopherLua.
JavaScript
1
star
50

chef-cookbooks-selenium

Chef cookbook to install selenium server.
Ruby
1
star
51

sshdocker

Run docker containers over SSH.
Go
1
star
52

govalidator-report

Go
1
star
53

TravisSSHClientTest

Work repository to try testing SSH client.
1
star
54

exgearscms

ExGearsCMS is a content management system for Google App Engine/Java. Currently an unstable preview release.
Java
1
star
55

LaravelMessageBinder

A little hack for Illuminate View to access flash messages easily.
PHP
1
star
56

gluaquestion

A library of gopher-lua to prompt the user for input.
Go
1
star
57

docker-rpmbuild

1
star
58

ApplicationConfigServiceProvider

Silex ServiceProvider for configuration files
PHP
1
star
59

genhosts

genhosts - generate hosts file from /etc/hosts.d/*.conf
1
star
60

browser-qrcode-reader

This is an example web app to read QR code in browser.
TypeScript
1
star
61

crun

Crun (Command-RUN) is a command execution wrapper.
Go
1
star
62

loglv

A package supports leveled logging for Go standard log package.
Go
1
star
63

go-i18n

Go
1
star
64

execmysql

This is a very simple bash script to execute some sql files to mysql database and output results to text files.
Shell
1
star
65

Temporary

A PHP helper class to manipulate a temporary file and directory.
PHP
1
star
66

go-sqlexec

go-sqlexec is a library to execute SQLs from various sources such as files, directory and strings.
Go
1
star