Coderty

Chain

A chain is a set of processes with its own properties like scheduling, notifications, dependencies, outputs, etc.

This is an example of a basic chain with one process:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
{
"chains":[
{
"id": "EXAMPLE_CHAIN",
"name": "Name of the sample chain",
"triggers": [],
"depends_chains": [],
"processes": [
{
"id": "PROCESS_ONE",
"name": "Firt process of the chains",
"exec": {
"id": "shell_default",
"command": "echo 'Hello world'"
},
"chain_action_on_fail": {
"action": "end"
}
}
]
}
]
}

Identification

A chain has two identification fields:

id Which identifies the chain. with this identifier Runnerty will provide a global value (CHAIN_ID) that can be used in the whole chain. To know more about global values have a look here

1
2
3
{
"id": "EXAMPLE_CHAIN"
}

name It is a description of the chain. Runnerty will also provide the global value CHAIN_NAME

1
2
3
4
{
"id": "EXAMPLE_CHAIN",
"name": "Name of the sample chain"
}

Execution (Triggers)

A chain can be fired by using triggers. There is a bunch of different triggers, have a look at them and how to use them here

Custom values

It is possible to define and overwrite global values at chain level, setting a custom_values attribute:

1
2
3
4
5
6
7
8
9
{
"id": "EXAMPLE_CHAIN",
"name": "Name of the sample chain",
"custom_values":
{
"YYYY": "1986",
"MY_LOCAL_CHAIN_VALUE": "ABC"
}
}

Dependencies

It is possible to define dependencies with other chains or other chains processes. This means than a chain with dependencies will never execute before their dependencies are resolved.

You can define these dependency restrictions through the depends_chains property, like in the sample below:

1
2
3
{
"depends_chains": ["CHAIN_ONE"]
}

The chain of the example will not be executed until the chain with id CHAIN_ONE is finished. It is also possible to configure a set of dependencies to one chain’s process or more:

1
2
3
4
5
6
{
"depends_chains": {
"chain_id": "CHAIN_ONE",
"process_id": "PROCESS_ONE"
}
}

Notifications

With the notifications property, Runnerty can be set up to emit notifications during the chain status flow, fired up by the following callbacks:

In these notifications we could notify anything using notifiers.

The following example shows how to set up notifications for the different states of the chain through Telegram Notifier, publishing messages to a previously defined Telegram’s chatroom:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
{
"notifications": {
"on_start": [
{
"id": "telegram_default",
"message": "THE CHAIN @GV(CHAIN_ID) HAS STARTED"
}
],
"on_fail": [
{
"id": "telegram_default",
"message": "THE CHAIN @GV(CHAIN_ID) HAS FAILED"
}
],
"on_end": [
{
"id": "telegram_default",
"message": "THE CHAIN @GV(CHAIN_ID) HAS FINISHED"
}
],
"on_retry": [
{
"id": "telegram_default",
"message": "THE CHAIN @GV(CHAIN_ID) HAS RETRY"
}
],
"on_queue": [
{
"id": "telegram_default",
"message": "THE CHAIN @GV(CHAIN_ID) HAS QUEUE"
}
]
}
}

Note the usage of the global value and function @GV(CHAIN_ID) on the previous example. This value will be replaced with the chain’s id. Know more about global values here. Know more about functions here

Learn more about notifiers and how to configure them here.

Processes

In the processes array property can be defined all the processes that are going to be part of the chain.

Learn more about processes and how to configure them here.

1
2
3
4
5
6
7
8
9
10
11
12
{
"processes": [
{
"id": "PROCESS_ONE",
"name": "First process of the chain",
"exec": {
"id": "shell_default",
"command": "echo 'Hello world'"
}
}
]
}

Actions in chain when process fail

It is possible to define what action (end or retry) to perform at the chain level in case a process fails.

Retry it 2 times with a delay of 2 seconds (2000ms) if the process fails:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
"...": "...",
"processes": [
{
"id": "SAMPLE_PROCESS",
"...": "...",
"chain_action_on_fail": {
"action": "retry",
"delay": "1 min",
"retries": 2
}
}
]
}

End the chain if the process fails:

1
2
3
4
5
6
7
8
9
10
11
12
{
"...": "...",
"processes": [
{
"id": "SAMPLE_PROCESS",
"...": "...",
"chain_action_on_fail": {
"action": "end"
}
}
]
}

Delay property understands the following strings:

The space after the number is optional so you can also write 1ms instead of 1 ms. In addition, it also accepts numbers and strings which only includes numbers and we assume that these are always in milliseconds.

From: [Millisecond module](https://github.com/unshiftio/millisecond)

Iterable chains

An iterale chain is a chain that is going to be executed for each object in the array previously returned by another process.

For example, if we have a process which returns one objects array we can execute an iterable chain for each object in the array.

In the following example we are going to send an email to every user of the USERS table.

First, we have the chain get-users-email.json with a process which selects all the users’s email:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{
"id": "GET-USERS-EMAIL",
"name": "It gets all the user's names to send an email",
"triggers": [
{
"id":"schedule_default",
"schedule_interval": "1 */1 * * *"
}
],
"processes": [
{
"id": "GET-USER-EMAIL",
"name": "it gets all the users email from the database",
"exec": {
"id": "mysql_default",
"command": "SELECT email, name FROM USERS"
},
"output_iterable": "PROCESS_EXEC_DATA_OUTPUT"
}
]
}

Then, we assign the returned resultset by the MySQL SELECT query as an object array with the PROCESS_EXEC_DATA_OUTPUT value, as part of the property output_iterable. This way we are announcing this process will return an iterable output.

Now we are going to define the iterable chain “send-mail-to-user”

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
{
"id": "SEND-MAIL-TO-USERS",
"name": "it sends an email to the users returned",
"depends_chains": {
"chain_id": "GET-USERS-EMAIL",
"process_id": "GET-USER-EMAIL"
},
"iterable": "parallel",
"input": [
{
"email": "email"
},
{
"name": "name"
}
],
"processes": [
{
"id": "SEND-MAIL",
"name": "sends the email to the user",
"exec": {
"id": "mail_default",
"to": [
":email"
],
"message": "Hello :name",
"title": "Message set by Runnerty"
},
"chain_action_on_fail": {
"action": "retry",
"delay": "1 min",
"retries": 1
}
}
]
}

Here we can see some properties that the chain needs to iterate. First of all we have the dependencies on the depends_chains property. An iterable chain must depends on the process from the “mother chain” whom it iterates:

1
2
3
4
5
6
{
"depends_chains": {
"chain_id": "GET-USERS-EMAIL",
"process_id": "GET-USER-EMAIL"
}
}

With the iterable property we can choose if we want to iterate over in series or parallel:

1
2
3
4
5
6
7
{
"depends_chains": {
"chain_id": "GET-USERS-EMAIL",
"process_id": "GET-USER-EMAIL"
},
"iterable": "parallel"
}

With the input property we can assign the properties of each object returned by de mother’s process array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
{
"depends_chains": {
"chain_id": "GET-USERS-EMAIL",
"process_id": "GET-USER-EMAIL"
},
"iterable": "parallel",
"input": [
{
"email": "email"
},
{
"name": "name"
}
],
"...":"..."
}

Now, we can use these values anywhere in our iterable chain:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
{
"processes": [
{
"id": "SEND-MAIL",
"name": "sends the email to the user",
"exec": {
"id": "mail_default",
"to": [
":email"
],
"message": "Hello :name",
"title": "Message send by Runnerty"
},
"chain_action_on_fail": {
"action": "end"
}
}
]
}

In the example :email will be replaced with the user’s email and :name will be replaced with the user’s name.