Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mle86/php-wq
A simple PHP library for work-queue and job handling.
https://github.com/mle86/php-wq
php-library php7 queued-jobs work-queue wq
Last synced: about 2 months ago
JSON representation
A simple PHP library for work-queue and job handling.
- Host: GitHub
- URL: https://github.com/mle86/php-wq
- Owner: mle86
- License: mit
- Created: 2017-04-19T21:32:33.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2022-05-24T09:38:07.000Z (over 2 years ago)
- Last Synced: 2024-10-26T13:42:20.074Z (2 months ago)
- Topics: php-library, php7, queued-jobs, work-queue, wq
- Language: PHP
- Size: 274 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# WQ (`mle86/wq`)
[![Build Status](https://travis-ci.org/mle86/php-wq.svg?branch=master)](https://travis-ci.org/mle86/php-wq)
[![Coverage Status](https://coveralls.io/repos/github/mle86/php-wq/badge.svg?branch=master)](https://coveralls.io/github/mle86/php-wq?branch=master)
[![Latest Stable Version](https://poser.pugx.org/mle86/wq/version)](https://packagist.org/packages/mle86/wq)
[![PHP 8](https://img.shields.io/badge/php-8-8892BF.svg?style=flat)](https://php.net/)
[![License](https://poser.pugx.org/mle86/wq/license)](https://packagist.org/packages/mle86/wq)This package provides an easy way
to put PHP tasks of any kind
into a work queue
such as Beanstalkd or Redis
to execute them at a later time.This is
**version 0.21.1**.# Installation and Dependencies
```
$ composer require mle86/wq
```It requires PHP 8.0+
and has no other dependencies
(apart from PHPUnit/Coveralls for development
and the PSR-3 interfaces).## Adapter Implementations
You'll also want to install at least one other package
which contains a `WorkServerAdapter` implementation,
such as:* [mle86/wq-beanstalkd](https://github.com/mle86/php-wq-beanstalkd)
(Beanstalkd server adapter),
* [mle86/wq-redis](https://github.com/mle86/php-wq-redis)
(Redis server adapter),
* [mle86/wq-amqp](https://github.com/mle86/php-wq-amqp)
(RabbitMQ server adapter).# Basic Concepts
- A *Job* is something which should be done exactly once.
Maybe it's sending an e-mail,
maybe it's an external API call like a webhook,
maybe it's some slow clean-up process.
In any case, we're talking about a unit of work
that could be executed right away
but it would be better for the application's performance
to put it in a *Work Queue* instead, so it can be done asynchronously.- A *Work Queue* is a list of jobs that should be executed at some other time.
They are stored in some kind of *Work Server*.
One work server well-known in the PHP world is [Beanstalkd](http://kr.github.io/beanstalkd/).
It can store any number of work queues, although it calls them “tubes”.Different work queues, or tubes, are commonly used to separate job types.
For example, the same work server might have
one “`mail`” queue for outgoing mails to be sent,
one “`cleanup`” queue for all kinds of clean-up jobs,
and one “`webhook`” queue for outgoing web-hook calls.This package provides some helpful classes
to set up a simple work queue system.# Quick Start
This is our Job implementation.
It represents an e-mail that can be sent.```php
recipient = $recipient;
$this->subject = $subject;
$this->message = $message;
}
public function send()
{
if (mail($this->recipient, $this->subject, $this->message)) {
// ok, has been sent!
} else {
throw new \RuntimeException ("mail() failed");
}
}
}
```We have some code using that e-mail class:
```php
storeJob("mail", $mailJob);
```And finally,
we have our background worker script
which regularly checks the work server
for new e-mail jobs:```php
send();
// don't catch exceptions here, or the WorkProcessor won't see them.
};while (true) {
try {
$processor->processNextJob($queue, $fn_handler);
} catch (\Throwable $e) {
echo $e . "\n"; // TODO: add some real logging here
}
}
```# Documentation
1. [Implementing a Job class]
1. [Execute or Queue]
1. [Work the Queue]
1. [Error Handling]
1. [Usage Examples]Class reference:
* [Job] interface
* [AbstractJob] base class
* [QueueEntry] wrapper class
* [WorkServerAdapter] interface
* [AffixAdapter] wrapper class
* [BlackHoleWorkServer] dummy class
* [WorkProcessor] class
* [SignalSafeWorkProcessor] class
* [JobResult] enum class
* [JobContext] dto class
* [Exceptions](doc/Ref_Exceptions.md)[Job]: doc/Ref_Job_interface.md
[AbstractJob]: doc/Ref_AbstractJob_base_class.md
[WorkServerAdapter]: doc/Ref_WorkServerAdapter_interface.md
[AffixAdapter]: doc/Ref_AffixAdapter_class.md
[BlackHoleWorkServer]: doc/Ref_BlackHoleWorkServer_class.md
[WorkProcessor]: doc/Ref_WorkProcessor_class.md
[QueueEntry]: doc/Ref_QueueEntry_class.md
[JobResult]: doc/Ref_JobResult_class.md
[JobContext]: doc/Ref_JobContext_class.md[Implementing a Job class]: doc/Implementing_a_Job_class.md
[Execute or Queue]: doc/Execute_or_Queue.md
[Work the Queue]: doc/Work_the_Queue.md
[Error Handling]: doc/Error_Handling.md
[Usage Examples]: doc/Usage_Examples.md
[SignalSafeWorkProcessor]: doc/Ref_SignalSafeWorkProcessor_class.md