Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
protogrid:agents [2020-04-20 08:54] – [Execution] jus | protogrid:agents [2023-01-16 23:16] (current) – [Example] dru | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Agents ====== | ====== Agents ====== | ||
- | |||
Agents run periodically and execute code specified by the user. | Agents run periodically and execute code specified by the user. | ||
- | |||
- | Note: Agents are only available in billed environments that run Protogrid 2.1 or higher. | ||
===== Setup ===== | ===== Setup ===== | ||
- | |||
To set up an agent, there are two necessary steps: | To set up an agent, there are two necessary steps: | ||
- | | + | |
- | | + | |
+ | |||
+ | ==== Example ==== | ||
+ | - Create a Proto with a DateTime Field and a Text Field. Copy the key of the Text Field. | ||
+ | - Create a user profile with rights to edit the proto created above but without Environment Administrator rights. | ||
+ | - Create an Agent Library, specify the user created above as context user and copy the following code. Replace the value of // | ||
+ | - Create a Proto for parameter values with a Text Field, copy the key of the Text Field and replace // | ||
+ | - Add this Agent Library to your Proto. | ||
+ | - Based on this proto, create some cards where the date lies within the next 60 minutes. Wait one hour. | ||
+ | - The value in your Text Field should have been replaced by //Frist abgelaufen.// | ||
+ | |||
+ | <code javascript> | ||
+ | function agent_library() { | ||
+ | var fieldkey_status_field = ' | ||
+ | var cardkey_parameter_deadline_reached = ' | ||
+ | var fieldkey_parameter_value = ' | ||
+ | |||
+ | // Make sure that this function does not return until all operations and JSON-API requests are completed / fulfilled. | ||
+ | // We recommend to use await / async syntax: https:// | ||
+ | // * card [object]: | ||
+ | // * cookie [string]: | ||
+ | // * axios [object]: | ||
+ | // * baseurl [string]: Contains the base url needed for API requests | ||
+ | // * appname [string]: Contains the application name | ||
+ | // most URLs needed for API calls are structured like baseurl + ' | ||
+ | // return value: A boolean. If set to true, it will save the current card | ||
+ | async function on_schedule(card, | ||
+ | // Here is an example for an authenticated axios GET request: | ||
+ | try { | ||
+ | var response = await axios.get(baseurl + ' | ||
+ | ' | ||
+ | ' | ||
+ | } | ||
+ | }); | ||
+ | console.log(' | ||
+ | |||
+ | var loaded_card = monkey_patch_card(response.data.result); | ||
+ | var loaded_message = loaded_card.get_value(fieldkey_parameter_value, | ||
+ | card.set_element_value(fieldkey_status_field, | ||
+ | |||
+ | return true; | ||
+ | } catch (e) { | ||
+ | console.error(e); | ||
+ | return false; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | return { | ||
+ | on_schedule: | ||
+ | }; | ||
+ | } | ||
+ | |||
+ | </ | ||
===== Execution ====== | ===== Execution ====== | ||
+ | Agents are by default checked every hour (this can be modified for your environment, | ||
+ | |||
+ | Agents run in the context of a specific user profile which must not have Environment Administrator rights. This user's password is automatically reset during the execution process. So this user profile shall only be used for agents and not for physical users. All agent executions are logged in the // | ||
+ | |||
+ | While an agent is running in the context of a specific card, it is also possible to load and save other cards by calling the JSON API endpoints using axios. A valid axios module is given in the // | ||
+ | |||
+ | The // | ||
- | Agents are checked every hour. If any DateTimeField | + | ===== Best Practices for Calculation Code ====== |
+ | In the following, we have compiled some recommendations based on our experience for high stability, performance, | ||
+ | - It is recommended to use [[protogrid: | ||
+ | - Note: Use multi column indexind views at all only if the free combination of several filters is required in the user interface. | ||
+ | - Write to as few Relation Fields as possible | ||
+ | - Procedure if the calculation code requires a " | ||
+ | - If editing or deleting a Card in the user interface subsequently to calculation has to be prevented, this can be done in both cases by restricting the editing rights ([[protogrid: | ||
+ | - Read/load as few specific/single cards as possible. Work with views whenever possible. Use " | ||
+ | - Analogous to the previous point, write as few specific/ |