Skip to content
This repository has been archived by the owner on May 1, 2022. It is now read-only.

Commit

Permalink
To version 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sebsel authored Jul 26, 2017
2 parents 57a7d43 + c728be5 commit 6f25f29
Show file tree
Hide file tree
Showing 9 changed files with 516 additions and 25 deletions.
65 changes: 55 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ You then need to add `site/plugins/queue-for-kirby/worker.php` to your [Cron Job

The plugin will try to create the folder `site/queue` and some files and folders within it.

## Widget

This plugin will add a widget to the panel dashboard if there are failed jobs, or if there are more than 5 jobs in the queue (indicating that there's something wrong).

<img src="queue-widget.jpg" alt="queue-widget" width="480px">

## How to define jobs

You need to define the following things within the base-file of your plugin, not in any lazy loaded classes (with Kirby's `load()` or composer's autoloading). Just put it in `site/plugins/your_plugin/your_plugin.php`.
Expand All @@ -26,12 +32,11 @@ queue::define('send_webmention', function($job) {
$target = $job->get('target');
$source = $job->get('source');

// Do something with your data, for example:
// send a webmention!
// Do something with your data, for example: send a webmention!
if(!send_webmention($target, $source)) {
// Throw an error to fail a job
throw new Error('Sending webmention failed');
// or just return false.
// or just return false, but setting a message for the user is better.
}

// No need to return or display anything else!
Expand Down Expand Up @@ -98,14 +103,14 @@ queue::add('another_job');

### queue::jobs()

Returns an array of Job objects, for all jobs in the queue.
Returns a [Collection](https://getkirby.com/docs/toolkit/api#collection) of Job objects, for all jobs in the queue.

Doing something with these jobs does **not** change the queue. Only `queue::work()` removes jobs from the queue.

```php
queue::jobs();
// Returns, for example:
[
object(Collection) {
object(Job) {
'id' => '5975f78ed3db6',
'added' => '2001-01-01T01:01:01+00:00',
Expand All @@ -120,17 +125,22 @@ queue::jobs();
'name' => 'another_job',
'data' => null
}
]
}

// and

queue::jobs()->first();
// returns the first Job
```

### queue::failedJobs()

Returns an array of Job objects, representing the failed jobs.
Returns a [Collection](https://getkirby.com/docs/toolkit/api#collection) of Job objects, representing the failed jobs.

```php
queue::jobs();
queue::failedJobs();
// Returns, for example:
[
object(Collection) {
object(Job) {
'id' => '5975f78ed3db6',
'added' => '2001-01-01T01:01:01+00:00',
Expand All @@ -141,7 +151,42 @@ queue::jobs();
'error' => 'Job returned false',
'tried' => '2001-01-01T01:01:03+00:00'
}
]
}

// and

queue::failedJobs()->last();
// returns the last failed Job
```

### queue::retry($failedJob)

Moves a failed job back in the queue. Use to trigger in a panel widget or after some other user input.

Note that this does not immediately act on the failed job. It is just added to the queue – probably at the front due to it's old ID – and gets handled as soon as your Cron Job executes `worker.php`.

```php
$failedJob = queue::failedJobs()->first();

queue::retry($failedJob);

// or

queue::retry('5975f78ed3db6');
```

### queue::remove($failedJob)

Removes a failed job entirely. Note that this only works for failed jobs.

```php
$failedJob = queue::failedJobs()->last();

queue::remove($failedJob);

// or

queue::remove('5975f78ed3db6');
```

### queue::work()
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"description": "Basic queue to the Kirby CMS, using Cron and Kirby's flat file system",
"author": "Sebastiaan Andeweg",
"license": "MIT",
"version": "1.0.2",
"version": "2.0.0",
"type": "kirby-plugin",
"files": [
"package.json",
"LICENSE.md",
"README.md",
"queue-for-kirby.php",
"worker.php"
"worker.php",
"panel/"
]
}
}
70 changes: 70 additions & 0 deletions panel/controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace Queue;

use Queue;
use Error;

class Controller extends \Kirby\Panel\Controllers\Base
{
public static function retry($id)
{
try {
queue::retry($id);

panel()->notify(':)');
} catch (Error $e) {
panel()->alert($e->getMessage());
}

panel()->redirect();
}

public static function remove($id)
{
try {
queue::remove($id);

panel()->notify(':)');
} catch (Error $e) {
panel()->alert($e->getMessage());
}

panel()->redirect();
}

public static function flush()
{
queue::flush();

panel()->notify(':)');

panel()->redirect();
}

public static function work()
{
$count_jobs = queue::jobs()->count();

while (queue::hasJobs()) {
queue::work();
}

$count_failed = queue::failedJobs()->count();

$message = 'Did ' . ($count_jobs == 1 ? '1 job' : $count_jobs . ' jobs');

if ($count_failed) {
$message .= ', ';
$message .= ($count_failed == 1 ? '1 job' : $count_failed . ' jobs');
$message .= ' failed';

panel()->alert($message);
} else {
panel()->notify($message);
}

panel()->redirect();
}
}

30 changes: 30 additions & 0 deletions panel/init.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

load([
'queue\\controller' => __DIR__ . DS . 'controller.php'
]);

$kirby->set('widget', 'failedjobs', __DIR__ . DS . 'widget');

panel()->routes([
[
'pattern' => 'queue/retry/(:any)',
'filter' => 'auth',
'action' => 'Queue\Controller::retry'
],
[
'pattern' => 'queue/remove/(:any)',
'filter' => 'auth',
'action' => 'Queue\Controller::remove'
],
[
'pattern' => 'queue/flush',
'filter' => 'auth',
'action' => 'Queue\Controller::flush'
],
[
'pattern' => 'queue/work',
'filter' => 'auth',
'action' => 'Queue\Controller::work'
]
]);
62 changes: 62 additions & 0 deletions panel/widget/failedjobs.html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<style>
.btn-small{
padding: .2em .75em;
display: block;
margin-bottom: .2em;
}

.buttons {
position: absolute;
top: 0;
right: .3em;
}

.normal-white-space {
white-space: normal;
position: relative;
}
</style>

<div class="dashboard-box">
<ul class="dashboard-items">

<?php foreach ($failed as $job): ?>

<li class="dashboard-item">
<figure class="normal-white-space">
<figcaption class="dashboard-item-text " style="
width: calc(100% - 3em);
padding-top: 0.5em;
">
<strong><?= $job->name() ?></strong><br>
<em><?= $job->error() ?></em>
</figcaption>

<div class="buttons">
<a class="btn btn-rounded btn-small"
href="<?=url('/panel/queue/retry/'.$job->id()) ?>" target>
<i class="icon fa fa-refresh"></i>
</a>

<a class="btn btn-rounded btn-negative btn-small"
href="<?=url('/panel/queue/remove/'.$job->id()) ?>" target>
<i class="icon fa fa-remove"></i>
</a>
</div>
</figure>



<div class="dashboard-item-text" style="
margin: 0.75em;
">
<?php if (is_array($job->data())) foreach ($job->data() as $key => $value): ?>
<strong><?= $key ?></strong>:&nbsp;<code><?=$value?></code><br>
<?php endforeach ?>

</div>
</li>
<?php endforeach ?>

</ul>
</div>
40 changes: 40 additions & 0 deletions panel/widget/failedjobs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

if(!panel()->user()->isAdmin()) return false;

$jobs_count = queue::jobs()->count();

// Don't show the Widget if there are no failed jobs,
// but DO display it if there are more than 5 jobs piled up
// to remind the user there is a queue at all
if(!queue::failedJobs()->count() and $jobs_count < 5) return false;

return
[
'title' => [
'text' => 'Failed jobs',
'compressed' => false
],
'options' => [
[
'text' =>
($jobs_count == 1 ? '1 job' : $jobs_count . ' jobs')
. ' waiting',
'icon' => 'square-o',
'link' => 'queue/work'
],
[
'text' => 'Flush all',
'icon' => 'trash-o',
'link' => 'queue/flush'
]

],
'html' => function() {
$failed = queue::failedJobs();

if (!$failed->count()) return 'No failed jobs.';

return tpl::load(__DIR__ . DS . 'failedjobs.html.php', compact('failed'));
}
];
Loading

0 comments on commit 6f25f29

Please sign in to comment.