I'm wanting to publish a python script as a geoprocessing service (a secured service). Is there any way for arcpy to get the name of the arcgis server user who calls the gp task?
e.g. I log in and run the GP tool from the rest url, the script grabs my login name and the current datetime and adds it to a log table. We then end up with a log table of all users and times the script was run.
Did you ever get an answer for this? I did exactly this for a map service using an SOI (java), but unfortunately you cannot write SOE/SOI's for geoprocessing services (which I am trying to figure a work around for now).
It seems as though you can achieve this by causing the GP tool to create a record in an editor-tracked table. At least, that is what I've observed with a secured GP service + web appbuilder + EGDB. Is there a reason why that's not practical in this case?
You mention "...by causing the GP tool to create a record in an editor-tracked table". The original question was about how a GP tool would know who the user is. Creating a record is easy of course, but no one yet has mentioned a way that the GP tool would actually know the identity/username of the user who triggered the GP service. If you know of a way, please share!
I was able to pull the user name out of the jobinfo.json file that is created when a GP tool runs. This file ends up in the Jobs directory where the results of GP tools end up. Such as:
c:\arcgisserver\directories\arcgisjobs\<folder name>\<service>_gpserver\<job id>\jobinfo.json
The jobinfo.json file has the user and the machine that ran the service. A new directory with GP results gets created when the code runs. I snag the path and job id by using arcpy.env.scratchFolder. Then I read the jobsinfo.json file to grab the user and write it to a log.