# class Chef::Application::WindowsService

### Public Instance Methods

service_init() click to toggle source

    # File lib/chef/application/windows_service.rb, line 62
    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) click to toggle source

    # File lib/chef/application/windows_service.rb, line 70
    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

      # Daemon class needs to have all the signal callbacks return
      # before service_main returns.
      Chef::Log.debug("Giving signal callbacks some time to exit...")
      sleep 1
      Chef::Log.debug("Exiting service...")
    end

service_pause() click to toggle source

    # File lib/chef/application/windows_service.rb, line 143
    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_resume() click to toggle source

    # File lib/chef/application/windows_service.rb, line 157
    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_shutdown() click to toggle source

    # File lib/chef/application/windows_service.rb, line 165
    def service_shutdown
      Chef::Log.info("SHUTDOWN signal received from the OS.")

      # Treat shutdown similar to stop.

      service_stop
    end

service_stop() click to toggle source

Control Signal Callback Methods

    # File lib/chef/application/windows_service.rb, line 119
    def service_stop
      run_warning_displayed = false
      Chef::Log.info("STOP request from operating system.")
      loop do
        # See if a run is in flight
        if @service_action_mutex.try_lock
          # Run is not in flight. Wake up service_main to exit.
          @service_signal.signal
          @service_action_mutex.unlock
          break
        else
          unless run_warning_displayed
            Chef::Log.info("Currently a chef run is happening on this system.")
            Chef::Log.info("Service  will stop when run is completed.")
            run_warning_displayed = true
          end

          Chef::Log.debug("Waiting for chef-client run...")
          sleep 1
        end
      end
      Chef::Log.info("Service is stopping....")
    end