Class: BlackStack::Worker

Inherits:
Object
  • Object
show all
Includes:
BaseWorker
Defined in:
lib/worker.rb

Constant Summary

Constants included from BaseWorker

BaseWorker::KEEP_ACTIVE_MINUTES

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.client_usage_ratio(id_client, period = 'M', units = 1) ⇒ Object

Average usage ratio of all the workers assigned to the client. Note that the same worker may has been assigned to different clients withing the same timeframe. This method will compute the seconds used by this client only, over the total timeframe.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/worker.rb', line 39

def self.client_usage_ratio(id_client, period='M', units=1)
  # 

  row = DB[
    "select count(*) as total_workers " +
    "from worker w with (nolock) " +
    "where w.id_client = '#{id_client}' "
  ].first
  t = row[:total_workers].to_f      
  
  # 

  row = DB[
    "select datediff(ss, dateadd(#{period}#{period}, -#{units.to_s}, getdate()), getdate()) as total_seconds, isnull(sum(datediff(ss, j.job_start_time, j.job_end_time)), 0) as used_seconds " +
    "from workerjob j with (nolock) " +
    "where j.id_client = '#{id_client}' " +
    "and j.create_time > dateadd(#{period}#{period}, -#{units.to_s}, getdate()) " +
    "and j.job_start_time is not null " +
    "and j.job_end_time is not null "
  ].first
  
  # 

  x = row[:used_seconds].to_f
  y = row[:total_seconds].to_f
  100.to_f * ( x / t ) / y
end

.client_usage_seconds(id_client, period = 'M', units = 1) ⇒ Object

Usage seconds of all the workers assigned to the client. Note that the same worker may has been assigned to different clients withing the same timeframe. This method will sum the seconds used by this client only



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/worker.rb', line 24

def self.client_usage_seconds(id_client, period='M', units=1)
  row = DB[
    "select datediff(ss, dateadd(#{period}#{period}, -#{units.to_s}, getdate()), getdate()) as total_seconds, isnull(sum(datediff(ss, j.job_start_time, j.job_end_time)), 0) as used_seconds " +
    "from workerjob j with (nolock) " +
    "where j.id_client = '#{id_client}' " +
    "and j.create_time > dateadd(#{period}#{period}, -#{units.to_s}, getdate()) " +
    "and j.job_start_time is not null " +
    "and j.job_end_time is not null "
  ].first
  row[:used_seconds].to_f
end

.create(h) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/worker.rb', line 99

def self.create(h)
  w = BlackStack::Worker.where(:name=>h['name']).first
  if w.nil?
    w = BlackStack::Worker.new
    w.id = h['id']
  end
  w.name                = h['name']
  w.process             = h['process']
  w.last_ping_time      = h['last_ping_time']
  w.assigned_process    = h['assigned_process']
  w.id_client           = h['id_client']
  w.id_division         = h['id_division']
  w.division_name       = h['division_name']
  w.public_ip_address   = h['public_ip_address']
  w.save
end

Instance Method Details

#active?Boolean

returns true if this worker had got a ping within the last 5 minutes

Returns:

  • (Boolean)


146
147
148
# File 'lib/worker.rb', line 146

def active?
  self.last_ping_minutes < BlackStack::BaseWorker::KEEP_ACTIVE_MINUTES
end

#hosted?Boolean

Retorna true si este worker esta corriendo en nuestros propios servidores, Retorna false si este worker esta correiendo en otro host, asumiendo que es el host del cliente. Comparando la pulic_ip_address del worer con la lista en BlackStack::Pampa::set_farm_external_ip_addresses.

Returns:

  • (Boolean)


134
135
136
# File 'lib/worker.rb', line 134

def hosted?
  BlackStack::Pampa::farm_external_ip_addresses.include?(self.public_ip_address)
end

#last_ping_minutesObject

Retorna la cantidad de minutos desde que este worker envio una senial de vida. Este metodo se usa para saber si un worker esta activo o no.



140
141
142
143
# File 'lib/worker.rb', line 140

def last_ping_minutes()
  q = "SELECT DATEDIFF(mi, p.last_ping_time, getdate()) AS minutes FROM worker p WHERE p.id='#{self.id}'"
  return DB[q].first[:minutes].to_i
end

#pingObject

envia una senial de vida a la division



151
152
153
# File 'lib/worker.rb', line 151

def ping()
  DB.execute("UPDATE worker SET last_ping_time=GETDATE() WHERE id='#{self.id}'")
end

#to_hashObject



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/worker.rb', line 117

def to_hash
  h = {}
  h['id'] = self.id
  h['name'] = self.name
  h['process'] = self.process
  h['last_ping_time'] = self.last_ping_time
  h['assigned_process'] = self.assigned_process
  h['id_client'] = self.id_client
  h['id_division'] = self.id_division
  h['division_name'] = self.division_name
  h['public_ip_address'] = self.public_ip_address
  h
end

#usage_ratio(id_client, period = 'M', units = 1) ⇒ Object

Usage ratio this worker by this client. Note that the same worker may has been assigned to different clients withing the same timeframe. This method will compute the seconds used by this client only, over the total timeframe.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/worker.rb', line 83

def usage_ratio(id_client, period='M', units=1)
  row = DB[
    "select datediff(ss, dateadd(#{period}#{period}, -#{units.to_s}, getdate()), getdate()) as total_seconds, isnull(sum(datediff(ss, j.job_start_time, j.job_end_time)), 0) as used_seconds " +
    "from workerjob j with (nolock) " +
    "where j.id_client = '#{id_client}' " +
    "and j.id_worker = '#{self.id}' " + 
    "and j.create_time > dateadd(#{period}#{period}, -#{units.to_s}, getdate()) " +
    "and j.job_start_time is not null " +
    "and j.job_end_time is not null "
  ].first
  x = row[:used_seconds].to_f
  y = row[:total_seconds].to_f
  100.to_f * x / y
end

#usage_seconds(id_client, period = 'M', units = 1) ⇒ Object

Usage ratio this worker by this client. Note that the same worker may has been assigned to different clients withing the same timeframe. This method will sum the seconds used by this client only.



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/worker.rb', line 67

def usage_seconds(id_client, period='M', units=1)
  row = DB[
    "select datediff(ss, dateadd(#{period}#{period}, -#{units.to_s}, getdate()), getdate()) as total_seconds, isnull(sum(datediff(ss, j.job_start_time, j.job_end_time)), 0) as used_seconds " +
    "from workerjob j with (nolock) " +
    "where j.id_client = '#{id_client}' " +
    "and j.id_worker = '#{self.id}' " + 
    "and j.create_time > dateadd(#{period}#{period}, -#{units.to_s}, getdate()) " +
    "and j.job_start_time is not null " +
    "and j.job_end_time is not null "
  ].first
  row[:used_seconds].to_f
end