# NotifySuccess.py # Safe Software Inc. # Last modified: 2013-Dec-09 # Purpose (much like the FMEServerNotifier but happens at end): # Notify an FME Server Topic if workspace runs successfully (subscribers to this topic will then fire) # Notify a different FME Server Topic if workspace fails (subscribers to this topic will then fire) # Requirements # * Access to FME Server 2012 or later and host name defined as published parameter called FMEServerHost # * Security Token for the REST service of FME Server # * FME Server Host must have topics defined for success and failure of this workspace (any push notifications must subscribe to the appropriate topic) # * Topics for success and failure must be defined as published parameters in the workspace (FailTopic and SuccessTopic) import urllib2 import urllib from urllib2 import Request, urlopen, URLError, HTTPError import base64 # Did FME succeed or not? status = FME_Status #Workspace Parameters Define the Success and Failure Topics FailTopic = FME_MacroValues['FailTopic'] SuccessTopic = FME_MacroValues['SuccessTopic'] #Workspace Parameters set the Host for REST service and Token svcHost = FME_MacroValues['RESTHost'] svcToken = FME_MacroValues['RESTToken'] svcUser = "" svcPass = "" logfilename = FME_LogFileName workspacename = FME_MacroValues['FME_MF_NAME'] def NotifySuccess(): #If workspace failed if status == 0: svcTopic = FailTopic message = 'Workspace Failed' errorMsg = 'FME_FailureMessage' errorMsg = FME_FailureMessage #If workspace successful else: svcTopic = SuccessTopic message = 'Workspace was Successful' errorMsg = 'None' # Set up URL and content for post to FME Server Notification Service via REST svcType = 'publish.json' svcUrl = svcHost + '/fmerest/notifier/topics/' + svcTopic + '/' + svcType #If user gave us a security token add that to the URL if len(svcToken) != 0: svcUrl = svcUrl + '?token=' + svcToken params = { 'subscriber_content': errorMsg + '\n\n' + message, 'email_attachment': logfilename, 'email_subject': workspacename } params = urllib.urlencode(params) #POST Request req = urllib2.Request(svcUrl, params) #If user gave us a user name and password use it to authenticate if len(svcUser) != 0 and len(svcPass) != 0: base64string = base64.encodestring('%s:%s' % (svcUser, svcPass))[:-1] req.add_header("Authorization", "Basic %s" % base64string) #Error Handling on POST try: svcHandle = urllib2.urlopen(req) except HTTPError, e: svcResult = 'Failed to Notify FME Server. Check that your user name and password are valid, FME Server is running, and the success and failure topics you entered exist on FME Server' svcError = 'Error:' + str(e.code) except URLError, e: svcResult = 'Failed to Notify FME Server. Check that your user name and password are valid, FME Server is running, and the success and failure topics you entered exist on FME Server' svcError = 'Error:' + e.reason else: svcResult = svcHandle.read() svcError = '' #Print Details of Post and Response from Server print '' print '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-' print '' print svcUrl if svcError == '': print 'Notified FME Server ' print message print 'FME Error Message: ' + errorMsg print '' print 'Notified FME Topic: ' + svcTopic print '' print 'Content (url encoded): ' print params print '' print 'Response from FME Server: ' print svcResult print svcError print '' print '=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-' print '' NotifySuccess()