Skip to content

Instantly share code, notes, and snippets.

@nvurgaft nvurgaft/service.rb
Created Jan 30, 2018

Embed
What would you like to do?
Runs a barebone Redmine installation as a Windows service and opens it to the outside on port 3000
REDMINE_DIR = 'C:\redmine\redmine-3.4.4\redmine-3.4.4'
LOG_FILE = "#{REDMINE_DIR}\\log\\service.log"
begin
require 'win32/daemon'
include Win32
class RedmineService < Daemon
def service_init
File.open(LOG_FILE, 'a'){ |f| f.puts "Initializing service #{Time.now}" }
@server_pid = Process.spawn 'ruby bin/rails s -b 0.0.0.0 -e production', :chdir => REDMINE_DIR, :err => [LOG_FILE, 'a']
end
def service_main
File.open(LOG_FILE, 'a'){ |f| f.puts "Service is running #{Time.now} with pid #{@server_pid}" }
while running?
sleep 10
end
end
def service_stop
File.open(LOG_FILE, 'a'){ |f| f.puts "Stopping server thread #{Time.now}" }
system "taskkill /PID #{@server_pid} /T /F"
Process.waitall
File.open(LOG_FILE, 'a'){ |f| f.puts "Service stopped #{Time.now}" }
exit!
end
end
RedmineService.mainloop
rescue Exception => e
File.open(LOG_FILE,'a+'){ |f| f.puts " ***Daemon failure #{Time.now} exception=#{e.inspect}\n#{e.backtrace.join($/)}" }
raise
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.