Class: Embulk::Output::Bigquery::GoogleClient

Inherits:
Object
  • Object
show all
Defined in:
lib/embulk/output/bigquery/google_client.rb

Direct Known Subclasses

BigqueryClient, GcsClient

Instance Method Summary collapse

Constructor Details

#initialize(task, scope, client_class) ⇒ GoogleClient

Returns a new instance of GoogleClient.



13
14
15
16
17
# File 'lib/embulk/output/bigquery/google_client.rb', line 13

def initialize(task, scope, client_class)
  @task = task
  @scope = scope
  @client_class = client_class
end

Instance Method Details

#clientObject



19
20
21
22
23
24
25
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
# File 'lib/embulk/output/bigquery/google_client.rb', line 19

def client
  return @cached_client if @cached_client && @cached_client_expiration > Time.now

  client = @client_class.new
  client.client_options.application_name = @task['application_name']
  client.request_options.retries = @task['retries']
  client.request_options.timeout_sec = @task['timeout_sec']
  client.request_options.open_timeout_sec = @task['open_timeout_sec']
  Embulk.logger.debug { "embulk-output-bigquery: client_options: #{client.client_options.to_h}" }
  Embulk.logger.debug { "embulk-output-bigquery: request_options: #{client.request_options.to_h}" }

  case @task['auth_method']
  when 'private_key'
    private_key_passphrase = 'notasecret'
    key = Google::APIClient::KeyUtils.load_from_pkcs12(@task['p12_keyfile'], private_key_passphrase)
    auth = Signet::OAuth2::Client.new(
      token_credential_uri: "https://accounts.google.com/o/oauth2/token",
      audience: "https://accounts.google.com/o/oauth2/token",
      scope: @scope,
      issuer: @task['service_account_email'],
      signing_key: key)

  when 'compute_engine'
    auth = Google::Auth::GCECredentials.new

  when 'json_key'
    json_key = @task['json_keyfile']
    if File.exist?(json_key)
      auth = File.open(json_key) do |f|
        Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: f, scope: @scope)
      end
    else
      key = StringIO.new(json_key)
      auth = Google::Auth::ServiceAccountCredentials.make_creds(json_key_io: key, scope: @scope)
    end

  when 'application_default'
    auth = Google::Auth.get_application_default([@scope])

  else
    raise ConfigError, "Unknown auth method: #{@task['auth_method']}"
  end

  client.authorization = auth

  @cached_client_expiration = Time.now + 1800
  @cached_client = client
end

#with_network_retry(&block) ⇒ Object

google-api-ruby-client itself has a retry feature, but it does not retry with SocketException



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/embulk/output/bigquery/google_client.rb', line 69

def with_network_retry(&block)
  retries = 0
  begin
    yield
  rescue ::Java::Java.net.SocketException, ::Java::Java.net.ConnectException => e
    if ['Broken pipe', 'Connection reset', 'Connection timed out'].include?(e.message)
      if retries < @task['retries']
        retries += 1
        Embulk.logger.warn { "embulk-output-bigquery: retry \##{retries}, #{e.class} #{e.message}" }
        retry
      else
        Embulk.logger.error { "embulk-output-bigquery: retry exhausted \##{retries}, #{e.class} #{e.message}" }
        raise e
      end
    else
      raise e
    end
  end
end