Skip to content
This repository has been archived by the owner on Aug 31, 2020. It is now read-only.

Commit

Permalink
Merge pull request #6 from steakknife/fix__prevent_init_to_up_on_stop…
Browse files Browse the repository at this point in the history
….txt

prevent init->up->stop when stop.txt exists
  • Loading branch information
rkistner committed Mar 5, 2014
2 parents a763a12 + 0b4b620 commit 2a790c9
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 82 deletions.
2 changes: 2 additions & 0 deletions lib/foreman_god/conditions.rb
@@ -0,0 +1,2 @@
require 'foreman_god/conditions/foreman_stop_file_exists'
require 'foreman_god/conditions/foreman_restart_file_touched'
37 changes: 37 additions & 0 deletions lib/foreman_god/conditions/foreman_restart_file_touched.rb
@@ -0,0 +1,37 @@
require 'god'

module God
module Conditions
# Adapted from https://gist.github.com/571095
class ForemanRestartFileTouched < PollCondition
attr_accessor :restart_file

def initialize
super
end

def process_start_time
Time.parse(`ps -o lstart= -p #{self.watch.pid}`) rescue nil
end

def restart_file_modification_time
File.mtime(self.restart_file) rescue Time.at(0)
end

def valid?
valid = true
valid &= complain("Attribute 'restart_file' must be specified", self) if self.restart_file.nil?
valid
end

def test
ptime = process_start_time
if ptime
ptime < restart_file_modification_time
else
false
end
end
end
end
end
34 changes: 34 additions & 0 deletions lib/foreman_god/conditions/foreman_stop_file_exists.rb
@@ -0,0 +1,34 @@
require 'god'

module God
module Conditions
class ForemanStopFileExists < PollCondition
attr_accessor :stop_file
# invert file test result
# ---------------------------
# false exists true
# false absent false
# true exists false
# true absent true
#
# default: false
attr_accessor :invert

def initialize
super
@invert = false
end

def valid?
valid = true
valid &= complain("Attribute 'invert' must be true or fails", self) unless self.invert === true || self.invert === false
valid &= complain("Attribute 'stop_file' must be specified", self) if self.stop_file.nil?
valid
end

def test
File.exist?(stop_file) ^ invert
end
end
end
end
107 changes: 25 additions & 82 deletions lib/foreman_god/god_config.rb
Expand Up @@ -5,85 +5,15 @@
require 'thor/core_ext/hash_with_indifferent_access'
require 'god'
require 'foreman_god'
require 'foreman_god/conditions'
require 'etc'

module God

Watch::VALID_STATES << :stop unless Watch::VALID_STATES.include? :stop

module Conditions
class ForemanStopFileDoesntExist < PollCondition
attr_accessor :stop_file

def initialize
super
end

def valid?
valid = true
valid &= complain("Attribute 'stop_file' must be specified", self) if self.stop_file.nil?
valid
end

def test
! File.exist? stop_file
end
end

class ForemanStopFileExists < PollCondition
attr_accessor :stop_file

def initialize
super
end

def valid?
valid = true
valid &= complain("Attribute 'stop_file' must be specified", self) if self.stop_file.nil?
valid
end

def test
File.exist? stop_file
end
end

# Adapted from https://gist.github.com/571095
class ForemanRestartFileTouched < PollCondition
attr_accessor :restart_file

def initialize
super
end

def process_start_time
Time.parse(`ps -o lstart= -p #{self.watch.pid}`) rescue nil
end

def restart_file_modification_time
File.mtime(self.restart_file) rescue Time.at(0)
end

def valid?
valid = true
valid &= complain("Attribute 'restart_file' must be specified", self) if self.restart_file.nil?
valid
end

def test
ptime = process_start_time
if ptime
ptime < restart_file_modification_time
else
false
end
end
end
end
end


module ForemanGod

class GodConfig
attr_reader :dir_name, :options, :engine

Expand Down Expand Up @@ -133,6 +63,21 @@ def group_name
Etc.getgrgid(gid).name
end

def file_dir(process)
File.join(process.cwd, 'tmp')
end

def stop_file(process)
File.join(file_dir(process), 'stop.txt')
end

def stop_file?(process)
File.exists?(stop_file(process))
end

def restart_file(process)
File.join(file_dir(process), 'restart.txt')
end

def wrap_command(cmd)
if user_name
Expand Down Expand Up @@ -190,29 +135,29 @@ def watch_process(name, process, n)
w.transition(:up, :stop) do |stop|
stop.condition(:foreman_stop_file_exists) do |c|
c.interval = 5.seconds
# Should we make this path configurable? and DRY it up
c.stop_file = File.join(process.cwd, 'tmp', 'stop.txt')
c.stop_file = stop_file(process)
end
end

w.transition(:stop, :up) do |start|
start.condition(:foreman_stop_file_doesnt_exist) do |c|
start.condition(:foreman_stop_file_exists) do |c|
c.interval = 5.seconds
# Should we make this path configurable? and DRY it up
c.stop_file = File.join(process.cwd, 'tmp', 'stop.txt')
c.invert = true # absence
c.stop_file = stop_file(process)
end
end

w.transition(:up, :restart) do |on|
on.condition(:foreman_restart_file_touched) do |c|
c.interval = 5.seconds
# Should we make this path configurable?
c.restart_file = File.join(process.cwd, 'tmp', 'restart.txt')
c.restart_file = restart_file(process)
end
end

# determine the state on startup
w.transition(:init, { true => :up, false => :start }) do |on|
w.transition(:init, { true => stop_file?(process) ? :stop : :up,
false => stop_file?(process) ? :stop : :start }
) do |on|
on.condition(:process_running) do |c|
c.running = true
end
Expand All @@ -239,7 +184,6 @@ def watch_process(name, process, n)
c.running = false
end
end

end
end

Expand All @@ -253,4 +197,3 @@ def watch
end
end


0 comments on commit 2a790c9

Please sign in to comment.