Skip to content

Move an item to a new state at a deadline

Save a due time on the Item. Add an Event with a schedule, kept to the state the Item starts in, and a Listener on it. The Listener compares the due time with ctx.now, and executes a transition when the time has come.

In the tickets App, open creates each ticket with a due time four hours after the call:

tickets.ts
return await general.createChild(current.templates.ticket, {
properties: {
dueAt: ctx.now.plus({ hours: 4 }),
openedBy: ctx.principalId,
subject: input.subject,
},
});

Alice opens “Printer” at midnight, and Carol opens “Laptop” at 2:00:

Clock: Wed, 01 Jan 2020 00:00:00 GMT
As Alice: "Printer" is open, due Wed, 01 Jan 2020 04:00:00 GMT
Clock: Wed, 01 Jan 2020 02:00:00 GMT
As Carol: "Laptop" is open, due Wed, 01 Jan 2020 06:00:00 GMT

breach is the transition from open to overdue. The schedule publishes slaCheck for each open ticket, and markOverdue executes breach on a ticket that is due:

tickets.ts
const slaCheck = ticket.addEvent('slaCheck');
slaCheck.addSchedule('hourly', '0 * * * *').state(['open']);
slaCheck.addListener('markOverdue', async (_event, item, _current, ctx) => {
const dueAt = item.propertyValues.dueAt;
const breach = item.state?.transitions.breach;
if (dueAt?.type !== 'datetime' || breach === undefined) return;
if (dueAt.value <= ctx.now) await breach.execute({});
});

0 * * * * is a crontab: every hour, on the hour. A ticket that has left open has no breach transition, and the Listener leaves it as it is.

Look at the list Bob sees at 5:00. “Printer” is an hour past its due time, and “Laptop” has an hour to go:

Clock: Wed, 01 Jan 2020 05:00:00 GMT
As Bob: sees "Printer", overdue
As Bob: sees "Laptop", open

A ticket moves at the first check that finds it due, not at the due time itself. The Runtime publishes slaCheck when the queued work runs and an hour mark has passed since the last run, once for that run however many hours have passed.

The whole App: Support tickets with an SLA. Step by step: Tutorial: what happens next.