Class: Cyclid::API::Job::Runner
- Inherits:
-
Object
- Object
- Cyclid::API::Job::Runner
- Includes:
- Constants::JobStatus
- Defined in:
- app/cyclid/job/runner.rb
Overview
Run a job
Instance Method Summary collapse
-
#initialize(job_id, job_definition, notifier) ⇒ Runner
constructor
A new instance of Runner.
-
#run ⇒ Object
Run the stages.
Constructor Details
#initialize(job_id, job_definition, notifier) ⇒ Runner
Returns a new instance of Runner.
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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/cyclid/job/runner.rb', line 26 def initialize(job_id, job_definition, notifier) # The notifier for updating the job status & writing to the log # buffer @notifier = notifier # Un-serialize the job begin @job = Oj.load(job_definition, symbol_keys: true) environment = @job[:environment] secrets = @job[:secrets] rescue StandardError => ex Cyclid.logger.error "couldn't un-serialize job for job ID #{job_id}: #{ex}" raise 'job failed' end # Create an initial job context (more will be added as the job runs) @ctx = @job[:context] @ctx[:job_id] = job_id @ctx[:job_name] = @job[:name] @ctx[:job_version] = @job[:version] @ctx[:organization] = @job[:organization] @ctx.merge! environment @ctx.merge! secrets begin # We're off! @notifier.status = WAITING # Create a Builder @builder = create_builder # Obtain a host to run the job on @notifier.write "#{Time.now} : Obtaining build host...\n" @build_host = request_build_host(@builder, environment) # We have a build host @notifier.status = STARTED # Add some build host details to the build context @ctx.merge! @build_host.context_info # Connect a transport to the build host; the notifier is a proxy # to the log buffer @transport = create_transport(@build_host, @notifier, @ctx) # Prepare the host provisioner = create_provisioner(@build_host) @notifier.write "#{Time.now} : Preparing build host...\n#{'=' * 79}\n" provisioner.prepare(@transport, @build_host, environment) # Check out sources if @job[:sources].any? @notifier.write "#{'=' * 79}\n#{Time.now} : Checking out source...\n" checkout_sources(@transport, @ctx, @job[:sources]) end rescue StandardError => ex Cyclid.logger.error "job runner failed: #{ex}" Cyclid.logger.error ex.backtrace.join("\n") @notifier.status = FAILED @notifier.ended = Time.now.to_s begin @builder.release(@transport, @build_host) if @build_host @transport&.close rescue ::Net::SSH::Disconnect # rubocop:disable Lint/HandleExceptions # Ignored end raise # XXX Raise an internal exception end end |
Instance Method Details
#run ⇒ Object
Run the stages.
Start with the first stage, and execute all of the steps until either one fails, or there are no more steps. The follow the on_success & on_failure handlers to the next stage. If no handler is defined, stop.
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'app/cyclid/job/runner.rb', line 108 def run status = STARTED @notifier.write "#{'=' * 79}\n#{Time.now} : Job started. " \ "Context: #{@ctx.stringify_keys}\n" # Run the Job stage actions stages = @job[:stages] || [] sequence = (@job[:sequence] || []).first # Run each stage in the sequence until there are none left until sequence.nil? # Find the stage raise 'stage not found' unless stages.key? sequence.to_sym # Un-serialize the stage into a StageView stage_definition = stages[sequence.to_sym] stage = Oj.load(stage_definition, symbol_keys: true) # Evaluate any only_if/not_if expressions. Always run the stage if there are no # modifiers. do_run = if stage.only_if Evaluator.only_if(stage.only_if, @ctx) elsif stage.not_if Evaluator.not_if(stage.not_if, @ctx) else true end if do_run @notifier.write "#{'-' * 79}\n#{Time.now} : " \ "Running stage #{stage.name} v#{stage.version}\n" # Run the stage success, rc = run_stage(stage) Cyclid.logger.info "stage #{(success ? 'succeeded' : 'failed')} and returned #{rc}" else @notifier.write "#{'-' * 79}\n#{Time.now} : " \ "Skipping stage #{stage.name} v#{stage.version}\n" # Skip this stage; assume success success = true rc = 0 # rubocop:disable Style/MultilineTernaryOperator Cyclid.logger.info "stage skipped due to #{stage.only_if ? \ "only_if #{stage.only_if}" : "not_if #{stage.not_if}"}" # rubocop:enable Style/MultilineTernaryOperator end # Decide which stage to run next depending on the outcome of this # one if success sequence = stage.on_success else sequence = stage.on_failure # Remember the failure while the failure handlers run status = FAILING @notifier.status = status end end # Either all of the stages succeeded, and thus the job suceeded, or # (at least one of) the stages failed, and thus the job failed if status == FAILING @notifier.status = FAILED @notifier.ended = Time.now success = false else @notifier.status = SUCCEEDED @notifier.ended = Time.now success = true end # We no longer require the build host & transport begin @builder.release(@transport, @build_host) @transport.close rescue ::Net::SSH::Disconnect # rubocop:disable Lint/HandleExceptions # Ignored end return success end |