Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/n0nag0n/simple-job-queue
A simple library for interfacing with other job queue providers that gives you plenty of flexibility. Currently supports MySQL, SQLite, and Beanstalkd.
https://github.com/n0nag0n/simple-job-queue
beanstalk beanstalk-worker beanstalkd job-queue mariadb mysql php php-library sqlite
Last synced: about 1 month ago
JSON representation
A simple library for interfacing with other job queue providers that gives you plenty of flexibility. Currently supports MySQL, SQLite, and Beanstalkd.
- Host: GitHub
- URL: https://github.com/n0nag0n/simple-job-queue
- Owner: n0nag0n
- Created: 2020-08-24T19:17:19.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-06-14T15:12:21.000Z (6 months ago)
- Last Synced: 2024-11-03T13:06:05.999Z (about 2 months ago)
- Topics: beanstalk, beanstalk-worker, beanstalkd, job-queue, mariadb, mysql, php, php-library, sqlite
- Language: PHP
- Homepage:
- Size: 60.5 KB
- Stars: 26
- Watchers: 2
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Simple PHP Job Queue
I wanted/needed a simple job queue that could be used on a database, but also used with other adapters like beanstalkd/redis/etc if needed. Didn't really see a good option for a standalone job queue for a database.## Install
```bash
composer require n0nag0n/simple-job-queue
```## Usage
### Adding a new job
#### MySQL
```php
[
'table_name' => 'new_table_name', // default is job_queue_jobs
'use_compression' => false // default is true to use COMPRESS() and UNCOMPRESS() for payload
]
]);$PDO = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'user', 'pass');
$Job_Queue->addQueueConnection($PDO);$Job_Queue->selectPipeline('send_important_emails');
$Job_Queue->addJob(json_encode([ 'something' => 'that', 'ends' => 'up', 'a' => 'string' ]));
```#### PostgreSQL
```php
[
'table_name' => 'new_table_name', // default is job_queue_jobs
]
]);$PDO = new PDO('pgsql:dbname=testdb;host=127.0.0.1', 'user', 'pass');
$Job_Queue->addQueueConnection($PDO);$Job_Queue->selectPipeline('send_important_emails');
$Job_Queue->addJob(json_encode([ 'something' => 'that', 'ends' => 'up', 'a' => 'string' ]));
```#### SQLite3
```php
[
'table_name' => 'new_table_name' // default is job_queue_jobs
]
]);$PDO = new PDO('sqlite:example.db');
$Job_Queue->addQueueConnection($PDO);$Job_Queue->selectPipeline('send_important_emails');
$Job_Queue->addJob(json_encode([ 'something' => 'that', 'ends' => 'up', 'a' => 'string' ]));
```#### Beanstalkd
```php
addQueueConnection($pheanstalk);$Job_Queue->selectPipeline('send_important_emails');
$Job_Queue->addJob(json_encode([ 'something' => 'that', 'ends' => 'up', 'a' => 'string' ]));
```### Running a worker
See `example_worker.php` for file or see below:
```php
addQueueConnection($PDO);
$Job_Queue->watchPipeline('send_important_emails');
while(true) {
$job = $Job_Queue->getNextJobAndReserve();// adjust to whatever makes you sleep better at night (for database queues only, beanstalkd does not need this if statement)
if(empty($job)) {
usleep(500000);
continue;
}echo "Processing {$job['id']}\n";
$payload = json_decode($job['payload'], true);try {
$result = doSomethingThatDoesSomething($payload);if($result === true) {
$Job_Queue->deleteJob($job);
} else {
// this takes it out of the ready queue and puts it in another queue that can be picked up and "kicked" later.
$Job_Queue->buryJob($job);
}
} catch(Exception $e) {
$Job_Queue->buryJob($job);
}
}
```### Handling long processes
Supervisord is going to be your jam. Look up the many, many articles on how to implement this.### Testing
PHPUnit Tests with sqlite3 examples for the time being.
```
vendor/bin/phpunit
```