Class: SessionRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/bolt_train_runner/session_runner.rb

Overview

This class manages a thread which monitors a directory that the bolt-train-api will place session files in. As these files show up, the thread should read them and issue the commands, delete the session file, then move to the next oldest file in the directory.

Instance Method Summary collapse

Constructor Details

#initialize(comms, session_dir, log) ⇒ SessionRunner

Returns a new instance of SessionRunner.



18
19
20
21
22
23
24
25
26
# File 'lib/bolt_train_runner/session_runner.rb', line 18

def initialize(comms, session_dir, log)
  raise 'comms must not be nil' if comms.nil?
  raise 'session_dir must not be nil' if session_dir.nil?
  raise 'session_dir does not exist' unless File.exist?(session_dir)

  @session_dir = session_dir
  @comms = comms
  @log = log
end

Instance Method Details

#run_threadObject



38
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/bolt_train_runner/session_runner.rb', line 38

def run_thread
  while !@kill_thread
    files = Dir["#{@session_dir}/*"].sort_by { |f| File.mtime(f) }
    files.each do |f|
      begin
        data = JSON.parse(File.read(f))
      rescue
        next
      end
      session = data['session']
      email = session['email']
      @log.info("[Session Runner] Starting session for #{email}")
      commands = session['commands']
      commands.each do |args|
        c = args['command']
        args.delete('command')
        @log.info("[Session Runner] Sending command #{c}")
        @log.info("[Session Runner] Arguments = #{args}")
        Commands.send(c, args, @comms, @log)
      end
      File.delete(f)
      @log.info("[Session Runner] Session for #{email} complete")
    end
  end
end

#startObject



28
29
30
# File 'lib/bolt_train_runner/session_runner.rb', line 28

def start
  @session_thread = Thread.new { run_thread }
end

#stopObject



32
33
34
35
36
# File 'lib/bolt_train_runner/session_runner.rb', line 32

def stop
  @kill_thread = true
  @log.info('Stopping Session Runner')
  @session_thread.join if @session_thread
end