# Distributed Cron Task

AbyssMonitor provide an easy way to setup Cron Task using a distributed manager.\
From this tools, you will be able to create a secured task insuring trigger and logging.

### Workflow

<img src="/files/6Dt21wCBGNdXnQXynK63" alt="Workflow for distributed Cron Task on AbyssMonitor" class="gitbook-drawing">

### Setup

First, you have to create a new Cron Task on your Application interface.

<figure><img src="/files/x2WjQrEsUH9trRVsJRVR" alt=""><figcaption><p>Cron Task creation form</p></figcaption></figure>

A new card is added. You can edit your Cron Task and copy the secret signature.

<figure><img src="/files/KQxmUt4ThzwOk48fmqtt" alt=""><figcaption><p>Cron Task secret</p></figcaption></figure>

### Authentifie Cron Task request

Using the secret signature, you can authentifie the request.

{% code overflow="wrap" lineNumbers="true" %}

```typescript
import {
  HEADER_SIGNATURE_NAME,
  SIGNATURE_ALGORITHM,
} from '@abyss-project/monitor';

const headerSignature = req.headers[HEADER_SIGNATURE_NAME];

const hmac = crypto.createHmac(
  SIGNATURE_ALGORITHM,
  "my-secret-signature",
);
const signature = hmac.update(JSON.stringify(req.body)).digest('hex');

if (headerSignature !== signature) {
  // Throw an error
  throw new Error('Invalid Signature');
}
// The signature is correct

// Response with the requestId of the AbyssMonitor logger to link logs to the cronTask
// ex: res.status(200).json(getRequestContext().requestId);
```

{% endcode %}

{% hint style="info" %}
Your Cron Task can generate logs. Logs will be linked to the Cron Task to debug easily.

Check code example above.
{% 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://abyss-project.gitbook.io/abyss-monitor/tutorials/distributed-cron-task.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.
