Sha256: fbd9efdbb3069da3c9526d7aa21432397c681bb9fc7e2b29e7868946d3d16080

Contents?: true

Size: 1.26 KB

Versions: 9

Compression:

Stored size: 1.26 KB

Contents

module Shipit
  class PerformTaskJob < BackgroundJob
    queue_as :deploys

    def perform(task)
      @task = task
      @commands = Commands.for(@task)
      unless @task.pending?
        logger.error("Task ##{@task.id} already in `#{@task.status}` state. Aborting.")
        return
      end
      run
    ensure
      @commands.clear_working_directory
    end

    def run
      @task.run!
      checkout_repository
      perform_task
      @task.complete!
    rescue Command::Error => error
      @task.report_failure!(error)
    rescue StandardError => error
      @task.report_error!(error)
    end

    def perform_task
      Bundler.with_clean_env do
        capture_all @commands.install_dependencies
        capture_all @commands.perform
      end
    end

    def checkout_repository
      @task.acquire_git_cache_lock do
        capture @commands.fetch
        capture @commands.clone
      end
      capture @commands.checkout(@task.until_commit)
    end

    def capture_all(commands)
      commands.map { |c| capture(c) }
    end

    def capture(command)
      command.start
      @task.write("$ #{command}\npid: #{command.pid}\n")
      @task.pid = command.pid
      command.stream! do |line|
        @task.write(line)
      end
      @task.write("\n")
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
shipit-engine-0.8.9 app/jobs/shipit/perform_task_job.rb
shipit-engine-0.8.8 app/jobs/shipit/perform_task_job.rb
shipit-engine-0.8.7 app/jobs/shipit/perform_task_job.rb
shipit-engine-0.8.6 app/jobs/shipit/perform_task_job.rb
shipit-engine-0.8.5 app/jobs/shipit/perform_task_job.rb
shipit-engine-0.8.4 app/jobs/shipit/perform_task_job.rb
shipit-engine-0.8.3 app/jobs/shipit/perform_task_job.rb
shipit-engine-0.8.2 app/jobs/shipit/perform_task_job.rb
shipit-engine-0.8.1 app/jobs/shipit/perform_task_job.rb