Uploading SD file using Python "sometimes" causes errors

3030
2
08-08-2013 04:57 AM
ChristopherTilley
New Contributor
My organization is using a Python toolbox to upload two SD files every two hours. Most of the time we have no issues, but sometimes the upload has a problem. I've added a loop in the script to attemp the upload again and most of the time it will upload the second time. Here are the errors from the Server Logs:
SEVERE -- Failed to rename cache folder.  *This happen every time I upload but does not seem to effect the upload

Two below occur at the same instance (I assume they are the same problem)
SEVERE -- Error executing tool.: ERROR 001360: Failed to get basic item metadata for service definition upload. Failed to execute (Publish Service Definition). *This is critical. When I get this error the upload does not work

SEVERE -- Failed to get basic item metadata for service definition upload.: ERROR: code:404, Could not find resource or operation 'ie2c45393-1528-4e03-a9ba-3a28f6ad754b' on the system., No resource could be found at that address. *Same as 1360

The upload routine of my script:

def ServerUpload(serviceName, newDesc, summary, tags):


    # Current map document       
    try:
        mapdoc = arcpy.mapping.MapDocument("CURRENT")
        mapdocbase = os.path.basename(mapdoc.filePath)
        mxdpath = os.path.dirname(mapdoc.filePath)
    except:
        arcpy.AddMessage('PROGRAM STOPPED ---NO CURRENT MXD IDENTIFIED ---Must run this tool within a (named) MXD')
        sys.exit(0)           

    # Service name HARD CODED
    sddraftFile = mxdpath + "\\" + serviceName + ".sddraft"

    # Create connection file
    con_name = ("SerConTEMP_" + serviceName + ".ags")
    con = mxdpath + "\\" + con_name

    loopNum = 0
    while loopNum < 3:
   
        if os.path.exists(con):
            try:
                os.remove(con)
            except:
                arcpy.AddMessage("PROGRAM STOPPED ---Could not delete server connect file")
                sys.exit(0)

        try:
            use_arcgis_desktop_staging_folder = False   
            arcpy.mapping.CreateGISServerConnectionFile("PUBLISH_GIS_SERVICES",
                                                        mxdpath,
                                                        con_name,
                                                        server_url,
                                                        "ARCGIS_SERVER",
                                                        use_arcgis_desktop_staging_folder,
                                                        mxdpath,
                                                        LoginName,
                                                        PassWord,
                                                        "SAVE_USERNAME")
            sleep(10)
        except:
            arcpy.AddMessage('PROGRAM STOPPED ---COULD NOT CREATE SERVER CONNECT FILE ---Server url, login, or password may be incorrect')
            sys.exit(0)

   
        if os.path.exists(sddraftFile):
            try:
                os.remove(sddraftFile)
            except:
                arcpy.AddMessage('PROGRAM STOPPED ---NOT ABLE TO DELETE SDDRAFTFILE FILE ---File may be locked')
                sys.exit(0)

        try:
            analysis = arcpy.mapping.CreateMapSDDraft(mapdoc, sddraftFile, serviceName, 'ARCGIS_SERVER', con, True, "TIMS", summary, tags)
            #analysis = arcpy.mapping.CreateMapSDDraft(mapdoc, sddraftFile, serviceName, 'ARCGIS_SERVER', con, True, None, summary, tags)

        except:
            arcpy.AddMessage('PROGRAM STOPPED ---NOT ABLE TO CREATE SDDRAFTFILE FILE ---File variables may be incorrect')

        # Read and update the sddraft xml
        xml = sddraftFile
        doc = DOM.parse(xml)

        # Update the description
        try:
            descriptions = doc.getElementsByTagName('Description')
            for desc in descriptions:
                if desc.parentNode.tagName == 'ItemInfo':
                    if desc.hasChildNodes():
                       desc.firstChild.data = newDesc
        except:
            arcpy.AddMessage('NOT ABLE TO UPDATE DESCRIPTION IN SDDRAFT FILE ---File may be locked')

        # Update KML.
        try:
            soe = 'KmlServer'
            typeNames = doc.getElementsByTagName('TypeName')
            for typeName in typeNames:
                # Get the TypeName we want to disable.
                if typeName.firstChild.data == soe:
                    extension = typeName.parentNode
                    for extElement in extension.childNodes:
                        # Disabled SOE.
                        if extElement.tagName == 'Enabled':
                            extElement.firstChild.data = 'false'
        except:
            arcpy.AddMessage('NOT ABLE TO UPDATE KML IN SDDRAFT FILE ---File may be locked')

        try:
            outXml = xml   
            f = open(outXml, 'w')    
            doc.writexml( f )    
            f.close()
        except:
            arcpy.AddMessage('NOT ABLE TO WRITE SDDRAFT XML ---File may be locked')

#********************************************************************************************
        #Analyze the service definition draft
        analysisSddraft = arcpy.mapping.AnalyzeForSD(sddraftFile)

        # Print errors, warnings, and messages returned from the analysis
        arcpy.AddMessage("The following information was returned during analysis of the MXD:")
        for key in ('messages', 'warnings', 'errors'):
            arcpy.AddMessage('----' + key.upper() + '---')
            vars = analysisSddraft[key]
            for ((message, code), layerlist) in vars.iteritems():
                arcpy.AddMessage('    ' + message + ' (CODE %i)' % code)
                arcpy.AddMessage('       applies to: ')
                for layer in layerlist:
                    arcpy.AddMessage(layer.name)
                    arcpy.AddMessage("")


        # Service definition variables
        sdFile = mxdpath + "\\" + serviceName + ".sd"

        if os.path.exists(sdFile):
            try:
                os.remove(sdFile)
            except:
                arcpy.AddMessage('PROGRAM STOPPED ---NOT ABLE TO DELETE ORIGINAL SD FILE ---File may be locked')
                sys.exit(0)         

        try:
            arcpy.StageService_server(sddraftFile, sdFile)
            sleep(5)
        except:
            arcpy.AddMessage('PROGRAM STOPPED ---NOT ABLE TO CREATE SD FILE FROM SDDRAFT FILE ---SDDRAFT file may be corrupt')
            sys.exit(0)

        try:
            arcpy.UploadServiceDefinition_server(sdFile, con)
            loopNum = 3

        except:
            if loopNum < 2:
                loopNum = loopNum + 1
                arcpy.AddMessage("Program failed to upload SD file. Attempting to upload again [" + str(loopNum) + "]")
            else:
                arcpy.AddMessage('PROGRAM STOPPED ---NOT ABLE TO UPLOAD SD FILE TO the server ---Server may be experiencing problems')
                arcpy.AddMessage('PLEASE RE-RUN THE TOOL. Close MXD, re-open MXD, and re-run tool')
                sys.exit(0)

        if os.path.exists(sdFile):
            try:
                os.remove(sdFile)
            except:
                arcpy.AddMessage('NOT ABLE TO DELETE SD FILE ---File may be locked')

        if os.path.exists(con):
            try:
                os.remove(con)
            except:
                arcpy.AddMessage("PROGRAM STOPPED ---Could not delete server connect file")
                sys.exit(0)

    arcpy.AddMessage('UPLOAD TO SERVER SUCCESSFUL at ' + unicode((datetime.datetime.now()).replace(microsecond=0)))
    AddLog('PROGRAM SUCCESSFUL UPDATED, NCDOT SERVICE: ' + serviceName, False)
Tags (2)
0 Kudos
2 Replies
BrettElliot
New Contributor III
Hi Christopher,

You can use "[CODE ][ \CODE]" tags to format your python code in the forums window.


def ServerUpload(serviceName, newDesc, summary, tags):

# Current map document 
try:
mapdoc = arcpy.mapping.MapDocument("CURRENT")
mapdocbase = os.path.basename(mapdoc.filePath)
mxdpath = os.path.dirname(mapdoc.filePath)
except:
arcpy.AddMessage('PROGRAM STOPPED ---NO CURRENT MXD IDENTIFIED ---Must run this tool within a (named) MXD')
sys.exit(0) 


As for scripting with Python, although it is officially supported by ESRI, there are still a ton of bugs.  You could try adding a small time delay before trying to upload a second time as to not overwhelm the server.
0 Kudos
JeffMoulds
Esri Contributor
I second that, i.e. re-post your script with code tags. And what version of ArcGIS Desktop are you using?
0 Kudos