Skip to content

Commit

Permalink
fixed the readme and example worker file.
Browse files Browse the repository at this point in the history
  • Loading branch information
n0nag0n committed Sep 28, 2020
1 parent 76d6270 commit c1c2147
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
43 changes: 32 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Simple PHP Job Queue
Here's the basics (will fill out later......ha!)
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#
## Usage
### Adding a new job
#### MySQL
```php
Expand All @@ -23,7 +23,7 @@ $Job_Queue = new Job_Queue('mysql', [
]);

$PDO = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'user', 'pass');
$Job_Queue->addDbConnection($PDO);
$Job_Queue->addQueueConnection($PDO);

$Job_Queue->selectPipeline('send_important_emails');
$Job_Queue->addJob(json_encode([ 'something' => 'that', 'ends' => 'up', 'a' => 'string' ]));
Expand All @@ -43,7 +43,23 @@ $Job_Queue = new Job_Queue('sqlite', [
]);

$PDO = new PDO('sqlite:example.db');
$Job_Queue->addDbConnection($PDO);
$Job_Queue->addQueueConnection($PDO);

$Job_Queue->selectPipeline('send_important_emails');
$Job_Queue->addJob(json_encode([ 'something' => 'that', 'ends' => 'up', 'a' => 'string' ]));
```

#### Beanstalkd
```php
<?php

use n0nag0n\Job_Queue

// default is mysql based job queue
$Job_Queue = new Job_Queue('beanstalkd');

$pheanstalk = Pheanstalk\Pheanstalk::create('127.0.0.1');
$Job_Queue->addQueueConnection($pheanstalk);

$Job_Queue->selectPipeline('send_important_emails');
$Job_Queue->addJob(json_encode([ 'something' => 'that', 'ends' => 'up', 'a' => 'string' ]));
Expand All @@ -53,12 +69,12 @@ $Job_Queue->addJob(json_encode([ 'something' => 'that', 'ends' => 'up', 'a' => '
See `example_worker.php` for file or see below:
```php
<?php
$Job_Queue = new Job_Queue('mysql');
$Job_Queue->addDbConnection($f3->db);
$Job_Queue = $f3->job_queue;
$Job_Queue = new n0nag0n\Job_Queue('mysql');
$PDO = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'user', 'pass');
$Job_Queue->addQueueConnection($PDO);
$Job_Queue->watchPipeline('some_cool_pipeline_name');
while(true) {
$job = $Job_Queue->getNextJob();
$job = $Job_Queue->getNextJobAndReserve();
if(empty($job)) {
// adjust to whatever makes you sleep better at night
usleep(500000);
Expand All @@ -67,15 +83,14 @@ See `example_worker.php` for file or see below:

echo "Processing {$job['id']}\n";
$payload = json_decode($job['payload'], true);
$new_payload = $payload;
unset($new_payload['webhook_url']);

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) {
Expand All @@ -85,4 +100,10 @@ See `example_worker.php` for file or see below:
```

### Handling long processes
Supervisord is going to be your jam. Look up the many, many articles on how to implement this.
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
```
11 changes: 5 additions & 6 deletions example_worker.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
$Job_Queue = new Job_Queue('mysql');
$Job_Queue->addDbConnection($f3->db);
$Job_Queue = $f3->job_queue;
$Job_Queue = new n0nag0n\Job_Queue('mysql');
$PDO = new PDO('mysql:dbname=testdb;host=127.0.0.1', 'user', 'pass');
$Job_Queue->addQueueConnection($PDO);
$Job_Queue->watchPipeline('some_cool_pipeline_name');
while(true) {
$job = $Job_Queue->getNextJob();
$job = $Job_Queue->getNextJobAndReserve();
if(empty($job)) {
// adjust to whatever makes you sleep better at night
usleep(500000);
Expand All @@ -13,15 +13,14 @@

echo "Processing {$job['id']}\n";
$payload = json_decode($job['payload'], true);
$new_payload = $payload;
unset($new_payload['webhook_url']);

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) {
Expand Down

0 comments on commit c1c2147

Please sign in to comment.