Jobs
Schedules and options
Cron or interval, run on start, overlap, timeout and the connection rule.
A JobBuilder runs its handler on a schedule. setCron(expression, { timezone }) takes a cron expression,
parsed by croner, with an optional IANA timezone. setInterval(seconds)
takes a delay between runs. Exactly one of the two is required.
import { JobBuilder, MessageBuilder } from '@meshcorejs/client';
import { config } from '../config.js';
import { weather } from '../weather.js';
export default new JobBuilder()
.setName('bulletin')
.setCron('0 7 * * *', { timezone: config.timezone })
.setOverlap('skip')
.setTimeout(60)
.setHandler(async (client) => {
const channel = client.channels.get(config.channel);
if (!channel) return client.logger.warn(`bulletin: channel ${config.channel} is not on the radio`);
const [today, tomorrow] = await weather.forecast(2);
const lines = [];
if (today) lines.push(`Today ${weather.format(today)}`);
if (tomorrow) lines.push(`Tomorrow ${weather.format(tomorrow)}`);
await channel.send(new MessageBuilder().setTitle('🌄 Good morning').addLines(lines).setOverflow('truncate'));
});
Options
| Method | Effect |
|---|---|
setRunOnStart() | Also runs the job once when it starts. Off by default. |
setOverlap('skip' | 'queue') | What happens when a run is due while the previous one is still going. skip drops it (default), queue runs it right after. |
setTimeout(seconds) | Aborts the run past that delay. The handler's signal is aborted with a JobTimeoutError. 0 disables it (default). |
setRequiresConnection(bool) | Skips a scheduled run while the radio is disconnected. On by default. A manual run() always runs. |
export default new JobBuilder()
.setName('health-check')
.setInterval(300)
.setRunOnStart()
.setOverlap('queue')
.setTimeout(30)
.setRequiresConnection(false)
.setHandler(async (client, job) => {
if (job.signal.aborted) return;
client.logger.info(`last run: ${job.lastRun?.toISOString() ?? 'never'}`);
});