# Functions

A Task represents a planned or scheduled piece of code. The code definition is a Function in Extra Horizon.

## Deploying a Function

Developers can leverage the power of the Extra Horizon CLI to efficiently create new Functions and seamlessly update existing ones, this can be achieved by using the `exh tasks sync` command.

{% tabs %}
{% tab title="CLI" %}

```bash
exh tasks sync --name=my-function \
  --code=/my/path/to/my-function \
  --entryPoint=index.handler \
  --runtime="nodejs14.x"
```

See the [Extra Horizon CLI documentation](https://docs.extrahorizon.com/cli/) for more information.
{% endtab %}
{% endtabs %}

### Tutorial

A step-by-step guide on how to deploy a Function is available in the [hello world](/extrahorizon/services/automation/task-service/examples/hello-world-js.md) example.

{% hint style="info" %}
The [JavaScript SDK](https://docs.extrahorizon.com/javascript-sdk/) may be used within a Function to easily interface with Extra Horizon services.
{% endhint %}

## List functions

{% tabs %}
{% tab title="CLI" %}

```
exh tasks list <options>
```

See the [Extra Horizon CLI documentation](https://docs.extrahorizon.com/cli/) for more information.
{% endtab %}
{% endtabs %}

## Delete a function

In order to **Delete** a Task Function, you will need as a User to have the **DELETE\_TASK\_FUNCTION** permission.

{% tabs %}
{% tab title="CLI" %}

```
exh tasks delete <options>
```

See the [Extra Horizon CLI documentation](https://docs.extrahorizon.com/cli/) for more information.
{% endtab %}
{% endtabs %}

{% hint style="success" %}
If a function is removed from the task service its executions (tasks) and the logs aren't removed and can still be retrieved
{% endhint %}

## Function Properties

```json
{
  "name": "my-function",
  "description": "This is an example of a test Lambda function",
  "entryPoint": "index.handler",
  "runtime": "nodejs14.x",
  "timeLimit": 60,
  "memoryLimit": 256,
  "environmentVariables": {
    "CLIENT_ID": {
      "value": "my-token-value" 
    },
    "CLIENT_SECRET": {
      "value": "my-secret-value" 
    }
  },
  "retryPolicy": {
    "enabled": true,
    "errorsToRetry": [
      "CONNECTION_ERROR",
      "DATABASE_ERROR"
    ]
  },
  "executionOptions": {
    "permissionMode": "permissionRequired",
    "defaultPriority": 10
  },
}
```

### name

The `name` property serves as the unique identifier amongst all Functions.

### description

The `description` property may be supplied to provide a brief explanation regarding a Function's purpose.

### entryPoint

The `entryPoint` property refers to the method in the Function code that is executed.

### runtime

The `runtime` property selects the runtime (or environment) the Function will be running in. The Task Service utilizes AWS Lambda to run Functions. Any runtime supported by AWS Lambda is supported by the Task Service.

See the AWS Lambda documentation for a list of supported runtimes:\
<https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html>

### timeLimit

The `timeLimit` represents the maximum allowed execution time limit for this Function in seconds. This value may have a minimum value of `3` seconds and a maximum value of `300` seconds.

### memoryLimit

The `memoryLimit` allocates the desired memory for a Function's execution in MB. This value may have a minimum value of `128` MB and a maximum value of `10240` MB.

### environmentVariables

The `environmentVariables` represent the environment variables in the Function execution runtime.

The following code snippet shows an example how to configure the environment variables:

```json
{
  ...
  "environmentVariables": {
    "CLIENT_ID": {
      "value": "my-token-value" 
    },
    "CLIENT_SECRET": {
      "value": "my-secret-value" 
    }
  }
}
```

### retryPolicy

{% hint style="success" %}
Available since v1.3.0
{% endhint %}

The `retryPolicy` field can be used to determine system behavior after the execution of a Function (Task) fails.

The following code snippet shows an example how to configure the retry policy:

```json
{
  ...
  "retryPolicy": {
    "enabled": true,
    "errorsToRetry": [
      "CONNECTION_ERROR",
      "DATABASE_ERROR"
    ]
  }
}
```

#### retryPolicy Properties

`enabled`

The retry policy is disabled by default, If this field is set to `true`, the retry policy becomes active. If active the policy will retry a maximum of 3 times, with an increasing timeout of 2, 5 and 10 seconds respectively.

<figure><img src="/files/hAenM0m5VOVRwGP5hgFo" alt=""><figcaption><p>Visual representation of the fixed retry policy</p></figcaption></figure>

`errorsToRetry`

It is possible to restrict the retry policy to a specific set of errors using this property.

### executionOptions

The `executionOptions` field may be used to configure how the function can be executed.

The following code snippet shows an example how to configure the execution options:

```json
{
  ...
  "executionOptions": {
    "permissionMode": "permissionRequired",
    "defaultPriority": 10
  },
}
```

#### executionOptions Properties

`permissionMode`

This property determines execution restrictions based on its assigned value. One of the following values may be assigned to the `permissionMode` property to enforce execution restrictions.

{% hint style="info" %}
The permissionMode is only considered for direct execution of a Function or [API Functions](/extrahorizon/services/automation/task-service/api-functions.md).
{% endhint %}

| permissionMode       | Description                                                                                                                                                         |
| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `permissionRequired` | <p>A user requires a permission as stated for the <a href="#user-permissions">API Function permissions</a> section.<br><br>This is the default permission mode.</p> |
| `allUsers`           | A user must be logged in, but no permission is required.                                                                                                            |
| `public`             | Any party may execute this function without any restrictions.                                                                                                       |

For information regarding the application of permissions to users please refer to [global roles](/extrahorizon/services/access-management/user-service/global-roles.md).

`defaultPriority`

This property determines the priority assigned to all tasks created for this function, unless a priority is specified for a task explicitly.

{% hint style="info" %}
The defaultPriority is not considered for direct execution of a Function or [API Functions](/extrahorizon/services/automation/task-service/api-functions.md). Since those execute directly and don't take a priority into account.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.extrahorizon.com/extrahorizon/services/automation/task-service/functions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
