Process runs and gets expected result using calculate field tool, but will not work with ArcGIS Pro scheduler.

805
6
06-19-2020 02:23 PM
ErikLeigh
New Contributor III

Windows 10 Enterprise w/ ArcGIS Pro 2.5.1 and Python 3.

I have managed to schedule a field concatenation process on the same feature layer that is currently working on a schedule and that process works just fine.  For some reason, the following process will execute and create the data I want when I use the calculate field tool, but when I try to schedule it, the arc pro scheduler won't run it at all. There is no error apparent or log file created.  

I am not an experienced python programmer and have a very rudimentary idea of how this works.  I put this together using examples.  I have "copied" the python command that the tool generates below. Hopefully this can help.

I want the script to look and see if the field "advisory_lvl" contains text of "CAUTION" or "CLOSURE" and write the field DESC5 with the appropriate image URL.  As I said, this process works perfectly when I initiate it manually using the calculate field tool so I don't really know what's wrong with scheduling it.  

This does not work with the Pro scheduler tool:

arcpy.management.CalculateField(r"Harmful Algal Bloom Report 3\surveyPoint", "DESC5", "Reclass(!advisory_lvl!)", "PYTHON3", @"def Reclass(advisory_lvl):
if (advisory_lvl == ""CAUTION""):
return '<a href=""url""><img src=""url"" style=""width: 150px;""></a>'
elif (advisory_lvl == ""CLOSURE""):
return '<a href=""url""><img src=""url"" style=""width: 150px;""></a>'
", "TEXT")

This works with the Pro scheduler tool:

arcpy.management.CalculateField(r"Harmful Algal Bloom Report 3\surveyPoint", "DESC4", "'<b>Agency:</b>' + ' ' +  str(!agency!) + '<br>' + '<b>Sample Date:</b>' + ' ' + str(!sample_date!) + '<br>' + '<b>Advisory Level:</b>' + ' ' + str(!advisory_lvl!) + '<br>' + '<b>Advisory Issued:</b>' + ' ' + str(!advisory_issued!) + '<br>' + '<b>Advisory Lifted:</b>' + ' ' + str(!advisory_lifted!) + '<br>' + '<b>Cyanobacteria Density:</b>' + ' ' + str(!cyano_density!) + '<br>' + '<b>Microcystin:</b>' + ' ' + str(!microcystin!) + '<br>' + '<b>Anatoxin:</b>' + ' ' + str(!anatoxin!) ", "PYTHON3", '', "TEXT")

Please let me know if there is any other information I can provide.

Thanks for your help!

Update:

ArcPro scheduler creates a task within windows task scheduler.  The last run result returns (0x1). Looking at the general history panel it runs pythonw.exe with return code 2147942401. This is apparently some form of "illegal function".  No idea what or why, especially since other scripts run just fine. I did try running the script "run only when user is logged on" and that doesn't seem to affect anything.

0 Kudos
6 Replies
ErikLeigh
New Contributor III

I've tried most of the ideas I can find online and nothing.  Anyone else got anything?

0 Kudos
ErikLeigh
New Contributor III

Ok, so new focus on this same topic.  

ArcGIS Pro generates it's own python script to run in windows task scheduler.  When I try to run that same script in the command line I get the following syntax error.  Can anyone tell me what this is about? The up arrow is supposed to be pointing at the @ symbol. At least that's how it is displayed in the cmd window.

path\task.py
File "path\task.py", line 17
ret = arcpy.management.CalculateField(r"Harmful Algal Bloom Report 3\surveyPoint", "DESC5", "Reclass(!advisory_lvl!)", "PYTHON3", @"def Reclass(advisory_lvl):
^
SyntaxError: invalid syntax

0 Kudos
DavidPike
MVP Frequent Contributor

I would imagine the syntax error is in your Reclass(), I've looked at your reclass() code above and apart from struggling a bit with the indentation I can't see what the double-double quotes are for and why it's not erroring on those initially.

0 Kudos
ErikLeigh
New Contributor III

Full python code created by ArcGIS Pro. 

# scheduled task: HABS_DESC5
import datetime, arcpy
from xml.sax.saxutils import escape

taskname = "HABS_DESC5"
ret = None
ticks = int((datetime.datetime.utcnow() - datetime.datetime(1, 1, 1)).total_seconds() * 10**7)

try:
   arcpy.env.overwriteOutput = True
   project = arcpy.mp.ArcGISProject(r'path.aprx')
   active_map = next((it for it in project.listMaps() if it.getDefinition('v2').uRI == 'CIMPATH=map1/map1.xml'), None)

   if active_map:
      with active_map._arc_object:
         ret = arcpy.management.CalculateField(r"Harmful Algal Bloom Report 3\surveyPoint", "DESC5", "Reclass(!advisory_lvl!)", "PYTHON3", @"def Reclass(advisory_lvl):
      if (advisory_lvl == ""CAUTION""):
         return '<a href=""path"" style=""width: 150px;""></a>'
      elif (advisory_lvl == ""CLOSURE""):
         return '<a href=""path"" style=""width: 150px;""></a>'
", "TEXT")
   else:
      ret = arcpy.management.CalculateField(r"Harmful Algal Bloom Report 3\surveyPoint", "DESC5", "Reclass(!advisory_lvl!)", "PYTHON3", @"def Reclass(advisory_lvl):
         if (advisory_lvl == ""CAUTION""):
         return '<a href=""path"" style=""width: 150px;""></a>'
      elif (advisory_lvl == ""CLOSURE""):
         return '<a href=""path"" style=""width: 150px;""></a>'
", "TEXT")

except:
   pass

finally:
   ticks2 = int((datetime.datetime.utcnow() - datetime.datetime(1, 1, 1)).total_seconds() * 10**7)
   status = 0 if ret is None else ret.status if type (ret) == arcpy.Result else 4
   xml = f'<log status="{status}" start="{ticks}" end="{ticks2}" taskname="{escape(taskname)}">'
   xml += '<messages>'
   try: xml += ''.join([f'<msg code="{msg[0]}" type="{msg[1]}">{escape(msg[2])}</msg>' for msg in arcpy.gp.GetAllMessages()])
   except: pass
   xml += '</messages></log>'

log_file = r"path.log".format('%x' % ticks)
with open(log_file, 'w', encoding='utf-8') as file:
   file.write(xml)

0 Kudos
DavidPike
MVP Frequent Contributor

Again the indentation will put people off from helping you.

How have you constructed this tool? Is it from a script or ModelBuilder?

Your path to reference the dataset for CalculateField seems to be based upon the MapDocument being open and using the MapDocument = "Current" but I'm not too sure on the syntax being spat out by Pro here if I'm honest (my lack of knowledge more than anything). I would hazard however that you reference the full filepath to your dataset e.g. r"C:\MyGDB\Feature"

0 Kudos
ErikLeigh
New Contributor III

Sorry, It pastes without indentation so I went back in and edited it.  

Yes, the path was a full file path. I used the calculate field tool in ArcGIS Pro. Original input to the calculate field tool looks like this:

Expression:

DESC5=Reclass(!advisory_lvl!)

Code Block:

def Reclass(advisory_lvl):
      if (advisory_lvl == "Caution"):
         return '<a href="path"><img src="path" style="width: 150px;"></a>'
      elif (advisory_lvl == "Closure"):
         return '<a href="path"><img src="path" style="width: 150px;"></a>'

0 Kudos