Different behavior of arcpy.server.StageService() depending on Runtime Environment

04-27-2022 06:42 AM
New Contributor


I have a script, that reads a .csv, does some calculations and outputs some tables and some feature classes. Then I want to share these results with arcgis online. If I run the script as Jupyter Notebook, or as a script in a toolbox inside ArcGIS Pro, everything works like a charm. However I want it to run scheduled, so I run via D:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\pythonw.exe  path\myscript.py. Here however the .sd files created by arcpy.server.StageService()  behave differently, and I dont get all the tables and feature layers. The .sddrafts look almost the same, only differences seem to be some IDs. There is also a noteable size difference between the two .sd files.

The code I use for this part. Somebody has an idea what I could do differently?

Tested it with ArcGIS Pro 2.9 and 2.92

        pw = parser.get('cred','pw')
        logging.debug("sd-Draft erstellen")
        send_message("Log","Progess","sd-Draft erstellen")
        arcpy.SignInToPortal('https://www.arcgis.com', 'user', pw)
        # Set output file names
        outdir = workspace
        service = "MobilfunkstandorteTEST4"
        sddraft_filename = service + ".sddraft"
        sddraft_output_filename = os.path.join(outdir, sddraft_filename)

        # Reference map to publish
        #smRes = aprx.listMaps("ResultMap")[0]

            assert len(mRes.listLayers("Antennen")) == 1
            assert len(mRes.listTables("BundeslandOut")) == 1
            assert len(mRes.listTables("GemeindeOut")) == 1
            assert len(mRes.listTables("LandkreisOut")) == 1
            assert len(mRes.listTables("Filter")) == 1
        except AssertionError:
            logging.error("Assertion Error Resultmap")
            arcpy.AddError("Assertion Error Resultmap")
            sendMail("Assertion Error Resultmap")

        antennen_layer = mRes.listLayers("Antennen")[0]
        landkreise_layer = mRes.listLayers("Landkreise")[0]
        gemeinde_layer = mRes.listLayers("Gemeinde")[0]
        bundeslaender_layer = mRes.listLayers("Bundeslaender")[0]
        filter_table = mRes.listTables("Filter")[0]
        bundesland_table = mRes.listTables("BundeslandOut")[0]
        gemeinde_table = mRes.listTables("GemeindeOut")[0]
        landkreis_table = mRes.listTables("LandkreisOut")[0]

        elements_to_upload = [antennen_layer, landkreise_layer, gemeinde_layer, bundeslaender_layer,bundesland_table, gemeinde_table, landkreis_table, filter_table]

        # Create FeatureSharingDraft and set service properties
        sharing_draft = mRes.getWebLayerSharingDraft("HOSTING_SERVER", "FEATURE", service, elements_to_upload)
        sharing_draft.summary = "My SummaryTEST"
        sharing_draft.tags = "My TagsTEST"
        sharing_draft.description = "My Description"
        sharing_draft.credits = "My Credits"
        sharing_draft.useLimitations = "My Use Limitations"
        sharing_draft.overwriteExistingService = True

        # Create Service Definition Draft file

        logging.debug("Creating Stage Service...")
        send_message("Log","Progess","Erzeuge Stage Service")
        # Stage Service
        sd_filename = service + ".sd"
        sd_output_filename = os.path.join(outdir, sd_filename)
        # arcpy.server.StageService(sddraft_output_filename, sd_output_filename)
            retval = arcpy.server.StageService(sddraft_output_filename, sd_output_filename, 102)
            warnings = arcpy.GetMessages(1)

        except Exception as stage_exception:
            print("Sddraft not staged. Analyzer errors encountered - {}".format(str(stage_exception)))

    except Exception:
        e = sys.exc_info()[1]
        logging.error("sd-Draft erstellen:" + str(e))
        arcpy.AddError("sd-Draft erstellen:"+ str(e))

        logging.debug("Feature Service aktualisieren")
        send_message("Log","Progess","Feature Service aktualisieren")
        sd_fs_name = "HOSTED_FEATURE_SERVICE_NAME"
        portal = "http://www.arcgis.com"
        user = "user"
        gis = GIS(portal, user, pw)

        shrOrg = True
        shrEveryone = False
        shrGroups = "Mobilfunkstandorte_Version2"

        logging.debug("Search for original SD on portal…")
        send_message("Log", "Progess","Suche original SD auf Portal")
        sdItem = gis.content.search("MobilfunkstandorteTEST4".format(sd_fs_name, user), item_type="Service Definition")[0]
        logging.debug("Found SD: {}, ID: {}  Uploading and overwriting…".format(sdItem.title, sdItem.id))
        send_message("Log","Progess","Found SD: {}, ID: {}  Uploading and overwriting…".format(sdItem.title, sdItem.id))
        logging.debug("Overwriting existing feature service…")
        send_message("Log","Progess","Ueberschreibe existierenden Feature Service")
        fs = sdItem.publish(overwrite=True)
        if shrOrg or shrEveryone or shrGroups:
            logging.debug("Setting sharing options…")
            send_message("Log","Progess","Setze Teilungsoptionen")
            fs.share(org=shrOrg, everyone=shrEveryone, groups=shrGroups)
            logging.debug("Finished updating: {} – ID: {}".format(fs.title, fs.id))
            send_message("Log","Progess","Finished updating: {} – ID: {}".format(fs.title, fs.id))
0 Kudos
0 Replies