7

I'm working my way through my first attempt at scheduled tasks with modelbuilder and python. So far I have:

  1. Created a model that reconciles our SDE versions and then compresses the database.

  2. Exported the model to a script so I can set it up in Windows Task Scheduler

Its a fairly straight forward model so I expect it to work, however, I'd like to set it up to email me when it is completed and include the geoprocessing messages.

I found this support article that includes a script that does just that.

My question is: How do I include this or reference this in my reconcile and compress model?

Craig
  • 4,570
  • 7
  • 40
  • 71

3 Answers3

3

I figured out that I need to just add the send email code to the python script. I was able to find this help document that pretty much laid out the process for me. I made some minor adjustments and came up with:

import smtplib, time, arcpy, arceditor

#block new connections to the database.
arcpy.AcceptConnections('Database Connections/MyConnection.sde', False)

# wait 5 minutes
time.sleep(300)

# Disconnect all users from the database.
arcpy.DisconnectUser('Database Connections/MyConnection.sde', "ALL")

# Get a list of versions to pass into the ReconcileVersions tool.
versionList = arcpy.ListVersions('Database Connections/MyConnection.sde')

# Execute the ReconcileVersions tool.
arcpy.ReconcileVersions_management('Database Connections/MyConnection.sde', "ALL_VERSIONS", "sde.DEFAULT", versionList, "LOCK_ACQUIRED", "ABORT_CONFLICTS", "BY_OBJECT", "FAVOR_TARGET_VERSION", "POST", "KEEP_VERSION", "c:/path/to/log.txt")

# Run the compress tool. 
arcpy.Compress_management('Database Connections/MyConnection.sde')

#Allow the database to begin accepting connections again
arcpy.AcceptConnections('Database Connections/MyConnection.sde', True)

# Get Messages
ScriptMessages = arcpy.GetMessages()

# Send Email when script is complete
SERVER = "mailserver.yourcompany.com"
FROM = "GIS Admin <mail@yourcompany.com>"
TO = "johndoe@yourcompany.com"
SUBJECT = "The Script Has Completed"
MSG = "This is an auto generated Message.\n\rThe Reconcile & Compress script has completed.\n\n"  + ScriptMessages

# Prepare actual message
MESSAGE = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, ", ".join(TO), SUBJECT, MSG)

# Send the mail
server = smtplib.SMTP(SERVER)
server.sendmail(FROM, TO, MESSAGE)
server.quit()
Craig
  • 4,570
  • 7
  • 40
  • 71
0

Try integrating the following script as a scripttool into your ModelBuilder workflow. You do not have to schedule any events, simply plug it into any process and it will send you a message. Keep in mind this is directed at gmail.

import smtplib, os
from email.MIMEMultipart import MIMEMultipart
from email.MIMEBase import MIMEBase
from email.MIMEText import MIMEText
from email import Encoders

# This will log you into your gmail account--this is where the mail will be sent from.
gmail_user = sys.argv[1] # String e.g. mypassword
gmail_pwd = sys.argv[2] # String e.g. Password

# The parameters
to = sys.argv[3] # String  e.g. recipient@gmail.com
subject = sys.argv[4] # String e.g. "This is a test"
text = sys.argv[5] # String e.g. "Subject test"

def mail(to, subject, text):
    msg = MIMEMultipart()

    msg['From'] = gmail_user
    msg['To'] = to
    msg['Subject'] = subject

    msg.attach(MIMEText(text))   
    mailServer =smtplib.SMTP("smtp.gmail.com", 587)
    mailServer.ehlo()
    mailServer.starttls()
    mailServer.ehlo()
    mailServer.login(gmail_user, gmail_pwd)
    mailServer.sendmail(gmail_user, to, msg.as_string())
    mailServer.close()

mail(to, subject, text)
Aaron
  • 51,658
  • 28
  • 154
  • 317
0

I sometimes use smtplib, but I use boto frequently (which you have to download/install from here (https://code.google.com/p/boto/)) and an Amazon SNS topic.

When adding a subscriber, you can specify email or SMS (and some other protocols that I haven't used):

enter image description here

import os,boto,traceback

aws_access_key_id = ''
aws_secret_access_key = ''
topicarn=''

def SendNotificationMessage(subject,message):
    try:
        #print 'subject,message',subject,message

        sns = boto.connect_sns(aws_access_key_id,aws_secret_access_key)

        res = sns.publish(topicarn, message, subject)

        #print res
    except:       
        traceback.print_exc()
Jay Cummins
  • 14,642
  • 7
  • 66
  • 141