Add a queue and a cron schedule
This chapter prepares the sync for bursts and recurring work. Your import job runs one file at a time, but the day you backfill Dynamics with SAP's historical exports, twenty files land in the hand-off folder at once. Until cutover, the two systems also stay aligned only if a full re-sync runs every night, whether or not a file arrived. You add a queue provider and a worker concurrency so I/O-bound bursts overlap, and a cron schedule that fires on a cadence rather than an event.
What you will build
A workers config that names a queue provider and a worker concurrency, so I/O-bound
imports overlap instead of running one by one, and a scheduled trigger
(defineScheduledTrigger) that enqueues a re-sync job on a cron cadence. You also see where the
running concurrency comes from under Aspire.
Before you begin
You need the my-erp/ workspace from chapter 2 with the
import-products job and product-import-trigger working, and aspire start healthy. Confirm the
job is registered:
ns-workers show-job import-products --json
import-products should appear in the list. If it does not, return to chapter 2 and re-run
netscript generate plugins.
Step 1 — Choose a queue provider
A NetScript queue is provider-agnostic: the same job-enqueue path runs on the zero-config Deno KV
default on your laptop and on a real broker under Aspire, with no change to your job code. The
QueueProvider enum has four selectable production backends, and auto-discovery picks one when you
do not pin one:
| Provider | How you select it | Reach for it when |
|---|---|---|
deno-kv | Auto-discovery fallback, or pin QueueProvider.DenoKv | Local dev and single-instance apps with no broker. Durable to the Deno KV store; needs --unstable-kv. |
redis | Auto-discovered when a Redis/Garnet connection is present; or pin QueueProvider.Redis | High-throughput production with a Redis/Garnet resource already provisioned by Aspire. |
rabbitmq | Auto-discovered first when an AMQP broker is present; or pin QueueProvider.RabbitMQ | Broker-grade routing and reliability; the top of the auto-discovery probe. |
postgres | Explicit only — pass QueueProvider.Postgres; never auto-discovered | You already run Postgres and want the queue in the same transactional store. Row-claim via FOR UPDATE SKIP LOCKED. |
For the ERP sync, let the environment decide. Write provider-neutral config and you get Deno KV
locally, then the Aspire cache once it is up: redis by default, or garnet via
--cache-backend. The auto-discovery probe order is RabbitMQ → Redis → Deno KV.
Step 2 — Size worker concurrency in config
concurrency declares queue concurrency. Jobs execute in-process in the background worker and
share its event loop. Raising concurrency lets I/O-bound imports overlap; it does not allocate V8
isolates or speed up synchronous CPU-bound transformations. Set it in the generated worker config:
// config/official-plugins/mod.ts
import { defineWorkers } from '@netscript/plugin-workers-core/config';
export const workers = defineWorkers({
jobsDir: './workers/jobs',
tasksDir: './workers/tasks',
queueProvider: 'auto', // Deno KV locally; the Aspire cache once up (redis default, garnet alt).
queueName: 'jobs',
concurrency: 4, // Queue concurrency: overlap up to 4 I/O-bound jobs; no CPU parallelism.
enabled: true,
groups: [],
});
| Name · Type | Description |
|---|---|
concurrencynumber |
Default queue concurrency for in-process jobs. Schema default 2; no isolate pool is allocated. |
queueProvider'auto' | 'deno-kv' | 'redis' | 'postgres' | 'amqp' |
Queue backend. 'auto' resolves one for you. Default 'auto'. |
queueNamestring |
Queue the runner consumes from. Default 'jobs'. |
jobsDir / tasksDirstring |
Directories scanned for default-exported job and task modules. |
groupsWorkerGroupData[] |
Per-topic worker groups, each with its own scaling.concurrency and retention. |
enabledboolean |
Whether workers run at all. Default true. |
For per-topic control, such as a busy imports topic at concurrency 10 while a heavy reports
topic stays at 1, use a WorkerGroup with its own scaling: { mode, concurrency }.
Tune the worker runtime covers the per-topic
and runner-mode settings.
WORKERS_CONCURRENCY sets the running queue concurrency. Aspire injects it on the workers
background resource with the plugin's declared default, so set it only to override that value.
Step 3 — Add a cron schedule
Some migration work runs on time, not on files: the nightly full re-sync that keeps Dynamics
aligned with SAP until cutover, or an hourly cleanup of stale staging files. That is a
scheduled trigger, defineScheduledTrigger(handler, spec) from
@netscript/plugin-triggers-core/builders. The add-scheduled verb, written as add scheduled,
can scaffold the job action already wired. ns-triggers and ns-workers are the plugin CLI
shorthands you installed in chapter 2.
ns-triggers add scheduled daily-resync-schedule \
--cron="0 6 * * *" \
--timezone=UTC \
--job=import-products \
--description="Runs a full product re-sync every morning." \
--tags=resync,products,scheduled
The command writes triggers/daily-resync-schedule-trigger.ts and refreshes the trigger registry.
Like the file-watch trigger, its generated handler returns an array of effects; the --job flag
supplies the enqueue action, so you don't write the definition by hand:
// triggers/daily-resync-schedule-trigger.ts (generated excerpt)
import { defineScheduledTrigger, enqueueJob } from '@netscript/plugin-triggers-core/builders';
import type { JobDefinition } from '@netscript/plugin-workers-core';
const importProductsJob = {
id: 'import-products' as JobDefinition<'import-products'>['id'],
name: 'Import Products',
topic: 'default',
entrypoint: './workers/jobs/import-products.ts',
} satisfies JobDefinition<'import-products'>;
export const dailyResyncSchedule = defineScheduledTrigger(
(event) => Promise.resolve([enqueueJob(importProductsJob, { payload: event.payload })]),
{
id: 'daily-resync-schedule',
cron: '0 6 * * *', // every day at 06:00
timezone: 'UTC',
description: 'Runs a full product re-sync every morning.',
tags: ['resync', 'products', 'scheduled'],
},
);
export default dailyResyncSchedule;
| Name · Type | Description |
|---|---|
idstring |
Stable identifier for the schedule, used in logs and the events feed. |
cronstring |
Standard 5-field cron expression. '0 6 * * *' = daily at 06:00; '*/5 * * * *' = every five minutes. |
timezonestring |
IANA timezone the cron is evaluated in. Set it explicitly ('UTC' here) so the cadence is unambiguous. |
description / tagsstring / string[] |
Metadata for discovery and the dashboard. |
Step 4 — Load the new trigger
The scaffold command already refreshed the registry. Restart aspire start (or let it hot-reload)
so the triggers processor loads daily-resync-schedule.
Verify your progress
-
Confirm the config type-checks with the new concurrency and provider settings:
Shelldeno task check -
Confirm the cron schedule registered. For fast feedback, temporarily set its
cronto*/2 * * * *(every two minutes), regenerate, restart Aspire, wait, and read the executions feed:Shellns-workers executions --limit=10 --jsonAn
import-productsexecution appears on the cron cadence with no file dropped: the schedule, not a file event, enqueued it. -
Set the cron back to
0 6 * * *when you are done testing.
- [ ]
config/official-plugins/mod.tssetsconcurrencyandqueueProvider. - [ ]
deno task checkis clean. - [ ]
daily-resync-scheduleis registered (afternetscript generate plugins+ Aspire restart). - [ ] A scheduled
import-productsexecution appears on the cron cadence with no file dropped. - [ ] The workers background resource shows
WORKERS_CONCURRENCYin the Aspire dashboard, and you changed it only to override the declared default.
What you built
A workers config that names a queue provider and a worker concurrency, so a SAP backfill burst
drains with overlapping I/O, and a defineScheduledTrigger cron that enqueues the nightly
re-sync. The sync now absorbs bursts and runs its recurring work unattended. Next, you run the
whole pipeline under Aspire.