Class: Isono::Rack::Job

Inherits:
Decorator show all
Includes:
Logger
Defined in:
lib/isono/rack/job.rb

Defined Under Namespace

Classes: JobRequest, JobResponse

Instance Attribute Summary

Attributes inherited from Decorator

#app

Instance Method Summary collapse

Methods included from Logger

included, initialize

Constructor Details

#initialize(app, job_worker) ⇒ Job

Returns a new instance of Job.

Raises:

  • (ArgumentError)


45
46
47
48
49
# File 'lib/isono/rack/job.rb', line 45

def initialize(app, job_worker)
  super(app)
  raise ArgumentError unless job_worker.respond_to?(:start)
  @job_worker = job_worker
end

Instance Method Details

#call(req, res) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/isono/rack/job.rb', line 51

def call(req, res)
  job = @job_worker.start do |job|
    job.job_id = req.r[:job_id]
    job.parent_job_id = req.r[:parent_job_id]
    job.session_id = req.r[:session_id]
    job.job_name = req.r[:job_name]
    job.run_cb = proc {
      begin
        @app.call(JobRequest.new(req.r, job), JobResponse.new(res.ctx, job))
        res.response(nil) unless res.responded?
      rescue Exception => e
        res.response(e) unless res.responded?
        raise e
      end
    }
  end

  # send the new job context info back as the first progress message.
  # the progress messages that follow will be handled as usual.
  res.progress(job.to_hash)
end