# Subscriptions

A task **subscription** is the expression of a task's intent to receive certain [**events**](https://learn.mechanic.dev/core/events), filtering by [**topic**](https://learn.mechanic.dev/core/events/topics). A subscription consists of an event topic, optionally combined with a time **offset**, which creates a delay.

A task may have any number of subscriptions.

![](https://3145272362-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MQrnkixbmVHvKHMwAbm%2Fuploads%2Fgit-blob-55bfa1231fff8888d23d3200b064e87e39b414c0%2FScreen%20Shot%202022-05-05%20at%2010.09.07%20AM.png?alt=media)

## Delays

... are accomplished using subscription offsets, as described below. This heading is here for folks searching for a way to delay their tasks. ;)

## Offsets

A subscription offset (sometimes called a delay) defines the amount of time a task should wait or delay (!!) before responding to the incoming event. It's the easiest way to add a delay to a task's subscription to a specific topic. (For finer control over event timing, try using the `run_at` option of the [Event action](https://learn.mechanic.dev/core/actions/event).)

Subscription offsets are appended to the subscription topic, and are of the form "+1.hour". Offsets may be given using seconds, minutes, hours, days, weeks, months, or years. There is no limit to how large the subscription offset may be.

**A subscription with an offset looks like `shopify/customers/create+1.hour`.**

To learn more about scheduling work with Mechanic, see [Scheduling](https://learn.mechanic.dev/core/runs/scheduling).

{% hint style="success" %}
In practice, large offsets can make debugging difficult! If you're thinking about work to be done weeks or months or years from now, consider running an hourly or daily task that scans for work that's due to be done, instead of scheduling tasks for the distant future.
{% endhint %}

{% hint style="info" %}
In some cases, the first task run on a new mechanic/scheduler/daily task may not be performed when expected.

To illustrate: if a user creates a task at 9am Monday, subscribing to mechanic/scheduler/daily+10.hours, they will have to wait until *the following midnight* before the mechanic/scheduler/daily event is created. When that event's run is performed, the task's subscription offset will be calculated and applied, and the task run will be enqueued for 10 hours later. This means that the task will run for the first time on 10am Tuesday, *not* 10am Monday.
{% endhint %}

{% hint style="warning" %}
The \[Shopify variables]\(code/environment-variables.md#shopify-variables) available to tasks always contain data drawn from the event itself. If a task has a offset event subscription, this data may be outdated by the time the task runs.

To reload the data in a Shopify variable, use something like this:

```liquid
{% unless event.preview %}
  {% assign customer = customer.reload %}
{% endunless %}
```

Remember, Mechanic does not permit access to the Shopify API during [event preview](https://learn.mechanic.dev/core/tasks/previews). Using this `unless` statement ensures that reloading only happens during a live event.
{% endhint %}

## Using Liquid

A task's subscriptions are parsed for Liquid, at the time the task is saved. Combined with [**task options**](https://learn.mechanic.dev/core/tasks/options), this is an opportunity to generate subscriptions based on user configuration, adding or removing subscriptions based on the user's choice, or adjusting subscription offset based on a user-entered value.

One subscription is permitted per line. Blank lines and leading/trailing whitespace are permitted.

### Examples

{% tabs %}
{% tab title="Conditional subscription" %}

```liquid
shopify/orders/create

{% if options.send_email_when_order_cancelled__boolean %}
  shopify/orders/cancelled
{% endif %}
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Dynamic offset" %}

```liquid
shopify/orders/paid+{{ options.days_to_wait_before_followup__number_required }}.days
```

{% endtab %}
{% endtabs %}

{% tabs %}
{% tab title="Optional offset" %}

```liquid
shopify/customers/create{% if options.wait_one_hour__boolean %}+1.hour{% endif %}
```

{% endtab %}
{% endtabs %}
