Class: Cosmos::BackgroundTasks
- Defined in:
- lib/cosmos/tools/cmd_tlm_server/background_tasks.rb
Overview
Manages starting and stopping all the background tasks which were discovered when parsing the configuration file.
Instance Method Summary collapse
-
#all ⇒ Object
Return the array of background tasks.
- #graceful_kill ⇒ Object
-
#initialize(cmd_tlm_server_config) ⇒ BackgroundTasks
constructor
A new instance of BackgroundTasks.
-
#start ⇒ Object
Start background tasks by creating a new Ruby thread for each and then calling their ‘call’ method once.
-
#stop ⇒ Object
Stop background tasks by calling their stop method and then killing their Ruby threads.
Constructor Details
#initialize(cmd_tlm_server_config) ⇒ BackgroundTasks
Returns a new instance of BackgroundTasks.
20 21 22 23 |
# File 'lib/cosmos/tools/cmd_tlm_server/background_tasks.rb', line 20 def initialize(cmd_tlm_server_config) @config = cmd_tlm_server_config @threads = [] end |
Instance Method Details
#all ⇒ Object
Return the array of background tasks
57 58 59 |
# File 'lib/cosmos/tools/cmd_tlm_server/background_tasks.rb', line 57 def all @config.background_tasks end |
#graceful_kill ⇒ Object
61 62 63 |
# File 'lib/cosmos/tools/cmd_tlm_server/background_tasks.rb', line 61 def graceful_kill # This method is just here to remove warnings - background_task.stop should kill the thread end |
#start ⇒ Object
Start background tasks by creating a new Ruby thread for each and then calling their ‘call’ method once.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/cosmos/tools/cmd_tlm_server/background_tasks.rb', line 27 def start @config.background_tasks.each do |background_task| new_thread = Thread.new do background_task.thread = Thread.current begin background_task.call rescue Exception => err Logger.error "Background Task thread unexpectedly died" Cosmos.handle_fatal_exception(err) end end @threads << new_thread end end |
#stop ⇒ Object
Stop background tasks by calling their stop method and then killing their Ruby threads.
44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/cosmos/tools/cmd_tlm_server/background_tasks.rb', line 44 def stop @config.background_tasks.each do |background_task| begin background_task.stop rescue # Ignore any errors because we're about to kill the thread anyway end end @threads.each {|thread| Cosmos.kill_thread(self, thread)} @threads = [] end |