Skip to content

Commit ba6bb20

Browse files
korydraughnalanking
authored andcommitted
[#187] README: Explain how to pass values back to the caller, across REP boundaries.
1 parent a5a52b3 commit ba6bb20

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,3 +439,66 @@ callback.writeLine('serverLog', 'hello, server log!')
439439
```
440440

441441
Note that the `writeLine` microservice can also target `stdout` in addition to `serverLog`. `callback.writeLine('stdout', ...)` will have the same effect as running `print`. The output will be discarded.
442+
443+
### How do I pass values back to the caller, across rule engine plugin boundaries?
444+
445+
This is achieved by writing a value back _(as a string)_ to the appropriate index within the `rule_args` array. The caller is required to pass one or more variables as **out** parameters.
446+
447+
Here's an example demonstrating how to pass a value, via the input arguments, from the PREP to the native rule engine plugin (NREP).
448+
449+
The following is a python function which overwrites the value of the first input argument.
450+
```python
451+
# file: core.py
452+
453+
def get_magic_number(rule_args, callback, rei):
454+
# Notice the integer is represented as a string.
455+
# Only strings can be passed across rule engine plugin boundaries.
456+
#
457+
# Take note of the index we're writing to. The index represents the
458+
# position of the input argument. Because this rule expects one input
459+
# argument, we use index 0. If there were more input arguments, we could
460+
# write to those as well using the index which identifies them.
461+
rule_args[0] = '42'
462+
```
463+
464+
And here is the NREP rule which invokes the PREP rule.
465+
```python
466+
# file: core.re
467+
468+
nrep_rule()
469+
{
470+
# Due to how the NREP works, we must initialize the variable to a string.
471+
# The value of the string isn't important.
472+
*magic_number = '-1';
473+
474+
# Now, we invoke the Python rule in core.py. Remember, the rule takes one
475+
# input argument. That argument maps to index 0 within the python rule.
476+
get_magic_number(*magic_number);
477+
478+
# Finally, the value is written to the log file.
479+
writeLine('serverLog', 'The magic number is [*magic_number].');
480+
}
481+
```
482+
483+
It's important that the value passed across rule engine plugin boundaries be of type string. Passing any other type will result in an error.
484+
485+
Upon invoking `nrep_rule`, a message very similar to the one that follows will appear in the server log file. See the **log_message** property. It should contain `[42]` instead of `[-1]`.
486+
```js
487+
{
488+
"log_category": "legacy",
489+
"log_level": "info",
490+
"log_message": "writeLine: inString = The magic number is [42].\n",
491+
"request_api_name": "EXEC_MY_RULE_AN",
492+
"request_api_number": 625,
493+
"request_api_version": "d",
494+
"request_client_user": "rods",
495+
"request_host": "10.15.164.3",
496+
"request_proxy_user": "rods",
497+
"request_release_version": "rods4.3.1",
498+
"server_host": "76f890b83c4d",
499+
"server_pid": 2637,
500+
"server_timestamp": "2024-04-10T12:43:31.381Z",
501+
"server_type": "agent",
502+
"server_zone": "tempZone"
503+
}
504+
```

0 commit comments

Comments
 (0)