Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseConfig ¶
type BaseConfig struct { // Enabled determines if the batch job is active and should be processed Enabled bool `json:"enabled"` // Interval specifies the frequency at which the job should run. // Supports cron-like syntax and duration strings (e.g., "@daily", "10s", "1m", "1h") Interval string `json:"interval"` // MaxRetries defines the maximum number of retry attempts for failed jobs MaxRetries int64 `json:"maxRetries"` }
BaseConfig provides fundamental configuration options for batch jobs. It defines common settings such as job scheduling intervals and retry policies.
func (*BaseConfig) ProcessingInterval ¶
func (c *BaseConfig) ProcessingInterval() taskprocessor.ProcessingInterval
ProcessingInterval converts the string interval configuration into a structured taskprocessor.ProcessingInterval value.
Example:
cfg := BaseConfig{Interval: "@daily"} interval := cfg.ProcessingInterval() // Returns taskprocessor.EveryDayAtMidnight
func (*BaseConfig) Validate ¶
func (c *BaseConfig) Validate() error
Validate checks the correctness of the base configuration values. Returns an error if any values are invalid.
Validation rules: - MaxRetries must be non-negative - Interval must not be empty
type BatchJob ¶
type BatchJob struct { BaseConfig // contains filtered or unexported fields }
BatchJob represents a configurable batch job with its associated task and identifier. It extends BaseConfig with task-specific configuration for asynq processing.
func NewBatchJob ¶
func NewBatchJob(options ...BatchJobConfigOption) *BatchJob
NewBatchJob creates a new BatchJob instance with the provided options. It uses the functional options pattern to allow flexible configuration.
Example:
taskId := "job-123" job := NewBatchJob( WithBaseConfig(baseCfg), WithTask(task), WithTaskId(&taskId), )
type BatchJobConfigOption ¶
type BatchJobConfigOption func(*BatchJob)
BatchJobConfigOption defines a function type for configuring BatchJob instances. This follows the functional options pattern for flexible configuration.
func WithBaseConfig ¶
func WithBaseConfig(cfg BaseConfig) BatchJobConfigOption
WithBaseConfig sets the base configuration for the batch job. This includes common settings like processing interval and retry policy.
Example:
baseCfg := BaseConfig{...} job := NewBatchJob(WithBaseConfig(baseCfg))
func WithTask ¶
func WithTask(task *asynq.Task) BatchJobConfigOption
WithTask sets the asynq task for the batch job. The task contains the actual work to be performed when the job executes.
Example:
task := asynq.NewTask(...) job := NewBatchJob(WithTask(task))
func WithTaskId ¶
func WithTaskId(taskId *string) BatchJobConfigOption
WithTaskId sets the unique identifier for the batch job. This ID is used to prevent duplicate job registrations.
Example:
taskId := "unique-job-id" job := NewBatchJob(WithTaskId(&taskId))
type BatchJobs ¶
type BatchJobs struct {
// contains filtered or unexported fields
}
BatchJobs manages a collection of batch jobs that can be scheduled and executed. It provides methods for adding and managing individual BatchJob instances.
func NewBatchJobs ¶
func NewBatchJobs(options ...BatchJobsOption) *BatchJobs
NewBatchJobs creates a new BatchJobs instance with the provided options. It uses the functional options pattern to allow flexible configuration.
Example:
jobs := NewBatchJobs( WithBatchJob(job1), WithBatchJob(job2), )
type BatchJobsOption ¶
type BatchJobsOption func(*BatchJobs)
BatchJobsOption defines a function type for configuring BatchJobs instances. This follows the functional options pattern for flexible configuration.
func WithBatchJob ¶
func WithBatchJob(batchJob *BatchJob) BatchJobsOption
WithBatchJob adds a single batch job to the collection. If the jobs slice hasn't been initialized, it will create a new slice.
Example:
job := &BatchJob{...} jobs := NewBatchJobs(WithBatchJob(job))
func WithBatchJobs ¶
func WithBatchJobs(batchJobs *BatchJobs) BatchJobsOption
WithBatchJobs replaces the entire collection of batch jobs with the provided jobs. This is useful when you want to configure multiple jobs at once.
Example:
existingJobs := &BatchJobs{...} newJobs := NewBatchJobs(WithBatchJobs(existingJobs))
type Batcher ¶
type Batcher struct {
// contains filtered or unexported fields
}
Batcher implements the Runnable interface and provides functionality to register and manage recurring batch jobs.
func NewBatcher ¶
func NewBatcher(options ...BatcherOption) *Batcher
NewBatcher creates a new Batcher instance with the provided options. It uses the functional options pattern to allow flexible configuration.
Example:
batcher := NewBatcher( WithBatchJobsRef(jobs), WithTaskProcessor(processor), )
func (*Batcher) RegisterRecurringBatchJobs ¶
RegisterRecurringBatchJobs implements the Runnable interface. It validates and registers each batch job with the task processor. If a job with the same ID already exists, it will be skipped without error. Any other registration errors will be returned immediately.
type BatcherOption ¶
type BatcherOption func(*Batcher)
BatcherOption defines a function type for configuring a Batcher instance. This follows the functional options pattern for flexible configuration.
func WithBatchJobsRef ¶
func WithBatchJobsRef(batchJobs *BatchJobs) BatcherOption
WithBatchJobsRef sets the batch jobs configuration for the batcher. The BatchJobs parameter contains the collection of jobs to be registered.
Example:
jobs := &BatchJobs{...} batcher := NewBatcher(WithBatchJobsRef(jobs))
func WithTaskProcessor ¶
func WithTaskProcessor(processor *taskprocessor.TaskProcessor) BatcherOption
WithTaskProcessor sets the task processor that will handle the execution of batch jobs. The task processor is responsible for managing the job queue and executing jobs according to their schedules.
Example:
processor := taskprocessor.New(...) batcher := NewBatcher(WithTaskProcessor(processor))
type Runnable ¶
type Runnable interface { // RegisterRecurringBatchJobs registers all batch jobs with the task processor. // It validates each job's configuration before registration and returns an error // if any job fails validation or registration. // // Example: // batcher := NewBatcher(WithBatchJobs(jobs), WithTaskProcessor(processor)) // if err := batcher.RegisterRecurringBatchJobs(ctx); err != nil { // log.Fatal(err) // } RegisterRecurringBatchJobs(ctx context.Context) error }
Runnable defines the interface for registering recurring batch jobs. Implementations of this interface are responsible for setting up and configuring batch jobs that need to run on a recurring schedule.