It took me awhile to figure out how to run Orbited and Dolores as Windows Services. Unfortunately, while I love Mac, almost every other computer at my workplace is Windows, including the servers. When deploying my web application, I can’t really just leave instructions like: “When you restart the server, please run these two scripts.”
They need to be Windows Services.
Thankfully, it isn’t actually too hard. You just need a couple of Python packages (which I actually don’t remember installing, so they may have come with Twisted or Python), and then all you have to do is write a couple of very simple scripts. Here are mine:
Orbited Service
import OrbitedManager # a copy of orbited.start with some very minor modifications, which I'll include after the break import time # Service Utilities import win32serviceutil import win32service import win32event class WindowsService(win32serviceutil.ServiceFramework): _svc_name_ = "Orbited" _svc_display_name_ = "Orbited Server" def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.hWaitStop) def SvcDoRun(self): import servicemanager OrbitedManager.start() while True: retval = win32event.WaitForSingleObject(self.hWaitStop, 10) if not retval == win32event.WAIT_TIMEOUT: OrbitedManager.stop() break time.sleep(5.0) if __name__=='__main__': win32serviceutil.HandleCommandLine(WindowsService)
Dolores Service
import Dolores import time # Service Utilities import win32serviceutil import win32service import win32event class WindowsService(win32serviceutil.ServiceFramework): _svc_name_ = "Dolores" _svc_display_name_ = "Dolores Server" def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) def SvcStop(self): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.hWaitStop) def SvcDoRun(self): import servicemanager Dolores.start() while True: retval = win32event.WaitForSingleObject(self.hWaitStop, 10) if not retval == win32event.WAIT_TIMEOUT: Dolores.stop() break time.sleep(5.0) if __name__=='__main__': win32serviceutil.HandleCommandLine(WindowsService)
That’s the basics, but as you may have noticed, I also changed Orbited’s start script. I did this because the original did not play nicely with threads.
Instead of changing it in its original location in the filesystem, I created a copy of it in the local directory (as you can see, I’m not importing Orbited.OrbitedManager).