Module: Conflow::Flow::JobHandler

Included in:
Conflow::Flow
Defined in:
lib/conflow/flow/job_handler.rb

Overview

Handles running and finishing jobs

Instance Method Summary collapse

Instance Method Details

#finish(job, result = nil) ⇒ Object

Finishes job, changes its status, runs hook if it’s present and queues new available jobs

Parameters:

  • job (Conflow::Job)

    job to be marked as finished

  • result (Object) (defaults to: nil)

    result of the job to be passed to hook



28
29
30
31
32
33
# File 'lib/conflow/flow/job_handler.rb', line 28

def finish(job, result = nil)
  send(job.hook.to_s, result) unless job.hook.nil?
  call_script(Conflow::Redis::CompleteJobScript, job)
  queue_available_jobs
  destroy! if finished?
end

#run(job_class, params: {}, after: [], hook: nil) ⇒ Conflow::Job

Returns enqueued job.

Parameters:

  • job_class (Class)

    Class of the worker which will perform the job

  • params (Hash) (defaults to: {})

    Parameters of the job. They will be passed to Worker#perform block. Defalts to empty hash.

  • after (Conflow::Job|Class|Integer|Array<Conflow::Job,Class,Integer>) (defaults to: [])

    Dependencies of the job. Can be one or more objects of the following classes: Job, Class, Integer

  • hook (Symbol) (defaults to: nil)

    method to be called on Conflow::Flow instance after job is performed. The hook method should accept result of the job (value returned by Worker#perform)

Returns:



15
16
17
18
19
20
21
22
23
# File 'lib/conflow/flow/job_handler.rb', line 15

def run(job_class, params: {}, after: [], hook: nil)
  build_job(job_class, params, hook).tap do |job|
    job_classes[job_class] = job
    after = prepare_dependencies(after).compact

    call_script(Conflow::Redis::AddJobScript, job, after: after)
    queue_available_jobs
  end
end