Sha256: cdba6ab94f64c426c9b7a4a88952fbf8541e1ebaf8024e453fd86ff607433a7a

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

require "jiralicious"
require "embulk/input/jira/issue"
require "timeout"

module Embulk
  module Input
    module Jira
      class Api
        SEARCH_TIMEOUT_SECONDS = 5
        SEARCH_ISSUES_TIMEOUT_SECONDS = 60
        DEFAULT_SEARCH_RETRY_TIMES = 10

        def self.setup(&block)
          Jiralicious.configure(&block)
          new
        end

        def search_issues(jql, options={})
          timeout_and_retry(SEARCH_ISSUES_TIMEOUT_SECONDS) do
            search(jql, options).issues.map do |issue|
              Jira::Issue.new(issue)
            end
          end
        end

        def search(jql, options={})
          timeout_and_retry(SEARCH_TIMEOUT_SECONDS) do
            Jiralicious.search(jql, options)
          end
        end

        def total_count(jql)
          search(jql, max_results: 1).num_results
        end

        private

        def timeout_and_retry(wait, retry_times = DEFAULT_SEARCH_RETRY_TIMES, &block)
          count = 1
          begin
            Timeout.timeout(wait) do
              yield
            end
          rescue Timeout::Error => e
            count += 1
            sleep count # retry after some seconds for JIRA API perhaps under the overload
            raise e if count > retry_times
            retry
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
embulk-input-jira-0.0.4 lib/embulk/input/jira/api.rb
embulk-input-jira-0.0.3 lib/embulk/input/jira/api.rb