I have several Python scripts which are set on a task scheduler to run around 7 or 8 times a day between the hours of 1:30PM-5:30PM every half hour. The output from the script is an email with the attachment of search cursor values. I am using error handling to send emails when it fails, and most of the fails that I receive have to do with the following errors, which are not consistent throughout the day.
These are several of the errors from a few of the scripts. The errors are not raised when I run the script manually from PyCharm and they are not raised consistently when executed from task scheduler. Below is an example of a skeleton of my script; each script resembles this but with a few changes parameters.
I am using ArcGIS Desktop and Server 10.2.1 and ArcPy 2.7
import smtplib, osfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEBase import MIMEBasefrom email.MIMEText import MIMETextfrom email.Utils import COMMASPACE, formatdatefrom email import Encodersimport loggingimport logging.handlersclass TlsSMTPHandler(logging.handlers.SMTPHandler): def emit(self, record): """ Emit a record. Format the record and send it to the specified addressees. """ try: import smtplib import string # for tls add this line try: from email.utils import formatdate except ImportError: formatdate = self.date_time port = self.mailport if not port: port = smtplib.SMTP_PORT smtp = smtplib.SMTP(self.mailhost, port) msg = self.format(record) msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % ( self.fromaddr, string.join(self.toaddrs, ","), self.getSubject(record), formatdate(), msg) if self.username: smtp.ehlo() # for tls add this line smtp.starttls() # for tls add this line smtp.ehlo() # for tls add this line smtp.login(self.username, self.password) smtp.sendmail(self.fromaddr, self.toaddrs, msg) smtp.quit() except (KeyboardInterrupt, SystemExit): raise except: self.handleError(record)logger = logging.getLogger()gm = TlsSMTPHandler(("smtp.gmail.com", 587), 'email', ['email'], ' Error found!', ('', ''))gm.setLevel(logging.ERROR)logger.addHandler(gm)try: def send_mail(send_from, send_to, subject, text, files=[], server="localhost"): assert type(send_to)==list assert type(files)==list msg = MIMEMultipart() msg['From'] = send_from msg['To'] = COMMASPACE.join(send_to) msg['Date'] = formatdate(localtime=True) msg['Subject'] = subject msg.attach( MIMEText(text) ) for f in files: part = MIMEBase('application', "octet-stream") part.set_payload( open(f,"rb").read() ) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f)) msg.attach(part) #Set Email smtp parameters smtp = smtplib.SMTP('smtp.gmail.com:587') smtp.starttls() smtp.login('', '') smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close() #Send Field to Text File import arcpy #Define Local Parameters whereclause = "PlannedDate = CONVERT(DATE, GETDATE())" SDEFeatureClass = "C:\Users\Administrator\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\Connection to localhost_SCData_sa.sde\SCData.DBO.SO_SC" LocalFGDB = "C:\PeterText.gdb" PeterTable = "C:\PeterText.gdb\WLATable" Outable = "WLATable" PeterLayer = "PeterLayer" expression = 'Trim([NUMBERCYLA])& "," & Trim([ShortCode])& "," & Trim([RESOLUTION_CODE]) & ",,,SC Truck,"& Trim([last_edited_user])& ",Driver,"& [DATE] &","' #Selection Query Harborselection = "CYLA_DISTRICT = 'WLA' AND PlannedDate = CONVERT(DATE, GETDATE()) AND RESOLUTION_CODE '0' AND RESOLUTION_CODE IS NOT NULL AND RESOLUTION_CODE '-1'" if arcpy.Exists(PeterTable): arcpy.Delete_management(PeterTable) arcpy.TableToTable_conversion(SDEFeatureClass, LocalFGDB, Outable, Harborselection) arcpy.AddField_management(PeterTable, "ShortCode", "TEXT") codeblock = """def findTwoLetter(sccatdesc): output = None if sccatdesc == "MAT": output = "SB" elif sccatdesc == "SBE": output = "SE" elif sccatdesc == "MBE": output = "ME" elif sccatdesc == "MBI": output = "MB" elif sccatdesc == "MW": output = "MW" elif sccatdesc == "MBW": output = "MW" elif sccatdesc == "SBI": output = "SB" elif sccatdesc == "SBW": output = "SW" elif sccatdesc == "SMB": output = "SM" elif sccatdesc == "SOT": output = "SO" return output""" ShortCodeExpression = "findTwoLetter(!SCCatDesc!)" arcpy.CalculateField_management(PeterTable, "ShortCode", ShortCodeExpression, "PYTHON_9.3", codeblock) arcpy.AddField_management(PeterTable, "DATE", "DATE") dateExpression = "Date" arcpy.CalculateField_management(PeterTable, "DATE", dateExpression) #Calculates Field with expression for Peter Text File arcpy.CalculateField_management(PeterTable, "PtrText", expression) #Search Cursor to extract Peter Text Field myOutputFile = open("C:\PeterScripts\WLA\Peter.txt", 'w') rows = arcpy.da.SearchCursor(PeterTable, ["PtrText"]) rowssent = str(arcpy.GetCount_management(PeterTable).getOutput(0)) for row in rows: myOutputFile.write(str(row[0]) + '\n') del row, rows myOutputFile.close() import time date= time.strftime("%m/%d/%Y") print date ATTACHMENTS = ["C:\PeterScripts\WLA\Peter.txt"] send_from='geoffreywestgis@gmail.com' send_to=[''] subject ='WLA Peter.Txt' + ' ' + date text = 'Attached'+ ' ' + rowssent + '' + '' +' rows to be processed' send_mail(send_from, send_to, subject, text, files=ATTACHMENTS)except: logger.exception("Something has gone wrong!")ExecuteError: ERROR 999999: Error executing function.Failed to execute (TableToTable).
ExecuteError: ERROR 000210: Cannot create output C:\PeterText.gdb\NCTABLETEST.dbfFailed to execute (TableToTable).
ExecuteError: Failed to execute. Parameters are not valid.ERROR 000732: Input Table: Dataset C:\PeterText.gdb\EVTable does not exist or is not supportedFailed to execute (AddField).
Traceback (most recent call last): File "C:/PeterScripts/CleanStreets/WVPeter.py", line 93, in import arcpy File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\__init__.py", line 21, in from arcpy.geoprocessing import gp File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\__init__.py", line 14, in from _base import * File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 592, in env = GPEnvironments(gp) File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 589, in GPEnvironments return GPEnvironment(geoprocessor) File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 545, in __init__ self._refresh() File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 547, in _refresh envset = (set(env for env in self._gp.listEnvironments()))RuntimeError: NotInitialized
أكثر...
These are several of the errors from a few of the scripts. The errors are not raised when I run the script manually from PyCharm and they are not raised consistently when executed from task scheduler. Below is an example of a skeleton of my script; each script resembles this but with a few changes parameters.
I am using ArcGIS Desktop and Server 10.2.1 and ArcPy 2.7
import smtplib, osfrom email.MIMEMultipart import MIMEMultipartfrom email.MIMEBase import MIMEBasefrom email.MIMEText import MIMETextfrom email.Utils import COMMASPACE, formatdatefrom email import Encodersimport loggingimport logging.handlersclass TlsSMTPHandler(logging.handlers.SMTPHandler): def emit(self, record): """ Emit a record. Format the record and send it to the specified addressees. """ try: import smtplib import string # for tls add this line try: from email.utils import formatdate except ImportError: formatdate = self.date_time port = self.mailport if not port: port = smtplib.SMTP_PORT smtp = smtplib.SMTP(self.mailhost, port) msg = self.format(record) msg = "From: %s\r\nTo: %s\r\nSubject: %s\r\nDate: %s\r\n\r\n%s" % ( self.fromaddr, string.join(self.toaddrs, ","), self.getSubject(record), formatdate(), msg) if self.username: smtp.ehlo() # for tls add this line smtp.starttls() # for tls add this line smtp.ehlo() # for tls add this line smtp.login(self.username, self.password) smtp.sendmail(self.fromaddr, self.toaddrs, msg) smtp.quit() except (KeyboardInterrupt, SystemExit): raise except: self.handleError(record)logger = logging.getLogger()gm = TlsSMTPHandler(("smtp.gmail.com", 587), 'email', ['email'], ' Error found!', ('', ''))gm.setLevel(logging.ERROR)logger.addHandler(gm)try: def send_mail(send_from, send_to, subject, text, files=[], server="localhost"): assert type(send_to)==list assert type(files)==list msg = MIMEMultipart() msg['From'] = send_from msg['To'] = COMMASPACE.join(send_to) msg['Date'] = formatdate(localtime=True) msg['Subject'] = subject msg.attach( MIMEText(text) ) for f in files: part = MIMEBase('application', "octet-stream") part.set_payload( open(f,"rb").read() ) Encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f)) msg.attach(part) #Set Email smtp parameters smtp = smtplib.SMTP('smtp.gmail.com:587') smtp.starttls() smtp.login('', '') smtp.sendmail(send_from, send_to, msg.as_string()) smtp.close() #Send Field to Text File import arcpy #Define Local Parameters whereclause = "PlannedDate = CONVERT(DATE, GETDATE())" SDEFeatureClass = "C:\Users\Administrator\AppData\Roaming\ESRI\Desktop10.2\ArcCatalog\Connection to localhost_SCData_sa.sde\SCData.DBO.SO_SC" LocalFGDB = "C:\PeterText.gdb" PeterTable = "C:\PeterText.gdb\WLATable" Outable = "WLATable" PeterLayer = "PeterLayer" expression = 'Trim([NUMBERCYLA])& "," & Trim([ShortCode])& "," & Trim([RESOLUTION_CODE]) & ",,,SC Truck,"& Trim([last_edited_user])& ",Driver,"& [DATE] &","' #Selection Query Harborselection = "CYLA_DISTRICT = 'WLA' AND PlannedDate = CONVERT(DATE, GETDATE()) AND RESOLUTION_CODE '0' AND RESOLUTION_CODE IS NOT NULL AND RESOLUTION_CODE '-1'" if arcpy.Exists(PeterTable): arcpy.Delete_management(PeterTable) arcpy.TableToTable_conversion(SDEFeatureClass, LocalFGDB, Outable, Harborselection) arcpy.AddField_management(PeterTable, "ShortCode", "TEXT") codeblock = """def findTwoLetter(sccatdesc): output = None if sccatdesc == "MAT": output = "SB" elif sccatdesc == "SBE": output = "SE" elif sccatdesc == "MBE": output = "ME" elif sccatdesc == "MBI": output = "MB" elif sccatdesc == "MW": output = "MW" elif sccatdesc == "MBW": output = "MW" elif sccatdesc == "SBI": output = "SB" elif sccatdesc == "SBW": output = "SW" elif sccatdesc == "SMB": output = "SM" elif sccatdesc == "SOT": output = "SO" return output""" ShortCodeExpression = "findTwoLetter(!SCCatDesc!)" arcpy.CalculateField_management(PeterTable, "ShortCode", ShortCodeExpression, "PYTHON_9.3", codeblock) arcpy.AddField_management(PeterTable, "DATE", "DATE") dateExpression = "Date" arcpy.CalculateField_management(PeterTable, "DATE", dateExpression) #Calculates Field with expression for Peter Text File arcpy.CalculateField_management(PeterTable, "PtrText", expression) #Search Cursor to extract Peter Text Field myOutputFile = open("C:\PeterScripts\WLA\Peter.txt", 'w') rows = arcpy.da.SearchCursor(PeterTable, ["PtrText"]) rowssent = str(arcpy.GetCount_management(PeterTable).getOutput(0)) for row in rows: myOutputFile.write(str(row[0]) + '\n') del row, rows myOutputFile.close() import time date= time.strftime("%m/%d/%Y") print date ATTACHMENTS = ["C:\PeterScripts\WLA\Peter.txt"] send_from='geoffreywestgis@gmail.com' send_to=[''] subject ='WLA Peter.Txt' + ' ' + date text = 'Attached'+ ' ' + rowssent + '' + '' +' rows to be processed' send_mail(send_from, send_to, subject, text, files=ATTACHMENTS)except: logger.exception("Something has gone wrong!")ExecuteError: ERROR 999999: Error executing function.Failed to execute (TableToTable).
ExecuteError: ERROR 000210: Cannot create output C:\PeterText.gdb\NCTABLETEST.dbfFailed to execute (TableToTable).
ExecuteError: Failed to execute. Parameters are not valid.ERROR 000732: Input Table: Dataset C:\PeterText.gdb\EVTable does not exist or is not supportedFailed to execute (AddField).
Traceback (most recent call last): File "C:/PeterScripts/CleanStreets/WVPeter.py", line 93, in import arcpy File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\__init__.py", line 21, in from arcpy.geoprocessing import gp File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\__init__.py", line 14, in from _base import * File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 592, in env = GPEnvironments(gp) File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 589, in GPEnvironments return GPEnvironment(geoprocessor) File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 545, in __init__ self._refresh() File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\geoprocessing\_base.py", line 547, in _refresh envset = (set(env for env in self._gp.listEnvironments()))RuntimeError: NotInitialized
أكثر...