> ## Documentation Index
> Fetch the complete documentation index at: https://docs.codenullapp.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Scheduled Jobs

> Schedule jobs are hooks that can be executed in a specific or period time

Schedule jobs are hooks that can be executed in a specific or period time

There are basically three ways to schedule Jobs

1. Execute every `x` time. For instance: Every hour, every 5 minutes, etc
2. Execute at a specific hour or day. For instance: Every day at 6 AM, Every first month day.
3. During your custom code (in hooks) you can schedule a Job. For instance, after 10 days I created a lead I want to send an email or notification or whatever.

## How to create a Scheduled Job

It's simple, go to hooks, create a new one and select the "Scheduled Job" type. Once you select this option on the "Type" dropdown you will see the next fields

<img src="https://mintcdn.com/codenull/9RtgKoAtzvpYtN-c/images/LrUOaNRuxyUfbT6RjnR7.png?fit=max&auto=format&n=9RtgKoAtzvpYtN-c&q=85&s=efb2304a2f3385e4ba29ba797cff4ecf" alt="Scheduled Job fields" width="2596" height="1080" data-path="images/LrUOaNRuxyUfbT6RjnR7.png" />

* **Interval**: Parameter to determine when to execute the Job
* **Run At Specific** Time: Active this if you want to run the job at any specific time or day
* **Start Date**: Set this when "Run At Specific Time" is active to determinate the initial execution, from this date the job will be executed using the interval param
* **Code**: The code that will be executed during the Job execution.

## Every `x` Time&#x20;

If you only want to execute a Job every `x` time you only need to set the `interval` field.

To define the interval field you can define it as a human-interval format, for instance

```
one minute
1.5 minutes
3 days and 4 hours
3 days, 4 hours and 36 seconds
```

To learn more about this standard read here: [https://github.com/agenda/human-interval](https://github.com/agenda/human-interval)

## Run At Specific Time

If you want to run at any specific time you need to configure `interval`, `run a specific time`, and `start date` fields.

You can configure the `start date` field for instance like:

```
tomorrow at noon
tomorrow at 6pm
```

## Create a Job from Hooks

If you want to create jobs dynamically from hooks, you need to do 2 steps

1. Create a  `Job` in Hooks Manager
   * Mark the `Dynamic Date` option
   * Copy the hook Id generated after you save it
2. Call that Job from the hook you want to use it

#### Example

We want to send reminders based on an Event Date. Every time a user creates an event, the system sends a reminder based on the date selected by the user.

Job Code

```javascript theme={null}
// Job code
const recordatorioEventos = async(job) => {
    const { eventoId } = job.attrs.data;
    //... code to send reminder
}

recordatorioEventos(options.job);
```

Hook Code

```javascript theme={null}
// hook code (after save Reminders)
const createJobAsync = require("./utils/getAgenda").createJobAsync;

const createJobEventos = async (instance) => {
    const hookId = '625a448921f01b0011cab12e';
    // the createJobAsync fn recieves 3 params
    // hookId, parameters for the Job, date when Job executes, interval (optional)
    await createJobAsync(hookId, {
        eventoId: instance.EventoId,
    }, instance.Fecha );
}
createJobEventos(instance);
```
