Take automation to the next level by scheduling tasks for Workforce for ArcGIS. If you are operating on a Windows machine, you can use the Windows task scheduler to automatically run Python scripts for your Workforce project at a customized date and time.
Here are a couple of examples of when you might want to schedule a Python script for Workforce:
- You want a daily report of all completed assignments, so you schedule the script to run at the end of each day.
- Every two weeks, you want to reset workers within your project to a status of “not working”, so you schedule the script to run every other Sunday.
All of the available Python scripts for Workforce are found in the workforce-scripts GitHub repository. If you are new to automating tasks for Workforce, you may want to check out this introductory blog series first: Automating Workforce with ArcGIS API for Python.
For this exercise, you will schedule the reset_stale_workers script using the Windows task scheduler. You can also use any script that makes sense for your Workforce project.
- Create the Python environment
- Clone the workforce-scripts repo and install dependencies
- Schedule the script in Windows Task Scheduler
Create the Python environment
First, you will create the Python environment that you'll use to run the Python scripts for Workforce. To do so, you'll first add the ArcGIS Pro helper scripts to your system path. Then you'll create a new conda environment to run the scripts in.
1. Sign into ArcGIS Pro with a licensed account. Then, exit the program.
Note: ArcGIS Pro comes with ArcGIS API for Python and Conda. If you don’t have ArcGIS Pro, you will need to download and install ArcGIS API for Python separately.
For more information, see Install and set up ArcGIS API for Python.
2. Add the ArcGIS Pro helper scripts that come with Pro to your system path environment variables. This will allow you to run conda in the command prompt.
On your Windows device, search for and open System (control panel)
In the System window, click Advanced system settings.
In the System Properties window, click Environment Variables.
In the Environment Variables window, under System variables, select Path. Then, click Edit.
In the Edit environment variable window, click New, and add the file path to the ArcGIS Pro Python scripts.
Here is the default file path: C:\Program Files\ArcGIS\Pro\bin\Python\Scripts
3. Open the ArcGIS Python Command Prompt. This should be located in:
Programs -> ArcGIS -> ArcGIS Pro -> Python Command Prompt
In the command prompt, run:
conda create -n workforce --clone arcgispro-py3 –y
This will create a new conda environment in the following location:
This is the environment that you'll use to run the Python scripts for Workforce.
Clone the workforce-scripts repo and install dependencies
Next, you'll clone the Workforce scripts to your machine and install the required Python libraries needed to run them.
1. Clone or download the Workforce scripts found in the GitHub repository.
2. In the ArcGIS API for Python Command Prompt, navigate to the Workforce scripts folder:
4. Install the required libraries using Python's default package installer, pip:
pip install -r requirements.txt
ArcGIS API for Python and the required libraries are now installed. You are now able to run Workforce scripts on your machine.
Schedule a script in Windows Task Scheduler
Finally, you will schedule a Python script to run using Windows Task Scheduler. You’ll schedule the reset_stale_workers script to run every other Sunday evening.
1. In the Windows Start menu, search for "Task Scheduler" and open the program. Click Actions > Create task. In the Create Task dialog box, name the task “Reset Workers” and select the Run whether user is logged on or not security option.
2. In the Create Task dialog box, click Actions > New.
The New Action window appears.
For the Program/script setting, copy and paste the file path for the Python environment you created earlier. The file path should read as follows, where <account-name> is your system account name:
For the Arguments setting, enter the complete file path to the script you want to run followed by the script’s parameters. It should look like this:
Be sure to edit the following parameters:
- Include the full path to your reset_stale_workers.py script.
- Edit the username, password, organization, and project-id to match your own credentials.
- Set the cutoff-date to 1. This tells your script to reset workers whose statuses haven’t changed in the past minute.
- Include the full path to your log.txt file.
In the Add arguments setting, add the complete file path to the script you want to run followed by the list of parameters (shown above).
Click OK to close the Edit Action window.
3. Create a new trigger
In the Create Task dialog box, on the Triggers tab, click New to open the New Trigger window. This is where you set how often you want your script to run. For this exercise, the script is set to run every two weeks on Sunday at 10:00PM.
Click OK to close the New Trigger window. The trigger you just created is now listed in the Triggers tab.
You can set multiple triggers for your script, depending on the needs of your Workforce project. Click OK to close the Reset Workers Properties window.
The script is now set to run every other Sunday. If you want to test that this script will work, you can run it manually at any time. Select the script in the Task Scheduler Library and click Run.
After running the script, you can view the log file to confirm that it worked.