Class: BackgroundServices::TeamController

Inherits:
Object
  • Object
show all
Includes:
DRbUndumped
Defined in:
lib/background_services/team_controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(team) ⇒ TeamController

Returns a new instance of TeamController.



8
9
10
# File 'lib/background_services/team_controller.rb', line 8

def initialize(team)
  @team = team
end

Instance Attribute Details

#teamObject

Returns the value of attribute team.



6
7
8
# File 'lib/background_services/team_controller.rb', line 6

def team
  @team
end

Instance Method Details

#check_processObject



20
21
22
23
24
25
26
# File 'lib/background_services/team_controller.rb', line 20

def check_process
  return unless running?
  pid, status = Process.waitpid2(@pid, Process::WNOHANG)
  if !status.nil? and status.exited?
    @running = false
  end
end

#running?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/background_services/team_controller.rb', line 12

def running?
  @running || false
end

#startObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/background_services/team_controller.rb', line 28

def start
  return if running?
  @team.bind_service
  @team.groups.each do |group|
    BackgroundServices::Server.instance.groups[group].register_team(self)
  end
  
  BackgroundServices::Server.logger.debug "will start team"
  @pid = fork do
    Signal.trap("TERM") do
      BackgroundServices::Server.logger.debug "will stop team"
      @team.stop
    end
    @team.start
    BackgroundServices::Server.logger.debug "did start team"
    @team.join
    BackgroundServices::Server.logger.debug "did stop team"
    exit!
  end
  @running = true
end

#stopObject



50
51
52
53
54
55
56
57
58
59
60
# File 'lib/background_services/team_controller.rb', line 50

def stop
  return unless running?
  begin
    Process.kill("TERM", @pid)
    Process.waitpid(@pid, 0)
  rescue Errno::ECHILD, Errno::ESRCH
    # already dead
  ensure
    @running = false
  end
end

#team_idObject



16
17
18
# File 'lib/background_services/team_controller.rb', line 16

def team_id
  @team.id
end