Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Queue Spec Laravel Package

queue-interop/queue-spec

Interoperability specification for PHP queue implementations. Defines common interfaces and contracts so different queue libraries and transports can work together with minimal coupling, enabling portable producers/consumers and easier swapping of backends.

Deep Wiki
Context7

Getting Started

Minimal Steps to Begin

  1. Installation Add the package via Composer:

    composer require queue-interop/queue-spec
    

    This package is primarily a specification (not an implementation), so no direct Laravel integration is needed. Instead, use it to validate or test your queue system.

  2. Understand the Purpose The queue-spec package defines interfaces and behaviors for queue systems (e.g., Queue, Context, Message). It’s useful for:

    • Writing unit tests to ensure your queue implementation adheres to standards.
    • Validating third-party queue drivers (e.g., custom Redis/SQS adapters).
    • Building abstraction layers for queue operations.
  3. First Use Case: Testing a Custom Queue Driver Suppose you’re building a custom queue driver for Laravel. Use queue-spec to test it:

    use QueueInterop\Queue;
    use QueueInterop\Message;
    use QueueInterop\Context;
    
    // Pseudocode: Validate your driver against the spec
    $queue = new YourCustomQueueDriver(); // Your Laravel queue adapter
    $context = new YourContext(); // Implement Context interface
    
    assert($queue instanceof Queue);
    assert($context instanceof Context);
    

Implementation Patterns

1. Laravel Queue Integration

While queue-spec doesn’t integrate directly with Laravel, you can use it to:

  • Test Laravel’s built-in queue drivers (e.g., DatabaseQueue, RedisQueue) against the spec.
  • Build a wrapper that enforces the spec for consistency:
    namespace App\Queues;
    
    use Illuminate\Queue\Queue as LaravelQueue;
    use QueueInterop\Queue;
    
    class SpecCompliantQueue implements Queue {
        protected $laravelQueue;
    
        public function __construct(LaravelQueue $queue) {
            $this->laravelQueue = $queue;
        }
    
        public function createContext() {
            return new SpecContext($this->laravelQueue);
        }
    
        // Delegate other methods to Laravel's queue...
    }
    

2. Workflow: Processing Messages

Use the spec to define how messages are handled:

// Example: Using the spec to validate message processing
$message = new YourMessage(); // Implement QueueInterop\Message
$context = $queue->createContext();

$context->createConsumer()
    ->consume($message)
    ->acknowledge();

3. Integration with Laravel Jobs

Extend Laravel’s ShouldQueue interface to comply with queue-spec:

use QueueInterop\Message;

class YourJob implements ShouldQueue {
    public function toQueueInteropMessage(): Message {
        return new YourJobMessage(
            $this->handle(),
            $this->data
        );
    }
}

4. Testing Queue Middleware

Validate middleware behavior against the spec:

public function testMiddlewareCompliance() {
    $queue = new YourQueueWithMiddleware();
    $context = $queue->createContext();

    $this->assertInstanceOf(
        QueueInterop\Context::class,
        $context,
        "Middleware must not break Context compliance."
    );
}

Gotchas and Tips

Pitfalls

  1. Not a Runtime Dependency

    • This is a specification-only package. If you’re not writing tests or abstractions, it’s useless in production.
    • Avoid using it to "fix" Laravel’s queue system—it’s for validation, not replacement.
  2. Laravel’s Queue System Already Works

    • Laravel’s queues already follow similar patterns. Over-engineering with this spec may add unnecessary complexity.
    • Example: Laravel’s Illuminate\Queue\Queue doesn’t natively implement QueueInterop\Queue, so you’ll need a wrapper.
  3. Message Serialization Mismatches

    • The spec expects messages to be serializable/deserializable. Laravel’s jobs use serialize() by default, which may not align with queue-spec expectations.
    • Fix: Implement QueueInterop\Message with Laravel-compatible serialization:
      class LaravelJobMessage implements Message {
          public function __construct(private array $data) {}
      
          public function getBody() {
              return serialize($this->data);
          }
      
          public static function fromBody(string $body) {
              return new self(unserialize($body));
          }
      }
      

Debugging Tips

  1. Check Interface Compliance Use instanceof checks to verify implementations:

    $this->assertInstanceOf(
        QueueInterop\Queue::class,
        $yourQueue,
        "Your queue must implement QueueInterop\Queue."
    );
    
  2. Mock Context for Testing Laravel’s Queue doesn’t provide a Context, so mock it for testing:

    $mockContext = $this->createMock(Context::class);
    $mockContext->method('createConsumer')->willReturn(new YourConsumer());
    
  3. Handle Exceptions The spec defines QueueException. Ensure your Laravel queue throws compatible exceptions:

    try {
        $queue->createContext();
    } catch (QueueInterop\QueueException $e) {
        // Handle spec-compliant errors
    }
    

Extension Points

  1. Custom Message Classes Extend QueueInterop\Message to add Laravel-specific metadata (e.g., job ID, retry count):

    class LaravelMessage implements Message {
        public function getJobId(): ?string {
            return $this->data['job_id'] ?? null;
        }
    }
    
  2. Queue Adapter Wrapper Create a Laravel service provider to auto-wrap queues:

    // config/queue.php
    'spec_compliant' => [
        'driver' => 'database',
        'wrapper' => App\Queues\SpecCompliantQueue::class,
    ];
    
  3. Testing Helpers Build a trait for Laravel tests:

    trait QueueSpecTests {
        protected function assertQueueCompliant($queue) {
            $this->assertInstanceOf(Queue::class, $queue);
            $this->assertInstanceOf(Context::class, $queue->createContext());
        }
    }
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport