Class: Chef::Application::WindowsService

Inherits:
Win32::Daemon
  • Object
show all
Includes:
Mixlib::CLI
Defined in:
lib/chef/application/windows_service.rb

Instance Method Summary collapse

Instance Method Details

#service_initObject



60
61
62
63
64
65
66
# File 'lib/chef/application/windows_service.rb', line 60

def service_init
  @service_action_mutex = Mutex.new
  @service_signal = ConditionVariable.new

  reconfigure
  Chef::Log.info("Chef Client Service initialized")
end

#service_main(*startup_parameters) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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
# File 'lib/chef/application/windows_service.rb', line 68

def service_main(*startup_parameters)
  # Chef::Config is initialized during service_init
  # Set the initial timeout to splay sleep time
  timeout = rand Chef::Config[:splay]

  while running? do
    # Grab the service_action_mutex to make a chef-client run
    @service_action_mutex.synchronize do
      begin
        Chef::Log.info("Next chef-client run will happen in #{timeout} seconds")
        @service_signal.wait(@service_action_mutex, timeout)

        # Continue only if service is RUNNING
        next if state != RUNNING

        # Reconfigure each time through to pick up any changes in the client file
        Chef::Log.info("Reconfiguring with startup parameters")
        reconfigure(startup_parameters)
        timeout = Chef::Config[:interval]

        # Honor splay sleep config
        timeout += rand Chef::Config[:splay]

        # run chef-client only if service is in RUNNING state
        next if state != RUNNING

        Chef::Log.info("Chef-Client service is starting a chef-client run...")
        run_chef_client
      rescue SystemExit => e
        # Do not raise any of the errors here in order to
        # prevent service crash
        Chef::Log.error("#{e.class}: #{e}")
      rescue Exception => e
        Chef::Log.error("#{e.class}: #{e}")
      end
    end
  end

  Chef::Log.debug("Exiting service...")
end

#service_pauseObject



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/chef/application/windows_service.rb', line 125

def service_pause
  Chef::Log.info("PAUSE request from operating system.")

  # We don't need to wake up the service_main if it's waiting
  # since this is a PAUSE signal.

  if @service_action_mutex.locked?
    Chef::Log.info("Currently a chef-client run is happening.")
    Chef::Log.info("Service will pause once it's completed.")
  else
    Chef::Log.info("Service is pausing....")
  end
end

#service_resumeObject



139
140
141
142
143
144
145
# File 'lib/chef/application/windows_service.rb', line 139

def service_resume
  # We don't need to wake up the service_main if it's waiting
  # since this is a RESUME signal.

  Chef::Log.info("RESUME signal received from the OS.")
  Chef::Log.info("Service is resuming....")
end

#service_shutdownObject



147
148
149
150
151
152
153
# File 'lib/chef/application/windows_service.rb', line 147

def service_shutdown
  Chef::Log.info("SHUTDOWN signal received from the OS.")

  # Treat shutdown similar to stop.

  service_stop
end

#service_stopObject

Control Signal Callback Methods



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/chef/application/windows_service.rb', line 113

def service_stop
  Chef::Log.info("STOP request from operating system.")
  if @service_action_mutex.try_lock
    @service_signal.signal
    @service_action_mutex.unlock
    Chef::Log.info("Service is stopping....")
  else
    Chef::Log.info("Currently a chef-client run is happening.")
    Chef::Log.info("Service will stop once it's completed.")
  end
end