Class: BlueGreenProcess::MasterProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/blue_green_process/master_process.rb

Instance Method Summary collapse

Constructor Details

#initialize(worker_instance:, max_work:) ⇒ MasterProcess

Returns a new instance of MasterProcess.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/blue_green_process/master_process.rb', line 5

def initialize(worker_instance:, max_work:)
  blue = fork_process(label: :blue, worker_instance: worker_instance)
  green = fork_process(label: :green, worker_instance: worker_instance)

  @stage_state = true
  @stage = {
    true => blue.be_active,
    false => green.be_inactive
  }
  @processes = @stage.values
  @max_work = max_work
end

Instance Method Details

#fork_process(label:, worker_instance:) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
63
64
65
66
67
68
# File 'lib/blue_green_process/master_process.rb', line 18

def fork_process(label:, worker_instance:)
  child_read, parent_write = IO.pipe
  parent_read, child_write = IO.pipe

  pid = fork do
    BlueGreenProcess.config.after_fork.call

    parent_write.close
    parent_read.close
    process_status = :inactive

    loop do
      data = child_read.gets&.strip
      case data
      when BlueGreenProcess::PROCESS_COMMAND_DIE, nil, ""
        BlueGreenProcess.debug_log "#{label}'ll die(#{$PROCESS_ID})"
        exit 0
      when BlueGreenProcess::PROCESS_COMMAND_BE_ACTIVE
        process_status = BlueGreenProcess::PROCESS_STATUS_ACTIVE
        BlueGreenProcess.debug_log "#{label}'ll be active(#{$PROCESS_ID})"
        child_write.puts BlueGreenProcess::PROCESS_RESPONSE
        ::GC.disable
      when BlueGreenProcess::PROCESS_COMMAND_BE_INACTIVE
        process_status = BlueGreenProcess::PROCESS_STATUS_INACTIVE
        BlueGreenProcess.debug_log "#{label}'ll be inactive(#{$PROCESS_ID})"
        child_write.puts BlueGreenProcess::PROCESS_RESPONSE
        ::GC.enable
        ::GC.start
      when BlueGreenProcess::PROCESS_COMMAND_WORK
        if process_status == BlueGreenProcess::PROCESS_STATUS_INACTIVE
          warn "Should not be able to run in this status"
        end
        # too verbose
        # BlueGreenProcess.debug_log "#{label}'ll work(#{$PROCESS_ID})"
        worker_instance.work(*label)
        child_write.puts BlueGreenProcess::PROCESS_RESPONSE
      else
        child_write.puts "NG"
        puts "unknown. from #{label}(#{$PROCESS_ID})"
        exit 1
      end
    end

    exit 0
  end

  child_write.close
  child_read.close

  BlueGreenProcess::WorkerProcess.new(pid, label, parent_read, parent_write)
end

#shutdownObject



80
81
82
83
84
# File 'lib/blue_green_process/master_process.rb', line 80

def shutdown
  @processes.each do |process|
    process.wpipe.puts(BlueGreenProcess::PROCESS_COMMAND_DIE)
  end
end

#workObject



70
71
72
73
74
75
76
77
78
# File 'lib/blue_green_process/master_process.rb', line 70

def work
  active_process do |process|
    @max_work.times do
      process.work
    end
  end

  true
end