Class: Cosmos::BackgroundTasks

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(cmd_tlm_server_config) ⇒ BackgroundTasks

Returns a new instance of BackgroundTasks.

Parameters:

  • cmd_tlm_server_config (CmdTlmServerConfig)

    The command telemetry server configuration



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

#allObject

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_killObject



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

#startObject

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

#stopObject

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