Module: Cyclid::API::Job::Helpers

Defined in:
app/cyclid/job/helpers.rb

Overview

Useful methods for dealing with Jobs

Instance Method Summary collapse

Instance Method Details

#job_from_definition(definition, callback = nil, context = {}) ⇒ Object

Create & dispatch a Job from the job definition



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/cyclid/job/helpers.rb', line 25

def job_from_definition(definition, callback = nil, context = {})
  # This function will only ever be called from a Sinatra context
  org = Organization.find_by(name: params[:name])
  halt_with_json_response(404, INVALID_ORG, 'organization does not exist') \
    if org.nil?

  # Create a new JobRecord
  job_record = JobRecord.new
  job_record.started = Time.now.to_s
  job_record.status = Constants::JobStatus::NEW
  job_record.save!

  org.job_records << job_record

  # The user may, or may not, be set: if the job has come via. the :organization/jobs
  # endpoint it'll be set (as that's authenticated), if it's come from an API extension the
  # user mat not be set (as it may be unauthenticated, or not using the same authentication
  # as Cyclid)
  user = current_user
  current_user.job_records << job_record if user

  begin
    job = ::Cyclid::API::Job::JobView.new(definition, context, org)
    Cyclid.logger.debug job.to_hash

    job_id = Cyclid.dispatcher.dispatch(job, job_record, callback)
  rescue StandardError => ex
    Cyclid.logger.error "job dispatch failed: #{ex}"

    # We couldn't dispatch the job; record the failure
    job_record.status = Constants::JobStatus::FAILED
    job_record.ended = Time.now.to_s
    job_record.save!

    raise
  end

  return job_id
end