# Name: ManageMapServerCacheTiles.py
# Description: The following stand-alone script demonstrates how to Recreate all
# cache tiles for for a map or image service using an area of interest.
# This tool works for weblayers published to ArcGIS Enterprise and ArcGIS Online.
# and for map and image services on a stand alone ArcGIS Server
# Example: This sample script updates map cache tiles.
import arcpy
from arcpy import env
import os, sys, time, datetime, traceback, string
import traceback, sys
# Set environment settings
env.workspace = "C:/data"
# Sign in to ArcGIS Enterprise
myPortal= "https://MyPortal.domain.com/portalwebadaptor"
arcpy.SignInToPortal(myPortal, "siteadminValue", "siteadminPassword")
myServer="https://Myserver.domain.com/serverwebadaptor"
serviceName= "SampleWorldCities"
serviceType= "MapServer"
myPortalServiceURL = (myServer + "/" + "rest/services" +"/" + serviceName + "/"
+ serviceType)
### Sign in to ArcGIS Online
##myPortal= "https://www.arcgis.com"
##arcpy.SignInToPortal(myPortal, "MyUserName", "MyPassword")
##serviceRestUrl="https://www.arcgis.com/tiles/orgid"
##serviceName= "MyServiceName"
##serviceType= "MapServer"
##myPortalServiceURL = (serviceRestUrl + "/" + "arcgis/rest/services" +"/" + serviceName + "/"
## + serviceType)
# Stand alone ArcGIS Server
##target_server_connection = r"C:\share\python3\arcgis on MyServer.ags"
##serviceName= "MyServiceName"
##serviceType= ".MapServer"
##myPortalServiceURL = (target_server_connection + "\\" + serviceName + serviceType)
print (myPortalServiceURL)
#variables for reporting
currentTime = datetime.datetime.now()
arg1 = currentTime.strftime("%H-%M")
arg2 = currentTime.strftime("%Y-%m-%d %H:%M")
file = r'C:\test\report_%s.txt'% arg1
# List of input variables for map or image service
##scales = [591657527.591555,295828763.79577702,147914381.89788899]
scales = [73957190.948944,36978595.474472]
numOfCachingServiceInstances = 8
updateMode = "RECREATE_ALL_TILES"
areaOfInterest = ""
waitForJobCompletion = "WAIT"
updateExtents = ""
portalURL =""
# Print results of the script to a report
report = open(file,'w')
try:
result = arcpy.server.ManageMapServerCacheTiles(myPortalServiceURL, scales, updateMode,
numOfCachingServiceInstances, areaOfInterest,
updateExtents, waitForJobCompletion,
portalURL)
while result.status < 4:
time.sleep(0.2)
resultValue = result.getMessages()
report.write ("completed " + str(resultValue))
print ("Created cache tiles for given schema successfully for " + serviceName )
except Exception as e:
# If an error occurred, print line number and error message
import traceback, sys
tb = sys.exc_info()[2]
report.write("Failed at step 1 \n" "Line %i" % tb.tb_lineno)
report.write(str(e))
report.close()
print ("Completed update of cache tiles for " + serviceName)