Files
Python_Examples/stm_etl/FTP/scheduler.py
T

259 lines
7.4 KiB
Python

'''
Created on May 25th, 2016
@author: Wes
'''
import schedule, time, threading, functools, glob, ctypes, sys
sys.dont_write_bytecode = True
from download_transmissions import maharamOrderDownload, maharamInvoiceUpload, maharamASNUpload
from download_transmissions import momentumOrderDownload, momentumInvoiceUpload, momentumASNUpload
from download_transmissions import kravetOrderDownload
from download_transmissions import glenravenASNDownload
from download_transmissions import UNIFI_846_Download
from kf_850 import processKravetPo
from mg_850 import processMomentumPo
from mg_810 import createMomentumInvoiceDocument
from mg_856 import createMomentumASN
from mf_850 import processMaharamPo
from mf_810 import createMaharamInvoiceDocuments
from mf_856 import createMaharamShipNotices
from gr_856_in import processInboundGRASN
from dt_cut import dtxInvoiceAlert
from FGI_report import run_FGI_report
#Function decorator to catch exceptions instead of halting scheduler job.
def catch_exceptions(job_func):
@functools.wraps(job_func)
def wrapper(*args, **kwargs):
try:
job_func(*args, **kwargs)
except:
import traceback
print(traceback.format_exc())
update_title()
return wrapper
@catch_exceptions
def mf_850():
print ('Submitting job: maharamOrderDownload()')
maharamOrderDownload()
print ''
if len(glob.glob('D:\\FTP\\MAH_850_*.txt')) > 0:
print 'Processing Maharam POs...'
for name in reversed(glob.glob('D:\\FTP\\MAH_850_*.txt')):
processMaharamPo(name)
print ''
update_title()
@catch_exceptions
def mf_810():
print 'Submitting job: createMaharamInvoiceDocuments()'
createMaharamInvoiceDocuments()
print ''
if len(glob.glob('D:\\FTP\\SEND_MF\\STM_INVOICE_*.xml')) > 0:
print 'Uploading invoice documents to Maharam FTP server...'
maharamInvoiceUpload()
print ''
update_title()
@catch_exceptions
def mf_856():
print 'Submitting job: createMaharamShipNotices()'
createMaharamShipNotices()
print ''
if len(glob.glob('D:\\FTP\\SEND_MF\\STM_856_*.txt')) > 0:
print 'Uploading shipment notices to Maharam FTP server...'
maharamASNUpload()
print ''
update_title()
@catch_exceptions
def mg_850():
print ('Submitting job: momentumOrderDownload()')
momentumOrderDownload()
print ''
if len(glob.glob('D:\\FTP\\SBR_850_*.txt')) > 0:
print 'Processing Momentum POs...'
for name in reversed(glob.glob('D:\\FTP\\SBR_850_*.txt')):
processMomentumPo(name)
print ''
update_title()
@catch_exceptions
def mg_810():
print 'Submitting job: createMomentumInvoiceDocument()'
createMomentumInvoiceDocument()
print ''
if len(glob.glob('D:\\FTP\\SEND_MG\\SBR_810_*.txt')) > 0:
print 'Uploading invoice documents to Momentum FTP server...'
momentumInvoiceUpload()
print ''
update_title()
@catch_exceptions
def mg_856():
print 'Submitting job: createMomentumASN()'
createMomentumASN()
print ''
if len(glob.glob('D:\\FTP\\SEND_MG\\SBR_856_*.txt')) > 0:
print 'Uploading shipment notices to Momentum FTP server...'
momentumASNUpload()
print ''
update_title()
@catch_exceptions
def kf_850():
print ('Submitting job: karvetOrderDownload()')
kravetOrderDownload()
print ''
if len(glob.glob('D:\\FTP\\Kravet_to_vendor*.csv')) > 0:
print 'Processing Kravet POs...'
for name in reversed(glob.glob('D:\\FTP\\Kravet_to_vendor*.csv')):
processKravetPo(name)
print ''
update_title()
@catch_exceptions
def gr_856_in():
print ('Submitting job: glenravenASNDownload()')
glenravenASNDownload()
print ''
#Add processing bit here later
if len(glob.glob('D:\\FTP\\INCOMING\\GR_ASN_*.csv')) > 0:
print 'Processing Glen Raven Ship Notices...'
for name in reversed(glob.glob('D:\\FTP\\INCOMING\\GR_ASN_*.csv')):
print name
#processInboundGRASN(name)
update_title()
@catch_exceptions
def un_846_in():
print ('Submitting job: UNIFI_846_Download()')
UNIFI_846_Download()
print ''
#Add processing bit here later
update_title()
@catch_exceptions
def dtx_cut_alert():
print ('Submitting job: dtxInvoiceAlert()')
dtxInvoiceAlert()
print ''
update_title()
@catch_exceptions
def FGI_report():
print ('Submitting job: run_FGI_report()')
run_FGI_report()
print ''
update_title()
'''
Hacked this together to so I can add the name of the next job and when it will run to the
console title.
'''
def update_title():
message = 'Scheduler.py - Next Job: ' + min(schedule.jobs).job_func.args.__repr__().split(' ')[1] + ' @ ' + str(schedule.next_run())
ctypes.windll.kernel32.SetConsoleTitleA(message)
#Use to run a job in it's own thread
def run_threaded(job_func):
job_thread = threading.Thread(target=job_func)
job_thread.start()
'''
An attempt to clean up the schedule code below.
Schedules 'func' to run at 'time' Monday-Friday
'''
def weekdays(time, func):
schedule.every().monday.at(time).do(run_threaded, func)
schedule.every().tuesday.at(time).do(run_threaded, func)
schedule.every().wednesday.at(time).do(run_threaded, func)
schedule.every().thursday.at(time).do(run_threaded, func)
schedule.every().friday.at(time).do(run_threaded, func)
if __name__ == '__main__':
#Maharam PO Download
mf_850_runs = ['08:25','09:55','11:25','12:55','14:25','15:55','17:25']
for t in mf_850_runs:
weekdays(t, mf_850)
#Maharam Invoice Upload. This transmission was sent at 16:40 on the AS400
mf_810_runs = ['16:40']
for t in mf_810_runs:
weekdays(t, mf_810)
#Maharam ASN Upload
mf_856_runs = ['08:45','12:45','13:45','14:45','15:45','16:44','17:45']
for t in mf_856_runs:
weekdays(t, mf_856)
#Momentum PO Download
mg_850_runs = ['12:25','19:25']
for t in mg_850_runs:
weekdays(t, mg_850)
#Momentum Invoice Upload Begin. This transmission was sent at 16:45 on the AS400
mg_810_runs = ['16:45']
for t in mg_810_runs:
weekdays(t, mg_810)
#Momentum ASN Upload. This transmission was sent at 12:15 on the AS400
mg_856_runs = ['12:15','17:30']
for t in mg_856_runs:
weekdays(t, mg_856)
#Kravet PO Download
kf_850_runs = ['07:25']
for t in kf_850_runs:
weekdays(t, kf_850)
#Glen Raven 856 Download
gr_856_in_runs = ['08:00', '13:00']
for t in gr_856_in_runs:
weekdays(t, gr_856_in)
#UNIFI 846 Download
un_846_in_runs = ['08:01', '13:01']
for t in un_846_in_runs:
weekdays(t, un_846_in)
'''
** DISABLED 2017-10-02 **
We have ended the consignment program
#Designtex Cut into alert
dt_cut_runs = ['08:02', '10:00']
for t in dt_cut_runs:
weekdays(t, dtx_cut_alert)
'''
#FGI Report
schedule.every().saturday.at('23:59').do(run_threaded, FGI_report)
#schedule.every(10).seconds.do(run_threaded, derp) #threaded example
#Non-threaded examples
#schedule.every(10).minutes.do(job)
#schedule.every().hour.do(job)
#schedule.every().day.at("10:30").do(job)
#schedule.every().monday.do(job)
#schedule.every().wednesday.at("13:15").do(job)
print 'Job started. Do not close this window!'
print 'Ctrl-C to exit.'
update_title()
while True:
schedule.run_pending()
time.sleep(1)