Skip to content

Instantly share code, notes, and snippets.

@jrgns
Created October 21, 2014 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrgns/1db23ac564d4f2c62bf3 to your computer and use it in GitHub Desktop.
Save jrgns/1db23ac564d4f2c62bf3 to your computer and use it in GitHub Desktop.
require 'dotenv'
require 'win32/service'
require 'logger'
Dotenv.load
include Win32
LOGGER = Logger.new(ENV['LOG_FILE'] || STDOUT)
LOGGER.level = Logger::WARN
task default: :help
desc 'Show help menu'
task :help do
puts 'Available rake tasks: '
system 'rake -T'
end
namespace :service do
desc 'Start the Service'
task :start do
start
end
desc 'Stop the Service'
task :stop do
stop
end
desc 'Restart the Service'
task :restart do
stop
start
end
desc 'Install the Service'
task :install do
Service.create(definition)
puts 'Service Installed'
end
desc 'Uninstall the Service'
task :uninstall do
begin
stop
rescue => e
puts e.message
end
Service.delete(definition[:service_name])
puts 'Service Uninstalled'
end
desc 'Execute the Service'
task :execute do
begin
service = MyService.new
service.logger = LOGGER
service.mainloop
rescue Exception => e
LOGGER.error "Daemon failure exception=#{e.inspect}\n#{e.backtrace.join($RS)}"
raise
end
end
end
def start
LOGGER.info "Starting Service #{definition[:service_name]}..."
Service.start(definition[:service_name])
LOGGER.info 'Service Started'
rescue => e
LOGGER.error e.message
end
def stop
LOGGER.info "Stopping Service #{definition[:service_name]}..."
Service.stop(definition[:service_name])
LOGGER.info 'Service Stopped'
rescue => e
LOGGER.error e.message
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment