# How Work Orders Are Generated

A Recurring or Condition template doesn't create work orders by itself. A Kanoa gateway timer does, and knowing how it
works explains why a work order can appear a little after it's due, and what to check when one never appears.

## The CMMS Schedule Timer

Kanoa runs a gateway timer called **CMMS Schedule**, about once a minute. Each run does two things, in this order:

1. `system.kanoa.cmms.checkWorkOrderSchedule()` creates the work orders that are due from **Recurring** templates,
   including the ones inside each template's **Generate Ahead** window.
2. `system.kanoa.cmms.checkWorkOrderConditions()` creates the work orders whose **Condition** has been met: run hours,
   counts or tag values.

You don't need a timer script of your own for either. The timer runs only:

- on the active gateway, if you have a redundant pair;
- while Kanoa Maintenance is licensed, or its trial is running;
- while **Enable Kanoa Maintenance** is on, under **Configuration → Application → Settings**.

It checks the last two before every run, so it starts again by itself once the license or the setting is back.

:::info[Changing the timer]
The Kanoa gateway config page has a **Timers** tab, where you can turn the CMMS Schedule timer off or give it another
interval. A saved interval stays after a restart.
:::

{/*
TODO: Confirm on a gateway the CMMS Schedule timer's default interval as shown on the Timers tab (the code registers it
at 45 seconds; JobControl lists 60 seconds as its default) and the exact Timers tab labels.
*/}

## Run It by Hand When Testing

While you're building templates, you don't have to wait for the timer. Run either check yourself and see how many work
orders it created.

:::action[Check for due work orders now]

1. Open the Script Console in the Designer.
2. Run:

   ```python
   created = system.kanoa.cmms.checkWorkOrderSchedule()
   print 'Recurring work orders created:', created
   created = system.kanoa.cmms.checkWorkOrderConditions()
   print 'Condition work orders created:', created
   ```

3. If either prints more than 0, open **Maintenance → Work Orders** and find the new work orders.

:::

Both functions sweep every template, the same as the timer does, so running them by hand is safe. A work order that's
already been created isn't created twice.

{/*
TODO: Confirm on a gateway that calling the checks by hand right after the timer has run creates no duplicates.
*/}

::embed-section{src="../../../systemFunctions/cmms/checkWorkOrderSchedule.md" collapsed="true" summary="Reference Materials: checkWorkOrderSchedule"}

::embed-section{src="../../../systemFunctions/cmms/checkWorkOrderConditions.md" collapsed="true" summary="Reference Materials: checkWorkOrderConditions"}

## When a Work Order Doesn't Appear

Check these, in order:

1. Is the template **Released**? Draft templates are never scheduled.
2. Is **Enable Kanoa Maintenance** on, and is the license (or trial) active?
3. Is the **CMMS Schedule** timer on, on the **Timers** tab?
4. For a Recurring template: is the next occurrence inside its **Generate Ahead** window yet?
5. For a Condition template: has the asset really passed the threshold since **Last serviced on**? Run hours only count
   while it's running.

Then run the checks by hand, as above, and see whether they create it.

You now know what turns a template into work orders, and how to test it without waiting.
