延迟工作"事件成功'无法过渡

时间:2018-03-14 08:16:47

标签: ruby-on-rails delayed-job

我已经集成了延迟作业来从后台运行Jenkins构建。因此,对于每个版本,我使用 AASM gem来更新我的状态。一旦每个构建完成,我就会因为"Event 'success' cannot transition from 'success'我的工作延迟而得到错误,因为它为什么会发生。

我是通过 approver_controller.rb

来调用我的工作
@build_request.delay(attempts: 1).run_batch

在build_request模型中,我有我的作业方法来执行Jenkins批处理

class BuildRequest < ApplicationRecord
    include AASM
     aasm :column => :build_state do
        state :pending, initial: true
        state :onhold
        state :release
        state :approved
        state :rejected
        state :success
        state :failed

        event :approve do
            transitions from: [:pending], to: :approved
        end

        event :onhold do
            transitions from: [:pending], to: :onhold
        end

        event :release do
            transitions from: [:onhold], to: :pending
        end

        event :reject do
            transitions from: [:pending], to: :rejected
        end

        event :success do
            transitions from: [:approved], to: :success
        end

        event :fail do
            transitions from: [:approved], to: :failed
        end
    end

    def run_batch
            if self.approved?   
                file_name = "#{self.project_job_name}_#{self.id}.txt"
                new_file = File.open(file_name, "w+")
                new_file.puts self.project.job_name + "\r"
                new_file.puts input_path(self)
                new_file.close
                FileUtils.mv(Rails.root.join(file_name), Rails.root.join('public'))
                Net::SCP.start(ENV["IP_ADDRESSS"], ENV["USER_NAME"], :password => ENV["PASSWORD"]) do |scp|
                # asynchronous upload; call returns immediately and requires SSH
                # event loop to run
                    channel = scp.upload(Rails.root.join('public', file_name).to_s, jenkins_build_path)
                    channel.wait
                end
                Net::SSH.start(ENV["IP_ADDRESSS"], ENV["USER_NAME"], :password => ENV["PASSWORD"]) do |ssh|
                    output = ssh.exec!("file_path" " file_name")
                    # start building job 
                    build_status = ApplicationController.helpers.jenkins_client.job.build(self.project_job_name)
                    build_status == "201" ? self.success : self.failed
                end
            end
        end

0 个答案:

没有答案