Method: CEML::Driver#ping

Defined in:
lib/ceml/driver.rb

#ping(script_collection_id, roleset, candidate, involvement = :sticky) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/ceml/driver.rb', line 83

def ping script_collection_id, roleset, candidate, involvement = :sticky
  puts "CEML: TOP PING: #{candidate.inspect}"
  return unless roleset.any?{ |r| r.fits? candidate }
  candidate[:ts] = CEML.clock
  already_launched_with = nil
  run_after = []

  puts "CEML: INSIDE PING"
  with_confluences script_collection_id, roleset do |confluences|
    live_with = confluences.select{ |c| c.live_with?(candidate) }
    if not live_with.empty?
      puts "CEML: LIVE WITH DETECTED"
      already_launched_with = live_with.first.incident_id
      live_with.each{ |c| c.rm candidate } if involvement != :sticky
      break if involvement != :released
    end

    locs = confluences.group_by{ |l| l.stage_with_candidate(candidate) }
    if locs[:joinable]
      log "joining..."
      first = locs[:joinable].shift
      first.push candidate unless first.incident_id == already_launched_with
      # JOIN THEM
      run_after << [:push, first.incident_id, nil, candidate]

    elsif locs[:launchable]
      log "launching..."
      first = locs[:launchable].shift
      first.push candidate
      cast = first.cast
      first.incident_id = gen_code
      (locs[:launchable] + (locs[:listable]||[])).each{ |l| l.rm *cast }
      # LAUNCH
      run_after << [:launch, first.incident_id, script_collection_id, roleset, *cast]

    elsif locs[:listable]
      log "listing..."
      locs[:listable].each{ |l| l.push candidate }

    else
      c = Confluence.new(roleset)
      log "start-listing..."
      if c.stage_with_candidate(candidate) == :launchable
        log "start-launching..."
        c.push candidate
        c.incident_id = gen_code
        run_after << [:launch, c.incident_id, script_collection_id, roleset, candidate]
      else
        c.push candidate
      end
      confluences << c
    end
    confluences.delete_if(&:over?)
  end

  run_after.each do |cmd|
    send(*cmd)
  end

  if already_launched_with and involvement == :sticky
    puts "PUSHING INSTEAD"
    push already_launched_with, nil, candidate
  end
end