Sha256: c0ad564d92ebcc0a3bcb3126369e641e4982e828440c8f67ee072cd875be4a3d

Contents?: true

Size: 1.37 KB

Versions: 3

Compression:

Stored size: 1.37 KB

Contents

module Workhorse
  module Enqueuer
    # Enqueue any object that is serializable and has a `perform` method
    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 an ActiveJob job
    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 the execution of an operation by its class and params
    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
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
workhorse-1.2.2 lib/workhorse/enqueuer.rb
workhorse-1.2.1 lib/workhorse/enqueuer.rb
workhorse-1.2.0 lib/workhorse/enqueuer.rb