pampa_workers 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ module BlackStack
2
+
3
+ class RemoteDivision
4
+ attr_accessor :name
5
+ include BlackStack::BaseDivision
6
+ end # RemoteDivision
7
+
8
+ end # module BlackStack
@@ -0,0 +1,8 @@
1
+ module BlackStack
2
+
3
+ class RemoteWorker
4
+ attr_accessor :id, :process, :last_ping_time, :name, :active, :id_division, :assigned_process, :id_client, :division_name, :ws_url, :ws_port, :division
5
+ include BlackStack::BaseWorker
6
+ end # Remote Worker
7
+
8
+ end # module BlackStack
@@ -0,0 +1,8 @@
1
+ module BlackStack
2
+ class Role < Sequel::Model(:role)
3
+ BlackStack::Role.dataset = BlackStack::Role.dataset.disable_insert_output
4
+
5
+ ROLE_PRISMA_USER = "prisma.user"
6
+
7
+ end
8
+ end # module BlackStack
@@ -0,0 +1,151 @@
1
+ module BlackStack
2
+ class Timezone < Sequel::Model(:timezone)
3
+ BlackStack::Timezone.dataset = BlackStack::Timezone.dataset.disable_insert_output
4
+
5
+ # Recibe un string con formato "+HH:MM".
6
+ # Retorna la cantidad de horas como un numero real, con decimales.
7
+ def self.descToFloat(s)
8
+ sign = s[0]
9
+ if (sign=="+")
10
+ n = 1.0
11
+ else
12
+ n = -1.0
13
+ end
14
+ hours = s[1..2]
15
+ minutes = s[4..5]
16
+ int_part = hours.to_f
17
+ dec_part = minutes.to_f / 60.00
18
+ ret = n*(int_part + dec_part)
19
+ return ret.to_f
20
+ end
21
+
22
+ # Recibe la cantidad de horas como un numero real, con decimales.
23
+ # Retorna un string con formato "+HH:MM".
24
+ def self.floatToDesc(x)
25
+ int_part = x.to_i
26
+ dec_part = (x-int_part.to_f).round(2).abs
27
+ hours = int_part
28
+ minutes = (dec_part * 60.00).to_i
29
+ if (hours<0)
30
+ desc = "%03d" % hours
31
+ else
32
+ desc = "+" + "%02d" % hours
33
+ end
34
+ desc += ":" + "%02d" % minutes
35
+ return desc
36
+ end
37
+
38
+ # Example: '-03:00'
39
+ # Example: ' 08:00'
40
+ def self.getDatabaseOffset()
41
+ dbtime = DB["select SYSDATETIMEOFFSET() AS o"].first[:o].to_s
42
+ #puts ""
43
+ #puts ""
44
+ #puts "dbtime:#{dbtime.to_s}:."
45
+ #puts ""
46
+ #puts ""
47
+ ret = "#{dbtime[-5..-3]}:#{dbtime[-2..-1]}"
48
+ ret[0] = '+' if ret[0]!='-'
49
+ #puts ""
50
+ #puts ""
51
+ #puts "dbtime:#{ret.to_s}:."
52
+ #puts ""
53
+ #puts ""
54
+ return ret
55
+ end
56
+
57
+ # Convierte el atributo offset en un string con formato de hora.
58
+ # Ejemplos: 5.0 => 05:00, 5.5 => 05:30, -5.75 => -05:45
59
+ def offsetDescription()
60
+ return BlackStack::Timezone.floatToDesc(self.offset.to_f)
61
+ end
62
+
63
+ # Convierte una hora almacenada en el servidor de bases de datos, a la hora en esta zona horaria.
64
+ # Recibe un string con formato "+HH:MM".
65
+ # Retorna un string con formato "+HH:MM".
66
+ def convertFromThisTimeZoneToDatabaseTime(s_time_in_client_timezone)
67
+ f_time_in_client_timezone = BlackStack::Timezone.descToFloat(s_time_in_client_timezone)
68
+ s = BlackStack::Timezone.getDatabaseOffset()
69
+ x = BlackStack::Timezone.descToFloat(s) # database offset
70
+ y = self.offset # client offset
71
+ z = y - x
72
+ return BlackStack::Timezone.floatToDesc((f_time_in_client_timezone.to_f + z.to_f).modulo(24))
73
+ end
74
+
75
+ # Convierte una hora almacenada en el servidor de bases de datos, a la hora en esta zona horaria.
76
+ # Recibe un string con formato "+HH:MM".
77
+ # Retorna un string con formato "+HH:MM".
78
+ def convertFromDatabaseTimeToThisTimeZone(s_time_in_database)
79
+ f_time_in_database = BlackStack::Timezone.descToFloat(s_time_in_database)
80
+ s = BlackStack::Timezone.getDatabaseOffset()
81
+ x = BlackStack::Timezone.descToFloat(s) # database offset
82
+ y = self.offset # client offset
83
+ z = y - x
84
+ return BlackStack::Timezone.floatToDesc((f_time_in_database.to_f - z.to_f).modulo(24))
85
+ end
86
+
87
+ # Convierte una fecha-hora almacenada en el servidor de bases de datos, a la hora en esta zona horaria.
88
+ # Recibe un string con formato "YYYY-MM-DD HH:MM:SS".
89
+ # Retorna un string con formato "YYYY-MM-DD HH:MM:SS".
90
+ def convertFromDatabaseDateTimeToThisTimeZone(s_datetime_in_database)
91
+ s = BlackStack::Timezone.getDatabaseOffset() # Example: '-03:00'
92
+ #puts ""
93
+ #puts ""
94
+ #puts "s:#{s.to_s}:."
95
+ #puts ""
96
+ #puts ""
97
+ x = BlackStack::Timezone.descToFloat(s) # database offset. Number of hours, with decimals
98
+ #puts ""
99
+ #puts ""
100
+ #puts "x:#{x.to_s}:."
101
+ #puts ""
102
+ #puts ""
103
+ y = self.offset # client offset
104
+ #puts ""
105
+ #puts ""
106
+ #puts "y:#{y.to_s}:."
107
+ #puts ""
108
+ #puts ""
109
+ z = y - x
110
+ #puts ""
111
+ #puts ""
112
+ #puts "z:#{z.to_s}:."
113
+ #puts ""
114
+ #puts ""
115
+ ret = DB["SELECT DATEADD(HH, #{z.to_s}, '#{s_datetime_in_database}') AS o"].first[:o].to_s[0..18]
116
+ #puts ""
117
+ #puts ""
118
+ #puts "convertFromDatabaseDateTimeToThisTimeZone:#{ret}:."
119
+ #puts ""
120
+ #puts ""
121
+
122
+ return ret
123
+ end
124
+
125
+ #
126
+ def get_now()
127
+ datetime1 = DB["SELECT GETDATE() AS o"].first[:o].to_s[0..18]
128
+ datetime2 = self.convertFromDatabaseDateTimeToThisTimeZone(datetime1)
129
+ #puts ""
130
+ #puts ""
131
+ #puts "timezone:#{self.large_description}:."
132
+ #puts "datetime1:#{datetime1}:."
133
+ #puts "datetime2:#{datetime2}:."
134
+ #puts ""
135
+ #puts ""
136
+ yy = datetime2[0..3] # yyyy-mm-dd hh:mi:ss
137
+ mm = datetime2[5..6]
138
+ dd = datetime2[8..9]
139
+ hh = datetime2[11..12]
140
+ mi = datetime2[14..15]
141
+ ss = datetime2[17..18]
142
+ #puts ""
143
+ #puts ""
144
+ #puts "get_now:#{yy}-#{mm}-#{dd} #{hh}:#{mi}:#{ss}:."
145
+ #puts ""
146
+ #puts ""
147
+ return Time.new(yy,mm,dd,hh,mi,ss)
148
+ end
149
+
150
+ end
151
+ end # module BlackStack
@@ -0,0 +1,25 @@
1
+ module BlackStack
2
+ class User < Sequel::Model(:user)
3
+ BlackStack::User.dataset = BlackStack::User.dataset.disable_insert_output
4
+ many_to_one :client, :class=>:'BlackStack::Client', :key=>:id_client
5
+ one_to_many :user_roles, :class=>:'BlackStack::UserRole', :key=>:id_user
6
+
7
+ # TODO: agregar todos los arrays de un usuario
8
+ # => one_to_many :searches, :class=>:Search, :key=>:id_user
9
+ # => one_to_many :emlist, :class=>:EmList, :key=>:id_user
10
+ # => one_to_many :emlist, :class=>:EmList, :key=>:id_user
11
+ # => etc.
12
+
13
+ # retorna la primera division habilitada a la que pertenezca este usuario
14
+ def division
15
+ row = DB[
16
+ "SELECT d.id " +
17
+ "FROM division d WITH (NOLOCK) " +
18
+ "JOIN user_division ud WITH (NOLOCK) ON (d.id=ud.id_division AND ud.id_user='#{self.id}') " +
19
+ "WHERE ISNULL(d.available,0) = 1 "
20
+ ].first
21
+ return nil if row.nil?
22
+ return BlackStack::Division.where(:id=>row[:id]).first if !row.nil?
23
+ end
24
+ end # class User
25
+ end # module BlackStack
@@ -0,0 +1,5 @@
1
+ module BlackStack
2
+ class UserDivision < Sequel::Model(:user_division)
3
+ BlackStack::UserDivision.dataset = BlackStack::UserDivision.dataset.disable_insert_output
4
+ end
5
+ end # module BlackStack
@@ -0,0 +1,8 @@
1
+ module BlackStack
2
+ class UserRole < Sequel::Model(:user_role)
3
+ BlackStack::UserRole.dataset = BlackStack::UserRole.dataset.disable_insert_output
4
+ many_to_one :user, :class=>:'BlackStack::User', :key=>:id_user
5
+ many_to_one :role, :class=>:'BlackStack::Role', :key=>:id_role
6
+
7
+ end
8
+ end # module BlackStack
@@ -0,0 +1,143 @@
1
+ module BlackStack
2
+
3
+ #
4
+ class WorkerJob < Sequel::Model(:workerjob)
5
+
6
+ end
7
+
8
+ #
9
+ class Worker < Sequel::Model(:worker)
10
+ include BlackStack::BaseWorker
11
+ BlackStack::Worker.dataset = BlackStack::Worker.dataset.disable_insert_output
12
+ many_to_one :division, :class=>:'BlackStack::Division', :key=>:id_division
13
+ many_to_one :user, :class=>:'BlackStack::User', :key=>:id_user
14
+ many_to_one :client, :class=>:'BlackStack::Client', :key=>:id_client
15
+ many_to_one :owner, :class=>:'BlackStack::Client', :key=>:id_client_owner
16
+ many_to_one :host, :class=>:'BlackStack::LocalHost', :key=>:id_host
17
+ many_to_one :current_job, :class=>:'BlackStack::WorkerJob', :key=>:id_workerjob
18
+ many_to_one :lnuser, :class=>:'BlackStack::LnUser', :key=>:id_lnuser
19
+ many_to_one :proxy, :class=>:'BlackStack::Proxy', :key=>:id_proxy
20
+
21
+ # Usage seconds of all the workers assigned to the client.
22
+ # Note that the same worker may has been assigned to different clients withing the same timeframe.
23
+ # This method will sum the seconds used by this client only
24
+ def self.client_usage_seconds(id_client, period='M', units=1)
25
+ row = DB[
26
+ "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 " +
27
+ "from workerjob j with (nolock) " +
28
+ "where j.id_client = '#{id_client}' " +
29
+ "and j.create_time > dateadd(#{period}#{period}, -#{units.to_s}, getdate()) " +
30
+ "and j.job_start_time is not null " +
31
+ "and j.job_end_time is not null "
32
+ ].first
33
+ row[:used_seconds].to_f
34
+ end
35
+
36
+ # Average usage ratio of all the workers assigned to the client.
37
+ # Note that the same worker may has been assigned to different clients withing the same timeframe.
38
+ # This method will compute the seconds used by this client only, over the total timeframe.
39
+ def self.client_usage_ratio(id_client, period='M', units=1)
40
+ row = DB[
41
+ "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 " +
42
+ "from workerjob j with (nolock) " +
43
+ "where j.id_client = '#{id_client}' " +
44
+ "and j.create_time > dateadd(#{period}#{period}, -#{units.to_s}, getdate()) " +
45
+ "and j.job_start_time is not null " +
46
+ "and j.job_end_time is not null "
47
+ ].first
48
+ x = row[:used_seconds].to_f
49
+ y = row[:total_seconds].to_f
50
+ 100.to_f * x / y
51
+ end
52
+
53
+ # Usage ratio this worker by this client.
54
+ # Note that the same worker may has been assigned to different clients withing the same timeframe.
55
+ # This method will sum the seconds used by this client only.
56
+ def usage_seconds(id_client, period='M', units=1)
57
+ row = DB[
58
+ "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 " +
59
+ "from workerjob j with (nolock) " +
60
+ "where j.id_client = '#{id_client}' " +
61
+ "and j.id_worker = '#{self.id}' " +
62
+ "and j.create_time > dateadd(#{period}#{period}, -#{units.to_s}, getdate()) " +
63
+ "and j.job_start_time is not null " +
64
+ "and j.job_end_time is not null "
65
+ ].first
66
+ row[:used_seconds].to_f
67
+ end
68
+
69
+ # Usage ratio this worker by this client.
70
+ # Note that the same worker may has been assigned to different clients withing the same timeframe.
71
+ # This method will compute the seconds used by this client only, over the total timeframe.
72
+ def usage_ratio(id_client, period='M', units=1)
73
+ row = DB[
74
+ "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 " +
75
+ "from workerjob j with (nolock) " +
76
+ "where j.id_client = '#{id_client}' " +
77
+ "and j.id_worker = '#{self.id}' " +
78
+ "and j.create_time > dateadd(#{period}#{period}, -#{units.to_s}, getdate()) " +
79
+ "and j.job_start_time is not null " +
80
+ "and j.job_end_time is not null "
81
+ ].first
82
+ x = row[:used_seconds].to_f
83
+ y = row[:total_seconds].to_f
84
+ 100.to_f * x / y
85
+ end
86
+
87
+ #
88
+ def self.create(h)
89
+ w = BlackStack::Worker.where(:name=>h['name']).first
90
+ if w.nil?
91
+ w = BlackStack::Worker.new
92
+ w.id = h['id']
93
+ end
94
+ w.name = h['name']
95
+ w.process = h['process']
96
+ w.last_ping_time = h['last_ping_time']
97
+ w.assigned_process = h['assigned_process']
98
+ w.id_client = h['id_client']
99
+ w.id_division = h['id_division']
100
+ w.division_name = h['division_name']
101
+ w.save
102
+ end
103
+
104
+ #
105
+ def to_hash
106
+ h = {}
107
+ h['id'] = self.id
108
+ h['name'] = self.name
109
+ h['process'] = self.process
110
+ h['last_ping_time'] = self.last_ping_time
111
+ h['assigned_process'] = self.assigned_process
112
+ h['id_client'] = self.id_client
113
+ h['id_division'] = self.id_division
114
+ h['division_name'] = self.division_name
115
+ h
116
+ end
117
+
118
+ # Retorna true si este worker esta corriendo en nuestros propios servidores,
119
+ # Retorna false si este worker esta correiendo en otro host, asumiendo que es el host del cliente.
120
+ # Comparando la pulic_ip_address del worer con la lista en BlackStack::Pampa::set_farm_external_ip_addresses.
121
+ def hosted?
122
+ BlackStack::Pampa::farm_external_ip_addresses.include?(self.public_ip_address)
123
+ end # hosted?
124
+
125
+ # Retorna la cantidad de minutos desde que este worker envio una senial de vida.
126
+ # Este metodo se usa para saber si un worker esta activo o no.
127
+ def last_ping_minutes()
128
+ q = "SELECT DATEDIFF(mi, p.last_ping_time, getdate()) AS minutes FROM worker p WHERE p.id='#{self.id}'"
129
+ return DB[q].first[:minutes].to_i
130
+ end
131
+
132
+ # returns true if this worker had got a ping within the last 5 minutes
133
+ def active?
134
+ self.last_ping_minutes < BlackStack::BaseWorker::KEEP_ACTIVE_MINUTES
135
+ end
136
+
137
+ # envia una senial de vida a la division
138
+ def ping()
139
+ DB.execute("UPDATE worker SET last_ping_time=GETDATE() WHERE id='#{self.id}'")
140
+ end
141
+ end # class Worker
142
+
143
+ end # module BlackStack
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pampa_workers
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Leandro Daniel Sardi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-01-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: websocket
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.2.8
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 1.2.8
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: 1.2.8
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 1.2.8
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: 1.8.1
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.8.1
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: 1.8.1
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.8.1
53
+ - !ruby/object:Gem::Dependency
54
+ name: tiny_tds
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - "~>"
58
+ - !ruby/object:Gem::Version
59
+ version: 1.0.5
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 1.0.5
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: 1.0.5
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 1.0.5
73
+ - !ruby/object:Gem::Dependency
74
+ name: sequel
75
+ requirement: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: 4.28.0
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 4.28.0
83
+ type: :runtime
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 4.28.0
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 4.28.0
93
+ - !ruby/object:Gem::Dependency
94
+ name: simple_host_monitoring
95
+ requirement: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - "~>"
98
+ - !ruby/object:Gem::Version
99
+ version: 1.1.1
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 1.1.1
103
+ type: :runtime
104
+ prerelease: false
105
+ version_requirements: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 1.1.1
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 1.1.1
113
+ description: 'THIS GEM IS STILL IN DEVELOPMENT STAGE. Find documentation here: https://github.com/leandrosardi/pampa.'
114
+ email: leandro.sardi@expandedventure.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - lib/basedivision.rb
120
+ - lib/baseworker.rb
121
+ - lib/client.rb
122
+ - lib/division.rb
123
+ - lib/login.rb
124
+ - lib/mybotprocess.rb
125
+ - lib/mychildprocess.rb
126
+ - lib/mycrawlprocess.rb
127
+ - lib/mylocalprocess.rb
128
+ - lib/myparentprocess.rb
129
+ - lib/myprocess.rb
130
+ - lib/myremoteprocess.rb
131
+ - lib/pampa-local.rb
132
+ - lib/pampa_workers.rb
133
+ - lib/params.rb
134
+ - lib/remotedivision.rb
135
+ - lib/remoteworker.rb
136
+ - lib/role.rb
137
+ - lib/timezone.rb
138
+ - lib/user.rb
139
+ - lib/userdivision.rb
140
+ - lib/userrole.rb
141
+ - lib/worker.rb
142
+ homepage: https://rubygems.org/gems/pampa_workers
143
+ licenses:
144
+ - MIT
145
+ metadata: {}
146
+ post_install_message:
147
+ rdoc_options: []
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ required_rubygems_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ requirements: []
161
+ rubyforge_project:
162
+ rubygems_version: 2.4.5.1
163
+ signing_key:
164
+ specification_version: 4
165
+ summary: THIS GEM IS STILL IN DEVELOPMENT STAGE. Ruby library for distributing computing,
166
+ supporting dynamic reconfiguration, distribution of the computation jobs, error
167
+ handling, job-retry and fault tolerance, fast (non-direct) communication to ensure
168
+ real-time capabilities.
169
+ test_files: []