herdst_worker 0.2.21 → 0.2.23

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9fa8ccaf7c0b1ea579be583057020ce2499ad6d7fd1ea806f526c81d0a3adfe3
4
- data.tar.gz: 1c4af6793b81b741e308cad5db603fce45bd81dfb408ece724a4091c1c910471
3
+ metadata.gz: 1a7cc2b6da6b5a813708b3a1bea2d19e42bbdc2e8827e51cb10ff3f2d65c81f0
4
+ data.tar.gz: e97c06d7f2f48041b15331a8fe90d152e96483e04204ce86f6f278014add1792
5
5
  SHA512:
6
- metadata.gz: 7bf2ce6ce4e78f2f8b409c63d390774028e0296afd81dde6664ba962f60089a8ea275d850adfbffd5970145eb499225d4a377df229c250761db40aa592521e24
7
- data.tar.gz: 42c9f0b24900bef7ede09b10d0200cafa878f24dbd7d8506c4ea69f11555496b1c70bf6b67da800c1c2cc88c5409d5b8a1818e965cb05891fb4bf50b451acecb
6
+ metadata.gz: 05b7f71887d5d35e58099262c12308347c5817232bd5d0ea2d5a9d8dace2cbf775f8dc63e6e9a786d3eaa598fa129bdbe5f9f6fd844b6fc09fa680ac01002348
7
+ data.tar.gz: f715accff030919d5ffff2107595ec47982949b9a3e2791c8a5cd0c71811cf8c131f9e995f788a3977d6681f4a37e447eaaba342381928804b2fc1756cc3f462
data/bin/herdst_worker CHANGED
@@ -11,6 +11,16 @@ application_file = arg_pair["application"] || "application.rb"
11
11
  application_path = "#{Dir.pwd}/#{application_file}"
12
12
  application_env = arg_pair["env"] || "production"
13
13
  queue_enabled = arg_pair["queue_enabled"] == "false" ? false : true
14
+ primary_process = true
15
+ runtime = ((3600 * 4.75) + (rand * (3600 * 0.25))).floor # Between 4.75 hours and 5 hours
16
+
17
+ if arg_pair["primary"].to_s == "false"
18
+ primary_process = false
19
+ end
20
+
21
+ if arg_pair["runtime"]
22
+ runtime = arg_pair["runtime"].to_i || runtime
23
+ end
14
24
 
15
25
  raise "Please specify a name for the application E.g. --name=[name]" unless application_name
16
26
  raise "Please specify a family for the application E.g. --family=[family]" unless application_family
@@ -21,7 +31,7 @@ ENV["RAILS_ENV"] = application_env
21
31
 
22
32
  require_relative "../lib/herdst_worker"
23
33
 
24
- application_instance = HerdstWorker::Application.new(application_env, application_name, application_family, queue_enabled)
34
+ application_instance = HerdstWorker::Application.new(application_env, application_name, application_family, queue_enabled, primary_process, runtime)
25
35
 
26
36
  require_relative application_path
27
37
 
@@ -22,7 +22,7 @@ require "irb"
22
22
  require "irb/completion"
23
23
  require_relative "../lib/herdst_worker"
24
24
 
25
- application_instance = HerdstWorker::Application.new(application_env, application_name, application_family, false)
25
+ application_instance = HerdstWorker::Application.new(application_env, application_name, application_family, false, false, 0)
26
26
 
27
27
  require_relative application_path
28
28
 
@@ -4,7 +4,6 @@ require "aws-sdk-ecs"
4
4
  require_relative '../configuration/facade'
5
5
  require_relative '../log/facade'
6
6
  require_relative '../autoload/facade'
7
- require_relative '../signals/facade'
8
7
  require_relative '../queue/facade'
9
8
  require_relative '../adapters/facade'
10
9
 
@@ -15,22 +14,24 @@ module HerdstWorker
15
14
 
16
15
  attr_accessor :name, :family
17
16
  attr_accessor :logger, :config, :autoload
18
- attr_accessor :poller_enabled, :queues, :poller_url, :queue
17
+ attr_accessor :poller_enabled, :queues, :poller_url, :queue, :primary_process, :runtime
19
18
 
20
19
 
21
- def initialize(env, name, family, poller_enabled)
20
+ def initialize(env, name, family, poller_enabled, primary_process, runtime)
22
21
  self.name = name
23
22
  self.family = family
24
23
  self.poller_enabled = poller_enabled
24
+ self.runtime = runtime
25
25
  self.queues = ActiveSupport::HashWithIndifferentAccess.new
26
-
26
+
27
27
  HerdstWorker.set_application(self)
28
28
 
29
29
  self.logger = HerdstWorker::Log::Facade.new(get_logger_level(env))
30
30
  self.config = HerdstWorker::Configuration::Facade.new(env, name)
31
31
  self.autoload = HerdstWorker::Autoload::Facade.new(self)
32
32
  self.set_inflections
33
-
33
+ self.set_process_id_file
34
+
34
35
  HerdstWorker::Adapters::Facade.bootstrap(self)
35
36
  end
36
37
 
@@ -44,7 +45,7 @@ module HerdstWorker
44
45
 
45
46
  def get_queue(name)
46
47
  if self.queues[name]
47
- HerdstWorker::Queue::Facade.new(self, false, self.queues[name], 20)
48
+ HerdstWorker::Queue::Facade.new(self, false, 0, self.queues[name], 20)
48
49
  else
49
50
  nil
50
51
  end
@@ -61,9 +62,7 @@ module HerdstWorker
61
62
  if self.queue == nil
62
63
  self.logger.info "Starting Application (#{$$})"
63
64
 
64
- HerdstWorker::Signals::Facade.listen(self.config.paths.temp)
65
-
66
- self.queue = HerdstWorker::Queue::Facade.new(self, self.poller_enabled, self.poller_url, 20)
65
+ self.queue = HerdstWorker::Queue::Facade.new(self, self.poller_enabled, self.runtime, self.poller_url, 20)
67
66
  self.queue.start
68
67
  end
69
68
  end
@@ -91,7 +90,11 @@ module HerdstWorker
91
90
 
92
91
 
93
92
  def register_new_task
94
-
93
+ if self.primary_process
94
+ self.queue.stop
95
+ else
96
+ self.queue.stop
97
+ end
95
98
  end
96
99
 
97
100
 
@@ -134,6 +137,14 @@ module HerdstWorker
134
137
  return "FATAL" if env == "test"
135
138
  return "DEBUG"
136
139
  end
140
+
141
+ def set_process_id_file
142
+ # Write process id to file so we can signal the current process
143
+ process_id = "#{$$}"
144
+ process_file = self.config.paths.temp + "/process_id"
145
+
146
+ File.open(process_file, "w") { |file| file.write(process_id) }
147
+ end
137
148
 
138
149
 
139
150
  end
@@ -7,12 +7,13 @@ module HerdstWorker
7
7
  class Facade
8
8
 
9
9
 
10
- attr_accessor :app, :enabled, :url, :queue_wait_time, :processor
10
+ attr_accessor :app, :enabled, :runtime, :url, :queue_wait_time, :processor
11
11
 
12
12
 
13
- def initialize(app, enabled, url, queue_wait_time)
13
+ def initialize(app, enabled, runtime, url, queue_wait_time)
14
14
  self.app = app
15
15
  self.enabled = enabled
16
+ self.runtime = runtime
16
17
  self.url = url
17
18
  self.queue_wait_time = queue_wait_time
18
19
  end
@@ -28,11 +29,6 @@ module HerdstWorker
28
29
  end
29
30
 
30
31
 
31
- def halt
32
- self.get_processor.halt
33
- end
34
-
35
-
36
32
  def stop
37
33
  self.get_processor.stop
38
34
  end
@@ -40,7 +36,7 @@ module HerdstWorker
40
36
 
41
37
  def get_processor
42
38
  unless self.processor
43
- self.processor = HerdstWorker::Queue::Processor.new(app, self.enabled, self.url, self.queue_wait_time)
39
+ self.processor = HerdstWorker::Queue::Processor.new(app, self.enabled, self.runtime, self.url, self.queue_wait_time)
44
40
  end
45
41
 
46
42
  self.processor
@@ -9,12 +9,12 @@ module HerdstWorker
9
9
 
10
10
 
11
11
  attr_accessor :app, :enabled, :queue_url, :queue_wait_time, :poller
12
- attr_accessor :start_time, :stop_processing_at, :stop_at, :run_duration, :stop_suration
13
- attr_accessor :processor_status, :job_count, :max_jobs
12
+ attr_accessor :start_time, :run_duration, :expire_at
13
+ attr_accessor :processor_status, :processor_expired, :job_count, :max_jobs
14
14
  attr_accessor :attempt_threshold, :visibility_timeout, :ignored_notifications
15
15
 
16
16
 
17
- def initialize(app, enabled, queue_url, queue_wait_time)
17
+ def initialize(app, enabled, runtime, queue_url, queue_wait_time)
18
18
  self.app = app
19
19
  self.enabled = enabled
20
20
  self.queue_url = queue_url
@@ -27,12 +27,12 @@ module HerdstWorker
27
27
  self.ignored_notifications = [
28
28
  "AmazonSnsSubscriptionSucceeded"
29
29
  ]
30
-
31
- # Set the start time
32
- self.run_duration = (3600 * 4) + (rand * 900).floor # Four hours + bit more (< 15 minutes)
33
- self.stop_suration = 300 # 5 minutes
30
+
31
+ self.processor_status = "starting"
32
+ self.processor_expired = false
33
+ self.run_duration = runtime
34
34
  self.reset_time
35
-
35
+
36
36
  # Start the processor as working
37
37
  self.set_status "starting"
38
38
 
@@ -49,7 +49,7 @@ module HerdstWorker
49
49
  self.poller.poll(:wait_time_seconds => self.queue_wait_time, :skip_delete => false) do |msg|
50
50
  process_message(msg)
51
51
  end
52
-
52
+
53
53
  self.app.deregister_task
54
54
  else
55
55
  raise "Cannot start a queue which is not enabled"
@@ -72,14 +72,6 @@ module HerdstWorker
72
72
  end
73
73
 
74
74
 
75
- # Sets the processor status to finishing. The sqs before action will
76
- # take care of setting the idle state once all jobs have finished.
77
- def halt
78
- return if self.processor_status == "finishing"
79
- set_status "finishing"
80
- end
81
-
82
-
83
75
  # Sets the processor status to stopping. The sqs before action will
84
76
  # take care of stopping the application once all jobs have finished.
85
77
  def stop
@@ -118,23 +110,11 @@ module HerdstWorker
118
110
  # After hours of running terminate application.
119
111
  # The app will automatically restart in production
120
112
  current_time = Time.now.utc.to_i
121
-
122
- if (self.processor_status == "stopping") && (current_time >= self.stop_at)
123
- self.app.deregister_task
124
-
125
- elsif (self.processor_status == "working") && (current_time >= self.stop_processing_at)
113
+
114
+ if (self.processor_status == "working") && (current_time >= self.expire_at) && (self.processor_expired == false)
115
+ self.processor_expired = true
126
116
  self.app.force_stop(900)
127
117
  self.app.register_new_task
128
-
129
- set_status "stopping"
130
-
131
- # On finishing wait for jobs to complete and then set status to idle
132
- elsif self.processor_status == "finishing"
133
- if self.job_count == 0
134
- self.app.logger.queue.warn "Setting processor status to idle"
135
- set_status "idle"
136
- end
137
-
138
118
  end
139
119
 
140
120
  # On stopping wait for jobs to complete and then set status
@@ -233,8 +213,7 @@ module HerdstWorker
233
213
  private
234
214
  def reset_time
235
215
  self.start_time = Time.now.utc.to_i
236
- self.stop_processing_at = self.start_time + self.run_duration
237
- self.stop_at = self.stop_processing_at + self.stop_suration
216
+ self.expire_at = self.start_time + self.run_duration
238
217
  end
239
218
 
240
219
  end
@@ -1,3 +1,3 @@
1
1
  module HerdstWorker
2
- VERSION = '0.2.21'
2
+ VERSION = '0.2.23'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: herdst_worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.21
4
+ version: 0.2.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Herd.St
@@ -262,7 +262,6 @@ files:
262
262
  - lib/herdst_worker/queue/facade.rb
263
263
  - lib/herdst_worker/queue/processor.rb
264
264
  - lib/herdst_worker/queue/runner.rb
265
- - lib/herdst_worker/signals/facade.rb
266
265
  - lib/herdst_worker/version.rb
267
266
  homepage: https://herd.st
268
267
  licenses:
@@ -1,35 +0,0 @@
1
- module HerdstWorker
2
- module Signals
3
- class Facade
4
-
5
- def self.listen(process_path)
6
- write_process_file(process_path)
7
-
8
- # Start
9
- Signal.trap "USR1" do |x|
10
- HerdstWorker::Queue::Facade.start
11
- end
12
-
13
- # Halt
14
- Signal.trap "USR2" do |x|
15
- HerdstWorker::Queue::Facade.halt
16
- end
17
-
18
- # Stop (abort)
19
- Signal.trap "ABRT" do |x|
20
- HerdstWorker::Queue::Facade.stop
21
- end
22
- end
23
-
24
- private
25
- def self.write_process_file(process_path)
26
- # Write process id to file so we can signal the current process
27
- process_id = "#{$$}"
28
- process_file = process_path + "/process_id"
29
-
30
- File.open(process_file, "w") { |file| file.write(process_id) }
31
- end
32
-
33
- end
34
- end
35
- end