got/stark

This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

A message queue-based PHP daemon

2.1.1 2020-11-27 09:10 UTC

This package is auto-updated.

Last update: 2021-09-27 10:55:58 UTC


README

Latest Stable Version Total Downloads License

Stark is a library for running php code as multi-process daemon.

Requires

  • PHP 5.4 or Higher
  • A POSIX compatible operating system (Linux, OSX, BSD)
  • POSIX and PCNTL extensions for PHP
  • Redis extensions

Features

  • Simple Callbacks
  • Message Queue Processing
  • Daemon Monitoring
  • Automatic Restart

Examples

Consumer Only

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

use Stark\DaemonFactory;
use Stark\Daemon\Consumer\AbstractConsumer;
use Stark\Daemon\Worker;

class MyConsumer extends AbstractConsumer {
    public function consume(Worker $worker, $data) {
        // ...
        return true;
    }
}

$daemon = DaemonFactory::consumerOnly(new MyConsumer());
$daemon->setWorkerCount(3);
$daemon->setMaxRunCount(50000);
$daemon->setPort(9101);
$daemon->setName("consumer-self");
$daemon->setWorkingDirectory("/tmp");
$daemon->start();

Consumer Redis Queue

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

use Stark\Daemon\Consumer\AbstractConsumer;
use Stark\DaemonFactory;
use Stark\Daemon\Worker;

class MyConsumer extends AbstractConsumer {
    public function consume(Worker $worker, $data) {
        if ($data == false) {
            return false;
        }
        
        //... $data from redis queue-0
        return true;
    }
}

$daemon = DaemonFactory::consumeRedis(new MyConsumer(), "127.0.0.1", "9004", "queue-0");
$daemon->setWorkerCount(3);
$daemon->setMaxRunCount(500000);
$daemon->setPort(9102);
$daemon->setName("consumer-redis");
$daemon->start();

Admin

# Status
redis-cli -h 127.0.0.1 -p 9102 info

# Shutdown
redis-cli -h 127.0.0.1 -p 9102 shutdown