Sha256: 2234f05f5588a9c77616a6f7d0ed055b7dd9edc145325eb472863536e95847a1

Contents?: true

Size: 1.27 KB

Versions: 25

Compression:

Stored size: 1.27 KB

Contents

module Shipit
  class GithubSyncJob < BackgroundJob
    include BackgroundJob::Unique

    MAX_FETCHED_COMMITS = 10
    queue_as :default

    self.timeout = 60
    self.lock_timeout = 20

    def perform(params)
      @stack = Stack.find(params[:stack_id])

      handle_github_errors do
        new_commits, shared_parent = fetch_missing_commits { @stack.github_commits }

        @stack.transaction do
          shared_parent.try!(:detach_children!)
          new_commits.each do |gh_commit|
            @stack.commits.create_from_github!(gh_commit)
          end
        end
      end
      CacheDeploySpecJob.perform_later(@stack)
    end

    def fetch_missing_commits(&block)
      commits = []
      iterator = Shipit::FirstParentCommitsIterator.new(&block)
      iterator.each_with_index do |commit, index|
        break if index >= MAX_FETCHED_COMMITS

        if shared_parent = lookup_commit(commit.sha)
          return commits, shared_parent
        end
        commits.unshift(commit)
      end
      return commits, nil
    end

    protected

    def handle_github_errors
      yield
    rescue Octokit::NotFound
      @stack.mark_as_inaccessible!
    else
      @stack.mark_as_accessible!
    end

    def lookup_commit(sha)
      @stack.commits.find_by_sha(sha)
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
shipit-engine-0.16.0 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.15.0 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.14.0 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.13.0 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.12.1 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.12.0 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.11.0 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.10.0 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.9.0 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.8.9 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.8.8 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.8.7 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.8.6 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.8.5 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.8.4 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.8.3 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.8.2 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.8.1 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.8.0 app/jobs/shipit/github_sync_job.rb
shipit-engine-0.7.0 app/jobs/shipit/github_sync_job.rb