VOOZH about

URL: https://deepwiki.com/hypervel/bus

⇱ hypervel/bus | DeepWiki


Loading...
Menu

Overview

Purpose and Scope

This document provides an overview of the hypervel/bus package, a command bus and job dispatching system for the Hyperf framework. It introduces the package's purpose, core capabilities, system architecture, and key components. For detailed information on specific features, refer to the specialized pages: Installation and Configuration, Core Concepts, Dispatching Jobs, Job Traits, Job Chaining, Batch Processing, and Advanced Features.

What is Hypervel Bus?

hypervel/bus is a command bus implementation that provides a unified interface for dispatching commands and jobs in Hyperf applications. A command bus decouples the request to perform an action from the actual execution, enabling:

  • Deferred execution: Jobs can be queued for asynchronous processing
  • Separation of concerns: Business logic is encapsulated in job classes
  • Centralized dispatch logic: All job execution flows through a single dispatcher
  • Flexible routing: Jobs can execute immediately (synchronous) or via queue workers (asynchronous)

The package serves as the foundation for job orchestration, providing the infrastructure to dispatch single jobs, chain multiple jobs together, or batch jobs for coordinated execution.

Sources: composer.json1-60 LICENSE.md1-23

Core Capabilities

CapabilityDescriptionKey Components
Synchronous DispatchExecute jobs immediately in the current processdispatch_sync(), Dispatcher::dispatchSync()
Asynchronous DispatchQueue jobs for background processingdispatch(), QueueingDispatcher::dispatch()
Job ChainingExecute jobs sequentially, with each job triggering the nextPendingChain, Queueable::withChain()
Batch ProcessingGroup jobs together with shared lifecycle callbacks and state trackingPendingBatch, Batch, BatchRepository
Unique JobsPrevent duplicate job dispatch based on custom uniqueness criteriaShouldBeUnique, UniqueLock
Flexible ConfigurationConfigure connection, queue, delay, and callbacks per dispatchPendingDispatch, Queueable trait
Lifecycle HooksRegister callbacks for before/after dispatch, batch progress, errorsCallback methods on pending objects

Sources: composer.json1-60

System Architecture

The package is organized into four primary architectural layers:


Diagram: Package Architecture and Component Relationships

Layer Responsibilities

  1. Public API Layer: Entry points for developers, including global helper functions dispatch() and dispatch_sync() from src/Functions.php and reusable traits that add dispatch capabilities to job classes.

  2. Job Configuration Layer: "Pending" objects (PendingDispatch, PendingChain, PendingBatch) that accumulate configuration before dispatch. These provide fluent interfaces for setting queue names, delays, callbacks, and other options.

  3. Core Dispatching Engine: The Dispatcher class implements dispatch logic, routing jobs to either synchronous execution or the queue system. UniqueLock enforces job uniqueness constraints. ConfigProvider registers all services with Hyperf's dependency injection container.

  4. Batch Processing System: Specialized components for managing batched job execution, including state tracking, persistence via BatchRepository, and lifecycle callbacks.

Sources: composer.json37-43

Package Structure

The codebase maps to functional areas as follows:


Diagram: Package File Structure and Component Organization

Key Directories

DirectoryPurposeKey Files
src/ (root)Global functions and core dispatcherFunctions.php, Dispatcher.php, ConfigProvider.php
src/Contracts/Interfaces defining core abstractionsDispatcher.php, QueueingDispatcher.php, BatchRepository.php
src/Traits/Mixins providing reusable job capabilitiesDispatchable.php, Queueable.php, Batchable.php
src/ (pending classes)Configuration wrappers for fluent APIPendingDispatch.php, PendingChain.php, PendingBatch.php
src/ (batch classes)Batch execution and storageBatch.php, ChainedBatch.php, DatabaseBatchRepository.php
src/Factories/Factory classes for DI instantiationDispatcherFactory.php, DatabaseBatchRepositoryFactory.php
src/Events/Event classes for lifecycle notificationsBatchDispatched.php

Sources: composer.json37-43

Integration with Hypervel Ecosystem

hypervel/bus is part of the Hypervel framework ecosystem and integrates tightly with complementary packages:


Diagram: Package Dependencies and Integration Points

Required Dependencies

  • PHP 8.2+: Minimum language version requirement
  • Hyperf 3.1.0: Framework foundation providing DI container, coroutine management, and core contracts
  • hypervel/support: Shared utility functions and base classes
  • hypervel/cache: Required for UniqueLock to prevent duplicate job dispatch

Optional Dependencies

  • hypervel/queue: Suggested for asynchronous job dispatch and required when using closures in job chains (see composer.json53-55)

Integration Points

The package integrates with external systems through:

  1. Dependency Injection: ConfigProvider registers bindings with Hyperf's DI container
  2. Queue System: Dispatcher pushes jobs to queues via QueueFactory
  3. Cache System: UniqueLock stores locks via CacheFactory
  4. Database: DatabaseBatchRepository persists batch state to database tables

Sources: composer.json23-35 composer.json53-55

Key Components and Code Entities

The following table maps conceptual components to concrete code entities:

ConceptPrimary Class/FileContract/InterfacePurpose
Job DispatchDispatcherContracts\DispatcherRoutes jobs to sync or async execution
Queue DispatchDispatcherContracts\QueueingDispatcherAdds batch and queue-specific methods
Configuration WrapperPendingDispatchN/AFluent API for configuring single job dispatch
Chain ConfigurationPendingChainN/AConfigures sequential job execution
Batch ConfigurationPendingBatchN/AConfigures grouped job execution
Batch ExecutionBatchN/AManages batch state and job coordination
Batch StorageDatabaseBatchRepositoryContracts\BatchRepositoryPersists batch data to database
Uniqueness LockUniqueLockN/APrevents duplicate job dispatch
DI RegistrationConfigProviderN/ARegisters services with Hyperf container
Static DispatchTraits\DispatchableN/AAdds static dispatch() method to jobs
Queue ConfigTraits\QueueableN/AAdds queue properties to jobs
Batch AssociationTraits\BatchableN/ALinks jobs to parent batches
Global HelpersFunctions.phpN/AProvides dispatch() and dispatch_sync()

Sources: composer.json37-43

Dispatch Flow Overview

The typical dispatch flow follows this pattern:


Diagram: Standard Job Dispatch Sequence

  1. Developer calls dispatch() helper with a job instance
  2. PendingDispatch wraps the job and collects configuration via fluent methods
  3. When PendingDispatch destructs, it triggers Dispatcher::dispatch()
  4. If job implements ShouldBeUnique, UniqueLock checks for duplicates
  5. If job implements ShouldQueue, it's pushed to the queue; otherwise executed immediately

Sources: composer.json41-43

Getting Started

To begin using hypervel/bus:

  1. Install the package: See Installation and Configuration
  2. Understand core concepts: Review Core Concepts for foundational knowledge
  3. Create and dispatch jobs: Follow Dispatching Jobs to learn dispatch patterns
  4. Add job capabilities: Use Job Traits to enable queuing, batching, and more
  5. Implement advanced features: Explore Job Chaining, Batch Processing, and Advanced Features

Sources: README.md1-3