Module: Cyclid::API::Job::Helpers
- Includes:
- Exceptions
- Defined in:
- app/cyclid/job/helpers.rb
Overview
Useful methods for dealing with Jobs
Instance Method Summary collapse
-
#job_from_definition(definition, callback = nil, context = {}) ⇒ Object
Create & dispatch a Job from the job definition.
Instance Method Details
#job_from_definition(definition, callback = nil, context = {}) ⇒ Object
Create & dispatch a Job from the job definition
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'app/cyclid/job/helpers.rb', line 29 def job_from_definition(definition, callback = nil, context = {}) # Job definition is a hash (converted from JSON or YAML) definition.symbolize_keys! # This function will only ever be called from a Sinatra context org = Organization.find_by(name: params[:name]) raise NotFoundError, 'organization does not exist' \ if org.nil? # Lint the job and reject if there are errors verifier = Cyclid::Linter::Verifier.new verifier.verify(definition) raise InvalidObjectError, 'job definition has errors' \ unless verifier.status.errors.zero? # Create a new JobRecord job_record = JobRecord.new job_record.job_name = definition[:name] job_record.job_version = definition[:version] || '1.0.0' 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}" Cyclid.logger.debug ex.backtrace.join "\n" # 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! # Re-raise something useful raise InternalError, "job dispatch failed: #{ex}" end return job_id end |