An introduction to tasks

A Mechanic task is responsible for interpreting the events that occur, executing any number of actions as a result.

Tasks can be installed from our library (also available on GitHub), and can be customized or even written from scratch (see I need something custom – what next?).

A task is comprised of several components:

  • Event subscriptions
  • User-configured options
  • A task script

Event subscriptions

Subscriptions specify which events a task is interested in. A subscription is simply an event topic, with an optional delay specifier on the end in hours, days, or weeks.

A task can have zero, one, or many event subscriptions. (If you don't give it any, it'll never run.)

Here are a couple examples:

  • shopify/customers/create – for customer signups
  • shopify/orders/paid+1.week – useful for following up with customers to see how their order went
  • mechanic/scheduler/tuesday – runs every Tuesday at midnight, in the shop's local timezone
  • mechanic/scheduler/daily+17.hours – runs every day at 5pm, in the shop's local timezone

Event subscriptions can be given as a simple list, with one subscription per line, but can also be given in Liquid. Defining a task's subscriptions allows them to dynamically respond to user configuration, as in this example:

{% if options.run_when_a_customer_is_updated__boolean %}
{% endif %}

Further reading:

User-configured options

Mechanic dynamically generates a user-friendly configuration form, based on options that are used in the task script. For example, using {{ options.recipient_address__email }} in your task script will add an email field to the task's options form, labeled "Recipient address". This way, users are able to provide data for the task's use, and all the developer needs to do is reference that data using the options variable.

Further reading:

Task script

Where the lion's share of a task's work is done. A task script is responsible for working with whatever data applies (from the current event, or from the Shopify API, or from some other source), and then rendering some number of action definitions, determining what happens next.

Further reading:

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.