On Thursday, 26 July 2012 21:04:58 UTC+10, Tim Richardson wrote:
>
> I'm confused about this topic. 
> I want to run the scheduler in the background, as a windows service. 
> web2py is running as a windows service.
> From reading this group, it seems that web2py's built in cron is not a 
> reliable way to start the scheduler if web2py is a service. 
>
> I know little about windows services. I see there are a few python recipes 
> for making a script into a service, but I'm starting to feel like I'm 
> reinventing wheels. 
>
>
I did it like this:
'''
Created on 26/07/2012

@author: Tim
'''
'''

 Author: Alex Baker
 Date: 7th July 2008
 Description : Simple python program to generate wrap as a service based on 
example on the web, see link below.
 
 http://essiene.blogspot.com/2005/04/python-windows-services.html

 Usage : python aservice.py install
 Usage : python aservice.py start
 Usage : python aservice.py stop
 Usage : python aservice.py remove
 
 C:\>python aservice.py  --username <username> --password <PASSWORD> 
--startup auto install

'''


import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os
import subprocess

class aservice(win32serviceutil.ServiceFramework):
   
   _svc_name_ = "Web2Py Background Scheduler"
   _svc_display_name_ = "Web2Py scheduler for accpac_web_orders"
   _svc_description_ = "handles long running Web2Py tasks"
         
   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      
      servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, '')) 
      
      self.timeout = 3000

      win32api.WinExec('pythonw.exe d:\web2py\web2py.py -K 
accpac_web_orders')   
      self.timeout = 3000

      while 1:
         # Wait for service stop signal, if I timeout, loop again
         rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
         # Check to see if self.hWaitStop happened
         if rc == win32event.WAIT_OBJECT_0:
            # Stop signal encountered
            servicemanager.LogInfoMsg(_svc_name + " - STOPPED")
            break
         else:
            servicemanager.LogInfoMsg( _svc_name +  + " is alive and well")     
       
      
def ctrlHandler(ctrlType):
   return True
                  
if __name__ == '__main__':   
   win32api.SetConsoleCtrlHandler(ctrlHandler, True)   
   win32serviceutil.HandleCommandLine(aservice)Enter code here...

 

-- 



Reply via email to