Module: FlockOfChefs::FlockedChef

Defined in:
lib/flock_of_chefs/flocked_chef.rb

Instance Method Summary collapse

Instance Method Details

#mutexObject



9
10
11
12
13
14
# File 'lib/flock_of_chefs/flocked_chef.rb', line 9

def mutex
  unless(@mutex)
    @mutex = Mutex.new
  end
  @mutex
end

#run_applicationObject



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/flock_of_chefs/flocked_chef.rb', line 28

def run_application
  unless Chef::Platform.windows?
    SELF_PIPE.replace IO.pipe

    trap("USR1") do
      Chef::Log.info("SIGUSR1 received, waking up")
      SELF_PIPE[1].putc('.') # wakeup master process from select
    end
  end

  if Chef::Config[:version]
    puts "Chef version: #{::Chef::VERSION}"
  end

  if Chef::Config[:daemonize]
    Chef::Daemon.daemonize("chef-client")
  end

  loop do
    begin
      if Chef::Config[:splay]
        splay = rand Chef::Config[:splay]
        Chef::Log.debug("Splay sleep #{splay} seconds")
        sleep splay
      end
      run_chef_client
      if Chef::Config[:interval]
        Chef::Log.debug("Sleeping for #{Chef::Config[:interval]} seconds")
        unless SELF_PIPE.empty?
          client_sleep Chef::Config[:interval]
        else
          # Windows
          sleep Chef::Config[:interval]
        end
      else
        Chef::Application.exit! "Exiting", 0
      end
    rescue Chef::Application::Wakeup => e
      Chef::Log.debug("Received Wakeup signal.  Starting run.")
      next
    rescue SystemExit => e
      raise
    rescue Exception => e
      if Chef::Config[:interval]
        Chef::Log.error("#{e.class}: #{e}")
        Chef::Application.debug_stacktrace(e)
        Chef::Log.error("Sleeping for #{Chef::Config[:interval]} seconds before trying again")
        unless SELF_PIPE.empty?
          client_sleep Chef::Config[:interval]
        else
          # Windows
          sleep Chef::Config[:interval]
        end
        retry
      else
        Chef::Application.debug_stacktrace(e)
        Chef::Application.fatal!("#{e.class}: #{e.message}", 1)
      end
    end
  end
end

#run_chef_clientObject



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/flock_of_chefs/flocked_chef.rb', line 15

def run_chef_client
  puts '*' * 400
  mutex.synchronize do
    @chef_client = Chef::Client.new(
      @chef_client_json, 
      :override_runlist => config[:override_runlist]
    )
    @chef_client_json = nil
    @chef_client.run
    @chef_client = nil
  end
end