Script Not Working When Published as GeoProcessing Service

1837
3
03-11-2014 12:13 PM
GeorgeHaskett
Occasional Contributor III
Hello.

I have the following script that works fine when I run it via ArcGIS, however after publishing it to a GeoProcessing service using 10.2, it fails to run properly.  Any suggestions as to what might be wrong with my script or settings?

# Importing Modules
import os,arcpy,sys,traceback,time,shutil
from datetime import datetime
from arcpy import env


### Establish Local Variables For Exporting Reports
print "*****************************************"
print "Welcome to the Agent RFI Report tool."
print "Establishing local variables..."
arcpy.AddMessage("Establishing Variables")
print ""

try:
  agentNO = arcpy.GetParameterAsText(0) 
  reportFolderDated = "C:/arcgisserver/gisData/projects/RFIs/results/"
  reportFolder = "C:/inetpub/wwwroot/flexviewers/_supportDocs/reports/"
  pngPath = "C:/arcgisserver/gisData/projects/RFIs/reportTemplates/RFI_Report.png"
  rlfPath = "C:/arcgisserver/gisData/projects/RFIs/reportTemplates/RFI_Report.rlf"
  pdfReport = reportFolder+"RFIreport.pdf"
  pdfDatedReport = reportFolderDated+"Agent"+agentNO+"_{}.pdf".format(datetime.strftime(datetime.now(),"%Y-%m-%d_%H%M"))
  print "Variables Established"

  arcpy.env.overwriteOutput = True

  ### Establish Local Variables for Reports
  mxdReportMap = "C:/arcgisserver/gisData/projects/RFIs/reportTemplates/RFI_Report.mxd"  
  mxd = arcpy.mapping.MapDocument(mxdReportMap)
  df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
  lyr = arcpy.mapping.ListLayers(mxd, "Nexus DB", df)[0]

  ### Generate report
  pdfDoc = arcpy.mapping.PDFDocumentCreate(pdfDatedReport)
  whereClause = "AGENTNUMBER = "+str(agentNO)+" AND LAT_DEC > "+str(0)
  print "Query: " + whereClause
  lyr.definitionQuery = whereClause
  print "Features Selected"
  df.extent = lyr.getSelectedExtent()
  arcpy.mapping.ExportToPNG(mxd,pngPath,resolution=200)
  print "PNG exported."
  arcpy.AddMessage("PNG Exported")
  arcpy.mapping.ExportReport(report_source=lyr,report_layout_file=rlfPath,output_file=pdfDatedReport)
  pdfDoc.saveAndClose()

  if os.path.exists(pdfReport):
    os.remove(pdfReport)
  print "check pdf"
  print "PDF Report Compiled."
  arcpy.AddMessage("PDF Report Compiled")
  print "Validate Report."
  shutil.copy(pdfDatedReport,pdfReport)
##  arcpy.SelectLayerByAttribute_management(lyr,"CLEAR_SELECTION")

except:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "Oops, you have PYTHON ERRORS:\nTraceback Info:\n" + tbinfo + "\nError Info:\n" \
    + str(sys.exc_type) + ": " + str(sys.exc_value) + "\n"
    msgs = "Oops, you have ARCPY ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    arcpy.AddError(msgs)
    arcpy.AddError(pymsg)
    arcpy.AddMessage(msgs)
    arcpy.AddMessage(pymsg)
    arcpy.AddMessage(arcpy.GetMessages(1))
0 Kudos
3 Replies
RichardWatson
Frequent Contributor
You did not say what failed when you ran your tool.  I assume that this is a single machine on which you have both ArcMap and ArcGIS Server installed.

I have never written a GP tool but I am trying to understand the process.

ArcGIS Server runs as a specific user.  By default it creates a user whose name is "arcgis".  That account must have access to all the files that you reference in your script.  If you login as user arcgis does the script run correctly from ArcMap?
0 Kudos
JonathanQuinn
Esri Notable Contributor
Return at least "Error" messages for the service under the service properties > Parameters and run the tool again.  Check the server logs for the error returned.  That will give you a good start on troubleshooting.  You can post the error you see here as well.
0 Kudos
deleted-user-mezzanRtr2IZ
New Contributor III

Hi George,

You've probably already discovered this as this post is pretty old now, but ExportReport will not function as a geoprocessing service and is the likely cause of your gp service failure (it is for me running 10.3).  Have you found a workaround for this?

Jeff

0 Kudos