Event Triggers
|
<< Click to Display Table of Contents >> Navigation: Reference Section > Installation and Deployment > License GUI > STP Settings > Event Triggers |
STP can run a command of your choosing when something happens in PrimeCut - a plate is reconciled, a quote is won, a workorder is closed. Use it to tell another system, such as an ERP, straight away: a curl call to its web service, or a script that reads the item from the PrimeCut REST API and passes it on.
Event triggers are set up per STP instance, on the Event Triggers tab of STP Settings.
Setting up a trigger
1.Open STP Settings from PC4LicGUI (running as Administrator) and select the Event Triggers tab.
2.Choose the Instance (datastore) the triggers are for.
3.Paste an API key that belongs to a PrimeCut user in this datastore and click Test key. The instance must be serving the REST API for the key to be tested. A stored key cannot be read back; leave the box blank to keep it.
4.Set the Timeout (seconds) - default 120 - and Poll every (seconds) - how often STP looks for new events, default 2, 1 to 300.
5.Select an event in the list. The list is split into Common and Rare events; each row says what raises the event and whether %itemid% is a plate ID or a workorder ID. Workorder events cover quotes too.
6.Type the Command, or click Set example for a working one: a curl.exe call that fetches the item from this instance's REST API into C:\Temp, taking the key from PRIMECUT_APIKEY. Typing a command ticks the event; untick it to stop running the command while keeping it. A command is one line - line breaks become spaces.
7.Check the Runs as preview under the command. It shows the command with sample IDs in place of the placeholders.
8.Save, and restart the STP service when the dialog offers. STP reads its settings, including the triggers, only when it starts, and it encrypts a newly pasted API key on that start.
Never put the API key on the command line. Commands get it in the PRIMECUT_APIKEY environment variable - read it from there in your script (%PRIMECUT_APIKEY% in a batch file), or let curl put it in the address, as the example does.
Placeholders
STP replaces these in the command before running it. Case does not matter.
Placeholder |
Replaced with |
%itemid% |
The plate or workorder ID, as listed for the event. For the remnant events it is the remnant. |
%itemkind% |
plate or workorder. |
%itempath% |
The item's REST path: plates/<id> or workorders/<id>. |
%eventname% |
The event's name with _ for spaces, for example Plate_reconciled. Characters other than letters and digits are dropped. |
%eventid% |
This occurrence's number in the event log. A resend keeps it, so the receiving system can ignore repeats. |
%datastore% |
The instance's datastore name. |
Environment variables
Each command runs with STP's own environment plus these.
Variable |
Value |
PRIMECUT_APIKEY |
The API key set on the tab. Empty if none is set. |
PRIMECUT_URL |
This instance's REST API root on the server, for example http://localhost:8080/primecut-rest/v1. Empty if the instance serves no REST API. |
PRIMECUT_DATASTORE |
As %datastore%. |
PRIMECUT_EVENTID |
As %eventid%. |
PRIMECUT_ITEMID |
As %itemid%. |
PRIMECUT_ITEMPATH |
As %itempath%. |
Testing a command
Select the event, type an item ID in the item box beside the command and click Test. The command runs now, with the same placeholders, environment and timeout STP uses and event ID 0, and the dialog shows whether it succeeded, what ran and its output. The key used is the one typed in the API key box, so paste it before testing.
The test runs as you, on the PC running the dialog. STP runs the command as its service account, which may see different folders and have different permissions.
What PrimeCut users see
Where an instance has event triggers set up, PrimeCut shows an Events menu. Its caption counts the user's own runs: Events (2) while two are queued or running, and Events !1 error!, in red and bold, when a command for an event they caused has failed and they have not yet looked. A failure also appears in their notifications.
Events > Triggered Events lists the commands the server ran for events the user caused in the last 7 days: when, the event and item, the state (Queued, Running, Succeeded, Failed with the reason, or Timed out), how long it took and how many times it has run. Select one and click Resend to run its command again; PrimeCut asks first, and warns when the run already succeeded, since the other system may receive the same thing twice.
Notes
•Commands run one at a time, in the order the events happened, in the background - saving in PrimeCut never waits for them.
•A command still running after the timeout is stopped, together with anything it started, and the run is recorded as Timed out.
•Exit code 0 is success; any other exit code is a failure. Output goes to the STP log (the Log tab), with the API key masked.
•The command is started directly, not through a command prompt, in the STP program folder. To use redirection or batch commands, run a .bat file or cmd.exe /c.
•After STP restarts it carries on from where it stopped, but events older than 24 hours are not run. A command that was running when STP stopped is recorded as failed, and can be resent.
•The events themselves are what PrimeCut, TouchCut and the REST API record; the explanation on each row says which of them raise it.