Task 5 – Submit a WorkItem
When you post a WorkItem to the Automation Service, you are instructing the service to execute an Activity.
The relationship between an Activity and a WorkItem can be thought of as the relationship between a “function definition” and “function call”. Named parameters of the Activity have corresponding named arguments of the WorkItem. Like in function calls, optional parameters of the Activity can be skipped and left unspecified while posting a WorkItem.
By the end of this task, you will be able to:
- Create a WorkItem to execute an Activity.
- Check if execution succeeded or failed.
- Get the URL to the execution log file.
You will use the following operations to work with WorkItems in this task:
| HTTP Request | Description |
|---|---|
| POST /workitems | Creates a new WorkItem and queues it for processing. |
| GET /workitems/{id} | Gets the status of a specific WorkItem. |
Step 1 - Get a 3LO token
To get a 3LO token, follow the Get a 3-Legged Token tutorial in the APS Authentication documentation. The token must include the following scopes:
data:readdata:writedata:createdata:searchaccount:readprofapi:core-all-profile:readopenid
The code:all scope is additionally required to submit a WorkItem with the 3LO token.
For an overview of all the authentication and authorization options when submitting Fusion WorkItems, see Fusion Team Integration.
Step 2 - Create a WorkItem
To create a WorkItem to execute the Activity ConfigureDesignActivity:
Request
curl -X POST \ 'https://developer.api.autodesk.com/da/us-east/v3/workitems' \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer <YOUR_ACCESS_TOKEN>' \ -d '{ "activityId": "<YOUR_NICKNAME>.ConfigureDesignActivity+my_current_version", \ "arguments": { "adsk3LeggedToken": "<YOUR_3LO_TOKEN>", "TaskParameters": "{\n \"hubId\": \"<YOUR_HUB_ID>\",\n \"fileURN\": \"urn:adsk.wipprod:dm.lineage:shuH8zKvThW_4Tdu-m22sw\",\n \"parameters\": {\n \"d3\": \"40mm\"\n }\n}" } }'Show More
Note
Attribute Description activityIdThe fully qualified id of the activity (owner.activity+alias) this WorkItem will execute. argumentsContains all the parameters that need to be passed to the Activity specified by activityId. They must match the parameters you specified in Task 5, when you created the Activity. In this case we send over the 3LO token in theadsk3LeggedTokenargument so the Automation Service can access files on Fusion Team on behalf of the signed-in user. TheTaskParametersargument carries the script inputs: thehubIdof the Fusion Team hub to use, thefileURNof the design we want to change, and"d3": "40mm"as a map of parameters to change the height of the nut to 40mm.hubIdThe id of the Fusion Team hub that contains your design. You can find it in the URL when you open your hub in Fusion Team: it is the subdomain at the start of the address. For example, if your Fusion Team URL is https://autodesk5682.autodesk360.com/g/all_projects/active, the hubId isautodesk5682. The script uses this value to set the active hub before opening the file, so it must match a hub the 3LO user has access to. You must supply a hubId for the hub that contains your design; the script raises an error if no matching hub is found.priority(Beta)Coming soon. Optional. The priority tier of this WorkItem.- Supported values areP1andP3, case-insensitive.-P3, the standard queue, is equivalent to leaving it out.- Any other value is rejected with an error.- See beta limits and terms.- Send feedback and questions about the beta at feedback.autodesk.com/flexiblecompute.Setting the active hub in the script
The active hub must be set before your script works with any files. The Automation engine does not infer the hub from the
fileURN, so the script has to select it explicitly using thehubIdyou supply inTaskParameters, before it opens, copies, or saves anything. You must provide ahubIdthat points at the hub containing your design.In the sample
main.ts, thegetDocumentfunction resolves thehubIdand sets it as the active hub:// Possible hubId formats: base64 encoded string, or business:<id>, // or personal:<id> (deprecated) const hub = app.data.dataHubs.itemById(hubId) || app.data.dataHubs.itemById(`a.${adsk.btoa(`business:${hubId}`, true)}`) || app.data.dataHubs.itemById(`a.${adsk.btoa(`personal:${hubId}`, true)}`); if (!hub) throw Error(`Hub with id ${hubId} not found.`); adsk.log(`Setting hub: ${hub.name}.`); app.data.activeHub = hub;Show MoreThe script accepts the
hubIdin either of two formats and tries each in turn:
- The short hub name taken from your Fusion Team URL — the subdomain at the start of the address. For example, for
https://autodesk5682.autodesk360.com/g/all_projects/activethe hub name isautodesk5682. The script wraps this intobusiness:<name>(and falls back to the deprecatedpersonal:<name>), base64-encodes it, and looks up the hub.- The full hub id as returned by the Data Management API (for example,
a.YnVzaW5lc3M6YXV0b2Rlc2szNzQz). This is matched by the firstitemById(hubId)call. See GET hubs in the Data Management API documentation for how to retrieve it.Whichever format you use, the id must match a hub the signed-in 3LO user can access, and
app.data.activeHubmust be set before the script works with any files.The response contains the
idof the posted WorkItem:{ "status": "pending", "stats": { "timeQueued": "2023-09-27T08:52:30.7161306Z" }, "id": "<YOUR_WORKITEM_ID>" }
Step 3 - Check status of the WorkItem
WorkItems are queued before they are processed. A WorkItem’s processing time will vary depending on the size and complexity of the input files, the type of processing done by the AppBundle, and the size of the output files.
In this walkthrough, you will be checking the WorkItem status to see if it has completed.
However, the best practice is to use the onComplete argument when submitting a WorkItem.
The onComplete argument lets you specify a callback URL, which will be called once the WorkItem is completed.
For more information see the documentation on callbacks here.
You can check the status of a WorkItem by calling [GET] /workitems/{id}:
Request
curl -X GET \
'https://developer.api.autodesk.com/da/us-east/v3/workitems/<YOUR_WORKITEM_ID> \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer <YOUR_ACCESS_TOKEN>'
Response
{
"status": "success",
"reportUrl": "https://dasprod-store.s3.amazonaws.com/workItem/<YOUR_NICKNAME>/<YOUR_WORKITEM_ID>/report.txt?..(Truncated)",
"stats": {
"timeQueued": "2023-09-27T08:52:30.7161306Z",
"timeDownloadStarted": "2023-09-27T08:52:30.882113Z",
"timeInstructionsStarted": "2023-09-27T08:52:31.6418619Z",
"timeInstructionsEnded": "2023-09-27T08:52:55.0320553Z",
"timeUploadEnded": "2023-09-27T08:52:55.4675607Z",
"timeFinished": "2023-09-27T08:52:55.9182974Z",
"bytesDownloaded": 134691,
"bytesUploaded": 360182
},
"id": "<YOUR_WORKITEM_ID>"
}
| Attribute | Description |
|---|---|
status |
Indicates if execution is pending, successful, failed or cancelled. |
reportUrl |
The URL to get the report log for this WorkItem’s execution. |
Additional notes
(Beta) Priority Pass is coming soon for Fusion during the public beta. Until then, a P1 request for Fusion is rejected with an error. To check whether it is available, call GET engines/:id and look for "priorityPass": true.
The optional WorkItem attribute priority places a WorkItem in a priority queue. Priority affects the position of the WorkItem in the queue; it is not a commitment on queue time, processing time or completion time. During the beta, each client ID can have one P1 WorkItem. A second P1 request is rejected with an error that includes the reason.
Process a priority WorkItem
To use a priority queue, add "priority": "P1" to the WorkItem payload.
{
"activityId": "<YOUR_NICKNAME>.ConfigureDesignActivity+my_current_version",
"arguments": { ... },
"priority": "P1"
}
For when to use Priority Pass, the beta limits and the beta terms, see High Memory, GPU and Priority Pass (Beta). Send feedback and questions about the beta at feedback.autodesk.com/flexiblecompute.