Module: Workhorse::Enqueuer

Included in:
Workhorse
Defined in:
lib/workhorse/enqueuer.rb

Overview

Module providing job enqueuing functionality. Extended by the main Workhorse module to provide enqueuing capabilities. Supports plain Ruby objects, ActiveJob instances, and Rails operations.

Instance Method Summary collapse

Instance Method Details

#enqueue(job, queue: nil, priority: 0, perform_at: Time.now, description: nil) ⇒ Workhorse::DbJob

Enqueues any object that is serializable and has a ‘perform` method.

Parameters:

  • job (Object)

    The job object to enqueue (must respond to #perform)

  • queue (String, Symbol, nil) (defaults to: nil)

    The queue name

  • priority (Integer) (defaults to: 0)

    Job priority (lower numbers = higher priority)

  • perform_at (Time) (defaults to: Time.now)

    When to perform the job

  • description (String, nil) (defaults to: nil)

    Optional job description

Returns:



14
15
16
17
18
19
20
21
22
# File 'lib/workhorse/enqueuer.rb', line 14

def enqueue(job, queue: nil, priority: 0, perform_at: Time.now, description: nil)
  return DbJob.create!(
    queue:       queue,
    priority:    priority,
    perform_at:  perform_at,
    description: description,
    handler:     Marshal.dump(job)
  )
end

#enqueue_active_job(job, perform_at: Time.now, queue: nil, description: nil) ⇒ Workhorse::DbJob

Enqueues an ActiveJob job instance.

Parameters:

  • job (ActiveJob::Base)

    The ActiveJob instance to enqueue

  • perform_at (Time) (defaults to: Time.now)

    When to perform the job

  • queue (String, Symbol, nil) (defaults to: nil)

    Optional queue override

  • description (String, nil) (defaults to: nil)

    Optional job description

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/workhorse/enqueuer.rb', line 31

def enqueue_active_job(job, perform_at: Time.now, queue: nil, description: nil)
  wrapper_job = Jobs::RunActiveJob.new(job.serialize)
  queue ||= job.queue_name if job.queue_name.present?
  db_job = enqueue(
    wrapper_job,
    queue:       queue,
    priority:    job.priority || 0,
    perform_at:  Time.at(perform_at),
    description: description
  )
  job.provider_job_id = db_job.id
  return db_job
end

#enqueue_op(cls, *args) ⇒ Workhorse::DbJob

Enqueues the execution of a Rails operation by its class and parameters.

Parameters:

  • cls (Class)

    The operation class to execute

  • args (Array)

    Variable arguments (workhorse_args, op_args)

Returns:

Raises:

  • (ArgumentError)

    If wrong number of arguments provided



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/workhorse/enqueuer.rb', line 51

def enqueue_op(cls, *args)
  case args.size
  when 0
    workhorse_args = {}
    op_args = {}
  when 1
    workhorse_args = args.first
    op_args = {}
  when 2
    workhorse_args, op_args = *args
  else
    fail ArgumentError, "wrong number of arguments (#{args.size + 1} for 2..3)"
  end

  job = Workhorse::Jobs::RunRailsOp.new(cls, op_args)
  enqueue job, **workhorse_args
end