JoobQ::QueueFactory
Inherits Reference / Object
Queue Factory for creating queues from string-based job class names
This factory solves the problem of instantiating generic Queue(T) classes when the job type T is only known as a string at runtime (from YAML config).
Basic Usage:
# 1. Register job types at compile-time (in your application code)
QueueFactory.register_job_type(EmailJob)
QueueFactory.register_job_type(ImageProcessingJob)
# 2a. Create queues individually using string names (from YAML)
queue = QueueFactory.create_queue(
name: "email_queue",
job_class_name: "EmailJob",
workers: 5,
throttle: {limit: 10, period: 1.minute}
)
# 2b. Or get all instantiated queues from configuration (memoized)
all_queues = QueueFactory.queues # Reads from JoobQ.config and caches
Memoization Pattern:
The queues method integrates with Configure and YamlConfigLoader to:
- Read queue configurations from
JoobQ.config.queue_configs - Instantiate all configured queues on first access
- Cache the queue instances for subsequent calls
- Support cache clearing via
clear_queues_cacheorclear_registry
The Crystal Limitation
Crystal is statically-typed and compiled. Generic types like Queue(T) must be instantiated at compile-time, not runtime. You cannot do:
# ❌ This doesn't work in Crystal
job_class_name = "EmailJob" # from YAML
queue = Queue(job_class_name.constantize).new # .constantize doesn't exist!
The Solution
Use a factory pattern where:
- All possible job types are registered at compile-time
- The factory uses a hash mapping string names to factory procs
- Each factory proc creates the correctly-typed queue
- We return BaseQueue interface to hide the generic type
- Queues are instantiated lazily and memoized from configuration
Class methods
Internal method to add a factory function to the registry
Create a queue based on string job class name
Parameters:
name: Queue namejob_class_name: String name of the job class (e.g., "EmailJob")workers: Number of workers for this queuethrottle: Optional throttling configuration
Returns: A BaseQueue instance
Raises: QueueFactoryError if the job class is not registered
Create queues from YAML configuration data
This method takes the queue_configs hash from Configure and creates actual Queue instances for all configured queues.
Example:
config = YamlConfigLoader.load_from_file("config/joobq.yml")
queues = QueueFactory.create_queues_from_config(config.queue_configs)
queues.each do |name, queue|
config.queues[name] = queue
end
Populate a JobSchemaRegistry with all registered job types This is useful after config reset to repopulate the job_registry
Get all instantiated queues from configuration
This method instantiates and memoizes all queues based on the queue_configs defined in the Configure instance. Queues are created lazily on first access and cached for subsequent calls.
The method:
- Checks if queues are already cached
- If not, retrieves queue_configs from JoobQ.config
- Creates queue instances using the factory registry
- Caches and returns the queue hash indexed by queue name
Returns: Hash(String, BaseQueue) - queues indexed by queue name
Example:
# After registering job types and loading YAML config
QueueFactory.register_job_type(EmailJob)
QueueFactory.register_job_type(ImageProcessingJob)
config = Configure.load_from_yaml("config/joobq.yml")
# Get all instantiated queues
all_queues = QueueFactory.queues
email_queue = all_queues["email_queue"]?
Macros
Register a job type for queue creation
This macro creates a factory function for the given job type and stores it in the registry using the job class name as the key.
Example:
QueueFactory.register_job_type(EmailJob)
QueueFactory.register_job_type(ImageProcessingJob)