How can i stop scheduler in windows service?

  apscheduler, python, scheduler, service, windows

I’m working on a project to run Python APScheduler as a Windows Service with the results going to a text file. I can install and start the service without a hitch.

I’ve tried a ‘blocking’ and ‘background’ schedulers and they behave the same.

I’ve played with moving the scheduler.shutdown() into different places. I would like to place it in the service stop function and have the scheduler run until the service receives a stop command whereupon the service stop function would handle shutting down the scheduler.

Perhaps you can point me in the right direction? Here is the code sanitized to ensure you will not have to reboot your computer.

import socket
import win32serviceutil
import servicemanager
import win32event
import win32service
from apscheduler.schedulers.blocking import BlockingScheduler
from ApiLoop import ApiPostLoop
from communicate import Connect_device

class SMWinservice(win32serviceutil.ServiceFramework):
_svc_name_ = "EnvisoftFS"
_svc_display_name_ = "EnvisoftFS"
_svc_description_ = "envisoft.com.tr | 2021 All rights reserved."

@classmethod
def parse_command_line(cls):
    win32serviceutil.HandleCommandLine(cls)

def __init__(self, args):

    win32serviceutil.ServiceFramework.__init__(self, args)
    self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
    socket.setdefaulttimeout(60)
    self.Api = ApiPostLoop()
    self.scheduler = BlockingScheduler()

def SvcStop(self):
    self.stop()
    self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
    win32event.SetEvent(self.hWaitStop)

def SvcDoRun(self):
    self.start()
    self.Api.SendDiagnostic("İstasyon Çevrimiçi")
    self.Api.SendDiagnosticWithType("1") #İstasyon Çevrimiçi
    try:
        self.Api.GetChannelInformationByStationId()
    except:pass

    servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
                          servicemanager.PYS_SERVICE_STARTED,
                          (self._svc_name_, ''))

    self.scheduler.add_job(func=self.ConnectDevices, trigger='cron', second='30,59')
    self.scheduler.add_job(func=self.ChangeSystemTime, trigger='cron', hour='*/3',minute='1',second='20')
    self.scheduler.add_job(func=self.ChangeSaisInformation, trigger='cron', hour='23',minute='59',second='20')
    self.scheduler.add_job(func=self.ChangeChannelInformation, trigger='cron', minute='*/59',second='20')

    self.Api.SendDiagnosticWithType("5")  # Veri Gönderiyor.
    self.main()

def start(self):
    self.isrunning = True

def stop(self):
    self.isrunning = False

def ConnectDevices(self):

    try:
        Connect_device()
    except:pass

def ChangeSystemTime(self):
    #Her 3 saatte bir değişecek !
    try:
        self.Api.GetServerDateTime()
    except:pass

def ChangeSaisInformation(self):
    #Günde 1 defa
    try:
        self.Api.GetStationInformation()
    except:pass

def ChangeChannelInformation(self):
    #Her saat başı
    try:
        self.Api.GetChannelInformationByStationId()
    except:pass

def SendData(self):
    #Her 15 dakikada bir
    try:
        self.Api.SendData()
    except:pass


def main(self):
    self.scheduler.start()


if __name__ == '__main__':
    SMWinservice.parse_command_line()

Source: Windows Questions

LEAVE A COMMENT