Module: BlackStack::Pampa

Defined in:
lib/pampa_workers.rb

Constant Summary collapse

SLEEP_SECONDS =
10
@@division_name =
nil
@@timeout =
7
@@api_key =

Api-key of the client who will be the owner of a process.

nil
@@api_protocol =

Protocol, HTTP or HTTPS, of the BlackStack server where this process will be registered.

nil
@@api_domain =

Domain of the BlackStack server where this process will be registered.

nil
@@api_port =

Port of the BlackStack server where this process will be registered.

nil
@@storage_folder =

path where you will store the data of each client

nil
@@storage_sub_folders =
[]
@@id_timezone_default =

default timezome for any new user

nil
@@farm_external_ip_addresses =

Array of external IP addresses of the servers where farms are running. If you have many servers running behind a NAT server, only add the IP of the external gateway here. This array is used to know if a worker is running inside your farm datacenter, or it is running in the computer of someone else.

[]
@@db_url =

Central database connection parameters

nil
@@db_port =
nil
@@db_name =
nil
@@db_user =
nil
@@db_password =
nil

Class Method Summary collapse

Class Method Details

.api_domainObject



74
75
76
# File 'lib/pampa_workers.rb', line 74

def self.api_domain
  @@api_domain
end

.api_keyObject



58
59
60
# File 'lib/pampa_workers.rb', line 58

def self.api_key()
  @@api_key 
end

.api_portObject



82
83
84
# File 'lib/pampa_workers.rb', line 82

def self.api_port
  @@api_port
end

.api_protocolObject



66
67
68
# File 'lib/pampa_workers.rb', line 66

def self.api_protocol
  @@api_protocol
end

.api_urlObject

get the full URL of the worker api server



87
88
89
# File 'lib/pampa_workers.rb', line 87

def self.api_url()
  "#{BlackStack::Pampa::api_protocol}://#{BlackStack::Pampa::api_domain}:#{BlackStack::Pampa::api_port}"
end

.connection_descriptorObject

TODO: doc me!



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/pampa_workers.rb', line 210

def self.connection_descriptor()           
  ret = nil
  
  # validar que el formato no sea nulo

  if (self.division_name.to_s.length == 0)
    raise "Division name expected."
  end
  
  if (self.division_name == "local") 
    ret = {
      :adapter => 'tinytds',
      :dataserver => BlackStack::Pampa::db_url, # IP or hostname

      :port => BlackStack::Pampa::db_port, # Required when using other that 1433 (default)

      :database => BlackStack::Pampa::db_name,
      :user => BlackStack::Pampa::db_user,
      :password => BlackStack::Pampa::db_password,
      :timeout => BlackStack::Pampa::timeout
      }      
  else
    url = "#{BlackStack::Pampa::api_url}/api1.2/division/get.json"
    res = BlackStack::Netting::call_post(url, {
      'api_key' => BlackStack::Pampa::api_key, 
      'dname' => "#{self.division_name}",
    })
    parsed = JSON.parse(res.body)
    
    if (parsed["status"] != BlackStack::Netting::SUCCESS)
      raise "Error getting connection string: #{parsed["status"]}"
    else
      wid = parsed["value"]
      
      ret = {
        :adapter => 'tinytds',
        :dataserver => parsed["db_url"], # IP or hostname

        :port => parsed["db_port"], # only required if port is different than 1433

        :database => parsed["db_name"],
        :user => parsed["db_user"],
        :password => parsed["db_password"],
        :timeout => BlackStack::Pampa::timeout
      }
    end
  end
  
  ret
end

.db_connectionObject



257
258
259
# File 'lib/pampa_workers.rb', line 257

def self.db_connection()
  Sequel.connect(BlackStack::Pampa::connection_descriptor)
end

.db_nameObject



186
187
188
# File 'lib/pampa_workers.rb', line 186

def self.db_name
  @@db_name
end

.db_passwordObject



196
197
198
# File 'lib/pampa_workers.rb', line 196

def self.db_password
  @@db_password
end

.db_portObject



181
182
183
# File 'lib/pampa_workers.rb', line 181

def self.db_port
  @@db_port
end

.db_urlObject



176
177
178
# File 'lib/pampa_workers.rb', line 176

def self.db_url
  @@db_url
end

.db_userObject



191
192
193
# File 'lib/pampa_workers.rb', line 191

def self.db_user
  @@db_user
end

.division_nameObject



37
38
39
# File 'lib/pampa_workers.rb', line 37

def self.division_name() 
  @@division_name
end

.farm_external_ip_addressesObject



149
150
151
# File 'lib/pampa_workers.rb', line 149

def self.farm_external_ip_addresses()
  @@farm_external_ip_addresses
end

.get_guidObject



159
160
161
162
163
164
165
166
# File 'lib/pampa_workers.rb', line 159

def self.get_guid
  res = BlackStack::Netting::call_post(
    "#{self.api_url}/api1.4/get_guid.json",
    {'api_key' => @@api_key}
  )
  parsed = JSON.parse(res.body)
  parsed['value']        
end

.id_timezone_defaultObject



132
133
134
# File 'lib/pampa_workers.rb', line 132

def self.id_timezone_default()
  @@id_timezone_default
end

.require_db_classesObject



262
263
264
265
# File 'lib/pampa_workers.rb', line 262

def self.require_db_classes()
  # You have to load all the Sinatra classes after connect the database.

  require_relative '../lib/pampa-local.rb'
end

.set_api_key(s) ⇒ Object



92
93
94
# File 'lib/pampa_workers.rb', line 92

def self.set_api_key(s)
  @@api_key = s
end

.set_api_url(h) ⇒ Object



97
98
99
100
101
102
# File 'lib/pampa_workers.rb', line 97

def self.set_api_url(h)
  @@api_key = h[:api_key]
  @@api_protocol = h[:api_protocol]
  @@api_domain = h[:api_domain]
  @@api_port = h[:api_port]
end

.set_db_params(h) ⇒ Object

Set connection params to the central database



201
202
203
204
205
206
207
# File 'lib/pampa_workers.rb', line 201

def self.set_db_params(h)
  @@db_url = h[:db_url]
  @@db_port = h[:db_port]
  @@db_name = h[:db_name]
  @@db_user = h[:db_user]
  @@db_password = h[:db_password]
end

.set_division_name(s) ⇒ Object



42
43
44
# File 'lib/pampa_workers.rb', line 42

def self.set_division_name(s) 
  @@division_name = s
end

.set_farm_external_ip_addresses(a) ⇒ Object



154
155
156
# File 'lib/pampa_workers.rb', line 154

def self.set_farm_external_ip_addresses(a)
  @@farm_external_ip_addresses = a
end

.set_id_timezone_default(id) ⇒ Object



137
138
139
# File 'lib/pampa_workers.rb', line 137

def self.set_id_timezone_default(id)
  @@id_timezone_default = id
end

.set_storage_folder(path) ⇒ Object



117
118
119
# File 'lib/pampa_workers.rb', line 117

def self.set_storage_folder(path)
  @@storage_folder = path
end

.set_storage_sub_folders(a) ⇒ Object



120
121
122
# File 'lib/pampa_workers.rb', line 120

def self.set_storage_sub_folders(a)
  @@storage_sub_folders = a
end

.set_timeout(n) ⇒ Object



51
52
53
# File 'lib/pampa_workers.rb', line 51

def self.set_timeout(n) 
  @@timeout = n
end

.storage_folderObject



109
110
111
# File 'lib/pampa_workers.rb', line 109

def self.storage_folder()
  @@storage_folder
end

.storage_sub_foldersObject



112
113
114
# File 'lib/pampa_workers.rb', line 112

def self.storage_sub_folders()
  @@storage_sub_folders
end

.timeoutObject



46
47
48
# File 'lib/pampa_workers.rb', line 46

def self.timeout() 
  @@timeout
end