Skip to content
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
22 changes: 22 additions & 0 deletions src/Attributes/Queueable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Doppar\Queue\Attributes;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
class Queueable
{
/**
* @param int|null $tries
* @param int|null $retryAfter
* @param int|null $delayFor
* @param string|null $onQueue
*/
public function __construct(
public ?int $tries = null,
public ?int $retryAfter = null,
public ?int $delayFor = null,
public ?string $onQueue = null
) {}
}
107 changes: 0 additions & 107 deletions src/Commands/MakeJobCommand.php

This file was deleted.

52 changes: 52 additions & 0 deletions src/Dispatchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Doppar\Queue;

trait Dispatchable
{
/**
* Dispatch the job with the given arguments.
*
* @param mixed ...$args
* @return string|null Job ID if queued, null if executed immediately
*/
public static function dispatchWith(...$args): ?string
{
return (new static(...$args))->dispatch();
}

/**
* Dispatch the job synchronously with the given arguments.
*
* @param mixed ...$args
* @return void
*/
public static function queueAsSync(...$args): void
{
(new static(...$args))->handle();
}

/**
* Dispatch the job after a delay with the given arguments.
*
* @param int $delay
* @param mixed ...$args
* @return string Job ID
*/
public static function queueAfter(int $delay, ...$args): string
{
return (new static(...$args))->delayFor($delay)->forceQueue();
}

/**
* Dispatch the job to a specific queue with the given arguments.
*
* @param string $queue
* @param mixed ...$args
* @return string Job ID
*/
public static function queueOn(string $queue, ...$args): string
{
return (new static(...$args))->onQueue($queue)->forceQueue();
}
}
52 changes: 52 additions & 0 deletions src/InteractsWithQueueableAttributes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Doppar\Queue;

use Doppar\Queue\Attributes\Queueable;

trait InteractsWithQueueableAttributes
{
/**
* Check if the job should be queued based on the Queueable attribute.
*
* @return bool
*/
public function shouldQueue(): bool
{
$reflection = new \ReflectionClass($this);
$attributes = $reflection->getAttributes(Queueable::class);

return !empty($attributes);
}

/**
* Apply the Queueable attribute settings to the job.
*
* @return void
*/
protected function applyQueueableAttributes(): void
{
$reflection = new \ReflectionClass($this);
$attributes = $reflection->getAttributes(Queueable::class);

if (!empty($attributes)) {
$attribute = $attributes[0]->newInstance();

if ($attribute->tries !== null) {
$this->tries = $attribute->tries;
}

if ($attribute->retryAfter !== null) {
$this->retryAfter = $attribute->retryAfter;
}

if ($attribute->delayFor !== null) {
$this->jobDelay = $attribute->delayFor;
}

if ($attribute->onQueue !== null) {
$this->queueName = $attribute->onQueue;
}
}
}
}
45 changes: 42 additions & 3 deletions src/Job.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

abstract class Job implements JobInterface
{
use InteractsWithQueueableAttributes;

/**
* The number of times the job may be attempted.
*
Expand Down Expand Up @@ -150,11 +152,19 @@ public function delayFor(int $delay): self
/**
* Dispatch the job to the queue.
*
* @return string Job ID
* @return string|null
*/
public function dispatch(): string
public function dispatch(): ?string
{
return Queue::push($this);
$this->applyQueueableAttributes();

if ($this->shouldQueue()) {
return Queue::push($this);
}

$this->handle();

return null;
}

/**
Expand All @@ -167,6 +177,8 @@ public function dispatchAfter(int $delay): string
{
$this->delayFor($delay);

$this->applyQueueableAttributes();

return $this->dispatch();
}

Expand All @@ -180,6 +192,8 @@ public function dispatchOn(string $queue): string
{
$this->onQueue($queue);

$this->applyQueueableAttributes();

return $this->dispatch();
}

Expand All @@ -195,4 +209,29 @@ public static function dispatchNow(...$args): string

return $job->dispatch();
}

/**
* Dispatch the job synchronously
*
* @param mixed ...$args
* @return void
*/
public static function dispatchSync(...$args): void
{
$job = new static(...$args);

$job->handle();
}

/**
* Force the job to be queued even without Queueable attribute.
*
* @return string Job ID
*/
public function forceQueue(): string
{
$this->applyQueueableAttributes();

return Queue::push($this);
}
}
2 changes: 0 additions & 2 deletions src/QueueServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Doppar\Queue\Commands\QueueRetryCommand;
use Doppar\Queue\Commands\QueueFlushCommand;
use Doppar\Queue\Commands\QueueFailedCommand;
use Doppar\Queue\Commands\MakeJobCommand;
use Doppar\Queue\Commands\QueueMonitorCommand;

class QueueServiceProvider extends ServiceProvider
Expand Down Expand Up @@ -39,7 +38,6 @@ public function boot(): void
$this->commands([
QueueRunCommand::class,
QueueRetryCommand::class,
MakeJobCommand::class,
QueueFlushCommand::class,
QueueFailedCommand::class,
QueueMonitorCommand::class
Expand Down