Class: PushyClient::WindowsService

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

Instance Method Summary collapse

Instance Method Details

#service_initObject



49
50
51
52
53
54
55
# File 'lib/pushy_client/windows_service.rb', line 49

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

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

#service_main(*startup_parameters) ⇒ Object



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
89
90
91
92
93
94
95
# File 'lib/pushy_client/windows_service.rb', line 57

def service_main(*startup_parameters)
  begin
    @service_action_mutex.synchronize do
      Chef::Log.info("Push Client version: #{::PushyClient::VERSION}")
      Chef::Log.info("Push Client started as service with parameters: #{startup_parameters}")
      Chef::Log.info("Push Client passed: #{ARGV.join(', ')}")
      reconfigure(startup_parameters)

      # Lifted from PushyClient::CLI
      ohai = Ohai::System.new
      ohai.load_plugins
      ohai.run_plugins(true, ['os', 'hostname'])

      @client = PushyClient.new(
                                :chef_server_url => Chef::Config[:chef_server_url],
                                :client_key      => Chef::Config[:client_key],
                                :node_name       => Chef::Config[:node_name] || ohai[:fqdn] || ohai[:hostname],
                                :whitelist       => Chef::Config[:whitelist] || { 'chef-client' => 'chef-client' },
                                :hostname        => ohai[:hostname],
                                :allow_unencrypted => Chef::Config[:allow_unencrypted]
                                )

      @client.start
      Chef::Log.info("pushy-client is started...")

      # Wait until we get service exit signal
      @service_signal.wait(@service_action_mutex)
      Chef::Log.debug("Stopping pushy-client...")
      @client.stop
    end

  rescue Exception => e
    Chef::Log.error("#{e.class}: #{e}")
    Chef::Log.error("Terminating pushy-client service...")
    Chef::Application.debug_stacktrace(e)
  end

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

#service_pauseObject



112
113
114
115
116
# File 'lib/pushy_client/windows_service.rb', line 112

def service_pause
  Chef::Log.info("PAUSE request from operating system.")
  Chef::Log.info("Pushy Client Service doesn't support PAUSE.")
  Chef::Log.info("Pushy Client Service is still running.")
end

#service_resumeObject



118
119
120
# File 'lib/pushy_client/windows_service.rb', line 118

def service_resume
  Chef::Log.info("RESUME signal received from the OS.")
end

#service_shutdownObject



122
123
124
125
126
127
128
# File 'lib/pushy_client/windows_service.rb', line 122

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



101
102
103
104
105
106
107
108
109
110
# File 'lib/pushy_client/windows_service.rb', line 101

def service_stop
  Chef::Log.info("STOP request from operating system.")
  while !@service_action_mutex.try_lock do
    Chef::Log.info("Pushy is being initialized waiting for initialization to complete.")
    sleep(1)
  end

  @service_signal.signal
  @service_action_mutex.unlock
end