Skip to content
This repository was archived by the owner on Nov 5, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
# Laravel 5.3 Async Queue Driver
# Laravel 5.4 Async Queue Driver

## Push a function/closure to the background.


### For Laravel 5.3, check the [0.5 branch](https://github.com/barryvdh/laravel-async-queue/tree/0.4)

Just like the 'sync' driver, this is not a real queue driver. It is always fired immediatly.
The only difference is that the closure is sent to the background without waiting for the response.
This package is more usable as an alternative for running incidental tasks in the background, without setting up a 'real' queue driver.
Expand Down Expand Up @@ -31,7 +34,7 @@ You should now be able to use the async driver in config/queue.php. Use the same
...
'async' => array(
'driver' => 'async',
'table' => 'queue_jobs',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
),
Expand All @@ -44,7 +47,7 @@ By default, `php` is used as the binary path to PHP. You can change this by addi
...
'async' => array(
'driver' => 'async',
'table' => 'queue_jobs',
'table' => 'jobs',
'queue' => 'default',
'expire' => 60,
'binary' => 'php',
Expand Down
88 changes: 44 additions & 44 deletions src/AsyncQueue.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php
namespace Barryvdh\Queue;

use Carbon\Carbon;
use DateTime;
use Illuminate\Database\Connection;
use Illuminate\Queue\DatabaseQueue;
use Illuminate\Queue\Jobs\DatabaseJob;
Expand All @@ -21,9 +19,9 @@ class AsyncQueue extends DatabaseQueue

/**
* @param \Illuminate\Database\Connection $database
* @param string $table
* @param string $default
* @param int $expire
* @param string $table
* @param string $default
* @param int $expire
* @param string $binary
* @param string|array $binaryArgs
*/
Expand Down Expand Up @@ -53,20 +51,20 @@ public function push($job, $data = '', $queue = null)
}

/**
* Push a raw payload onto the queue.
*
* @param string $payload
* @param string $queue
* @param array $options
* @return mixed
*/
public function pushRaw($payload, $queue = null, array $options = array())
{
$id = parent::pushRaw($payload, $queue, $options);
* Push a raw payload onto the queue.
*
* @param string $payload
* @param string $queue
* @param array $options
* @return mixed
*/
public function pushRaw($payload, $queue = null, array $options = array())
{
$id = parent::pushRaw($payload, $queue, $options);
$this->startProcess($id);

return $id;
}
}

/**
* Push a new job onto the queue after a delay.
Expand All @@ -86,40 +84,42 @@ public function later($delay, $job, $data = '', $queue = null)
return $id;
}

protected function pushToDatabase($delay, $queue, $payload, $attempts = 0)
{
$availableAt = $delay instanceof DateTime ? $delay : Carbon::now()->addSeconds($delay);

return $this->database->table($this->table)->insertGetId([
'queue' => $this->getQueue($queue),
'payload' => $payload,
'attempts' => $attempts,
'reserved' => 1,
'reserved_at' => $this->getTime(),
'available_at' => $availableAt->getTimestamp(),
'created_at' => $this->getTime(),
]);
}
/**
* Create an array to insert for the given job.
*
* @param string|null $queue
* @param string $payload
* @param int $availableAt
* @param int $attempts
* @return array
*/
protected function buildDatabaseRecord($queue, $payload, $availableAt, $attempts = 0)
{
$record = parent::buildDatabaseRecord($queue, $payload, $availableAt, $attempts);
$record['reserved_at'] = $this->currentTime();

return $record;
}

/**
* Get the next available job for the queue.
*
* @param string|null $queue
* @return \StdClass|null
*/
public function getJobFromId($id)
{
$job = $this->database->table($this->table)
->where('id', $id)
->first();
* Get the next available job for the queue.
*
* @param string|null $queue
* @return \StdClass|null
*/
public function getJobFromId($id)
{
$job = $this->database->table($this->table)
->where('id', $id)
->first();

if($job) {

return new DatabaseJob(
$this->container, $this, $job, $job->queue
);
return new DatabaseJob(
$this->container, $this, $job, $job->queue
);
}
}
}

/**
* Make a Process for the Artisan command for the job id.
Expand Down