Thin as windows service and stdio

64 views
Skip to first unread message

Ericgj

unread,
Apr 20, 2015, 11:48:29 AM4/20/15
to thin...@googlegroups.com
I followed James' instructions in this very old (2009) thread, using win32-service and rubyw (v2.2.1) + rubyw_helper:

It works, except I get no output to the redirected out and err files from within the rack app (Rack::CommonLogger, etc). 
  • The files themselves are created. If there is an uncaught error booting the app, they are written to from rubyw_helper. So it's not a file permissions issue. 
  • I confirmed that running the app in a console (not as a service) does produce output.

Any suggestions?  Is there another way of doing this now?  

BTW, I think that thread is the only decent instructions for doing this on the entire internet. It has 1400+ delicious.com bookmarks to it, and it didn't even work for the OP!

Thanks,
Eric

Ericgj

unread,
Apr 20, 2015, 6:10:39 PM4/20/15
to thin...@googlegroups.com
Setting sync = true on the redirected out and err worked for me.  

Here's my code for posterity's sake:

  require 'win32/daemon'
  require 'rubyw_helper'
  include Win32

  SERVICE_ENV  = ARGV.shift || 'development'
  SERVICE_HOST = ARGV.shift || 'localhost'
  SERVICE_PORT = ARGV.shift || 8080
  
  class ThinServiceDaemon < Daemon

    def service_main
      rackup = File.expand_path("../server/service.ru", File.dirname(__FILE__))
      stdout = File.expand_path("../log/server-#{SERVICE_ENV}-out.log", File.dirname(__FILE__))
      stderr = File.expand_path("../log/server-#{SERVICE_ENV}-err.log", File.dirname(__FILE__))
      stdin  = (/mswin|mingw/ =~ RUBY_PLATFORM) ? 'NUL:' : '/dev/null'
   
      RubywHelper.new(stdout, stderr, stdin).with_redirection do
        
        $stderr.sync = true; $stdout.sync = true

        require 'thin'
        Thin::Runner.new(["start", 
                          "--address", SERVICE_HOST, 
                          "--port", SERVICE_PORT,
                          "--environment", SERVICE_ENV,
                          "--rackup", rackup
                         ]).run!
      end
    end

    def service_stop!
      exit!
    end

  end

  ThinServiceDaemon.mainloop
Reply all
Reply to author
Forward
0 new messages