VOOZH about

URL: https://deepwiki.com/hypervel/bus/5-job-traits

⇱ Job Traits | hypervel/bus | DeepWiki


Loading...
Menu

Job Traits

Purpose and Scope

This document provides an overview of the trait system in hypervel/bus. Jobs in this system gain their dispatching, queuing, and batching capabilities through three core traits: Dispatchable, Queueable, and Batchable. These traits provide fluent APIs for job configuration and enable jobs to integrate seamlessly with the dispatcher, queue system, and batch processing components.

For detailed documentation of individual traits, see:

For information on dispatching jobs using these traits, see Dispatching Jobs.

Trait Architecture Overview

The three traits in hypervel/bus are designed to be composable, allowing jobs to use any combination of them based on required functionality. Each trait focuses on a specific aspect of job behavior:


Figure 1: Trait Architecture and Integration Points

Sources: src/Dispatchable.php1-87 src/Queueable.php1-279 src/Batchable.php1-93

Trait Composition Patterns

Jobs typically use multiple traits together to gain comprehensive functionality. The most common composition patterns are:

PatternTraits UsedUse CaseExample
Synchronous JobDispatchable onlyJobs that execute immediately without queuingCommand handlers, simple operations
Queue JobDispatchable + QueueableAsynchronous jobs sent to queue systemEmail sending, data processing
Batch JobDispatchable + Queueable + BatchableJobs that participate in batch operationsBulk imports, parallel processing
Chained JobDispatchable + QueueableSequential job executionMulti-step workflows

Table 1: Common Trait Composition Patterns

Sources: src/Dispatchable.php1-87 src/Queueable.php1-279 src/Batchable.php1-93

Property and Method Structure

Each trait defines specific properties and methods that become part of the job class. Understanding this structure is essential for effective job configuration.


Figure 2: Complete Property and Method Structure Across Traits

Sources: src/Dispatchable.php16-86 src/Queueable.php20-278 src/Batchable.php15-92

Dispatchable Trait Structure

The Dispatchable trait provides static methods that serve as entry points for job dispatch:

MethodReturn TypePurpose
dispatch()PendingDispatchCreate new job instance and return pending dispatch object
dispatchIf()PendingDispatch|FluentConditionally dispatch based on boolean or closure
dispatchUnless()PendingDispatch|FluentDispatch unless condition is true
dispatchSync()mixedDispatch synchronously and return result
dispatchAfterResponse()mixedDispatch after HTTP response is sent
withChain()PendingChainCreate job with chained jobs

Table 2: Dispatchable Trait Methods

Sources: src/Dispatchable.php16-86

Queueable Trait Structure

The Queueable trait defines properties for queue configuration and methods for fluent configuration:

Properties:

Configuration Methods:

MethodParametersPurpose
onConnection()BackedEnum|string|nullSet job connection
onQueue()BackedEnum|string|nullSet job queue
allOnConnection()BackedEnum|string|nullSet connection for job and chain
allOnQueue()BackedEnum|string|nullSet queue for job and chain
delay()array|DateInterval|DateTimeInterface|int|nullSet job delay
withoutDelay()NoneClear delay (set to 0)
afterCommit()NoneWait for database commit
beforeCommit()NoneDispatch immediately
through()array|objectSet middleware

Table 3: Queueable Configuration Methods

Chain Management Methods:

MethodPurpose
chain()Set jobs that run after this job succeeds
prependToChain()Add job to beginning of chain
appendToChain()Add job to end of chain
dispatchNextJobInChain()Dispatch next job in sequence
invokeChainCatchCallbacks()Trigger failure callbacks

Table 4: Queueable Chain Management Methods

Sources: src/Queueable.php20-248

Batchable Trait Structure

The Batchable trait provides batch awareness and state access:

Properties:

Methods:

MethodReturn TypePurpose
batch()?BatchGet parent batch instance
batching()boolCheck if batch is active and not cancelled
withBatchId()staticSet batch ID
withFakeBatch()arrayConfigure fake batch for testing

Table 5: Batchable Trait Methods

Sources: src/Batchable.php15-92

Trait Interaction Flow

The following diagram illustrates how the three traits interact during a typical job lifecycle:


Figure 3: Trait Interaction During Job Lifecycle

Sources: src/Dispatchable.php19-22 src/Queueable.php70-121 src/Batchable.php28-51

Dependency Resolution and Container Integration

All three traits integrate with Hyperf's dependency injection container to resolve dependencies:


Figure 4: Container Integration in Traits

The Dispatchable trait resolves the Dispatcher from the container at src/Dispatchable.php67-69:


The Batchable trait resolves the BatchRepository from the container at src/Batchable.php35-37:


This design enables:

  • Late binding of dependencies
  • Testability through service substitution
  • Configuration-driven service resolution

Sources: src/Dispatchable.php65-69 src/Batchable.php34-38

Fluent API Design Pattern

The Queueable trait exemplifies the fluent API pattern used throughout hypervel/bus. All configuration methods return $this, enabling method chaining:


Figure 5: Fluent Method Chaining in Queueable Trait

Example method implementation at src/Queueable.php80-84:


This pattern allows expressive job configuration:


Sources: src/Queueable.php70-161

Chain Management Architecture

The Queueable trait implements sophisticated chain management through serialization and dynamic dispatch:


Figure 6: Chain Serialization and Execution Flow

The chain method at src/Queueable.php166-174 prepares nested batches and serializes jobs:


The dispatch method at src/Queueable.php224-237 handles chain continuation:


Sources: src/Queueable.php166-237

Testing Support

The traits include testing utilities to support unit and integration tests:

Queueable Testing Methods

The Queueable trait provides assertions at src/Queueable.php253-278:

MethodPurpose
assertHasChain()Verify job has expected chained jobs
assertDoesntHaveChain()Verify job has no chained jobs

Table 6: Queueable Testing Assertions

Batchable Testing Support

The Batchable trait provides fake batch creation at src/Batchable.php66-92:


This enables testing batch-aware jobs without requiring actual batch infrastructure.

Sources: src/Queueable.php253-278 src/Batchable.php66-92

Trait Usage Guidelines

When to Use Each Trait

TraitRequired WhenOptional When
DispatchableJob needs static dispatch methodsUsing dispatch() helper function
QueueableJob implements ShouldQueue interfaceConfiguring delays, middleware, or chains
BatchableJob participates in batchesJob needs batch state access

Table 7: Trait Usage Guidelines

Minimal Job Example

A minimal queueable job with all traits:


This job can be:

  • Dispatched using ProcessOrder::dispatch($order)
  • Configured with queue settings
  • Included in batches
  • Chained with other jobs

Sources: src/Dispatchable.php14-87 src/Queueable.php20-279 src/Batchable.php13-93

Integration with Bus Components

The traits integrate with multiple bus components to provide complete functionality:


Figure 7: Trait Integration with Bus Components

Sources: src/Dispatchable.php1-87 src/Queueable.php1-279 src/Batchable.php1-93

Summary

The trait system in hypervel/bus provides composable, focused functionality for jobs:

  • Dispatchable: Provides static entry points for job dispatch
  • Queueable: Enables queue configuration, delays, middleware, and chaining
  • Batchable: Provides batch awareness and state access

These traits follow the fluent API pattern, integrate with Hyperf's dependency injection container, and compose together to provide comprehensive job functionality. Jobs typically use all three traits when implementing the ShouldQueue interface.

For detailed information on each trait, see the dedicated pages: Dispatchable, Queueable, and Batchable.

Sources: src/Dispatchable.php1-87 src/Queueable.php1-279 src/Batchable.php1-93