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



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

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



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
96
# File 'lib/pushy_client/windows_service.rb', line 58

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



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

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



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

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

#service_shutdownObject



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

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



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

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