Class: TQ::App

Inherits:
Object
  • Object
show all
Defined in:
lib/tq/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, worker, options = {}) ⇒ App

Returns a new instance of App.



26
27
28
29
# File 'lib/tq/app.rb', line 26

def initialize(id, worker, options={})
  @id = id; @worker = worker  
  @options = DEFAULT_OPTIONS.merge(options)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



25
26
27
# File 'lib/tq/app.rb', line 25

def id
  @id
end

#workerObject (readonly)

Returns the value of attribute worker.



25
26
27
# File 'lib/tq/app.rb', line 25

def worker
  @worker
end

Instance Method Details

#application_nameObject



107
108
109
# File 'lib/tq/app.rb', line 107

def application_name
  @id.split('/')[0]
end

#application_versionObject



111
112
113
# File 'lib/tq/app.rb', line 111

def application_version
  @id.split('/')[1] || '0.0.0'
end

#auth!(secrets_file = nil, store_file = nil) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/tq/app.rb', line 87

def auth!(secrets_file=nil, store_file=nil)
  if store_file.nil? || (cred_store = credentials_store(store_file)).authorization.nil?
    client_secrets = Google::APIClient::ClientSecrets.load(secrets_file)
    flow = Google::APIClient::InstalledAppFlow.new(
      :client_id => client_secrets.client_id,
      :client_secret => client_secrets.client_secret,
      :scope => TASKQUEUE_API_SCOPES
    )
    client.authorization = store_file.nil? ? 
                             flow.authorize :
                             flow.authorize(cred_store)
  else
    client.authorization = cred_store.authorization
  end
  
  api = client.discovered_api(TASKQUEUE_API, TASKQUEUE_API_VERSION)

  return client, api
end

#env(_) ⇒ Object



47
48
49
# File 'lib/tq/app.rb', line 47

def env(_)
  options({'env' => @options['env'].merge(_)})
end

#log(_) ⇒ Object



39
40
41
# File 'lib/tq/app.rb', line 39

def log(_)
  options({'log' => @options['log'].merge(_)})
end

#logger(_) ⇒ Object



43
44
45
# File 'lib/tq/app.rb', line 43

def logger(_)
  options({'logger' => _})
end

#options(_) ⇒ Object



31
32
33
# File 'lib/tq/app.rb', line 31

def options(_)
  App.new @id, @worker, @options.merge(_)
end

#project(_) ⇒ Object



35
36
37
# File 'lib/tq/app.rb', line 35

def project(_)
  options({'project' => _})
end

#run!(secrets_file = nil, store_file = nil) ⇒ Object



66
67
68
69
# File 'lib/tq/app.rb', line 66

def run!(secrets_file=nil, store_file=nil)
  setup_logger!
  _run *(_queues( TQ::Queue.new( *(auth!(secrets_file, store_file)) ).project(@options['project']) ) )
end

#service_auth!(issuer, p12_file) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/tq/app.rb', line 72

def service_auth!(issuer, p12_file)
  key = Google::APIClient::KeyUtils.load_from_pkcs12(p12_file, 'notasecret')
  client.authorization = Signet::OAuth2::Client.new(
    :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
    :audience => 'https://accounts.google.com/o/oauth2/token',
    :scope => TASKQUEUE_API_SCOPES,
    :issuer => issuer,
    :signing_key => key)
  client.authorization.fetch_access_token!
  
  api = client.discovered_api(TASKQUEUE_API, TASKQUEUE_API_VERSION)

  return client, api
end

#stderr(_) ⇒ Object



61
62
63
64
# File 'lib/tq/app.rb', line 61

def stderr(_)
  return stderr({'name' => _}) if String === _ 
  options({'stderr' => _})
end

#stdin(_) ⇒ Object



51
52
53
54
# File 'lib/tq/app.rb', line 51

def stdin(_)
  return stdin({'name' => _}) if String === _ 
  options({'stdin' => _})
end

#stdout(_) ⇒ Object



56
57
58
59
# File 'lib/tq/app.rb', line 56

def stdout(_)
  return stdout({'name' => _}) if String === _ 
  options({'stdout' => _})
end