Run workers from a queue
Some work does not belong in a web request: sending email, resizing images, calling a slow API. One application puts a message on a queue, and a worker takes it off and does the job.
Every cluster has a shared NATS server for this. Each application you connect gets its own account on it. Its queues and subjects are invisible to every other application, even on the same cluster.
Connect an application
Connecting gives the application two variables and redeploys it:
| Variable | Holds |
|---|---|
NATS_URL | The address of the cluster’s NATS server |
NATS_CREDS | The credentials of the application’s account, stored as a secret |
Any NATS client library can use them. Most take the credentials as
a file: write NATS_CREDS to a file at startup and pass its path.
Open the application, then the Messaging tab, and select Connect.
flui app messaging connect my-workerflui app messaging my-workerConnect every application that uses the queue: the one that sends and the one that works. Disconnect removes the variables and the account.
Create a queue
A queue has a name, and takes messages on the subject with that
name and on every subject under it. For a queue named ORDERS:
ORDERS and ORDERS.>.
You choose three things:
- how long a message waits for a worker before it is dropped;
- how many times it is delivered before the queue gives up on it;
- whether the messages given up on are kept apart, so you can look
at them later. They go to a second queue named
ORDERS_DLQ.
Your worker pulls from the queue with a durable consumer that has the same name as the queue, and acknowledges each message when the job is done. A message that is not acknowledged is delivered again.
On the Messaging tab, type a name and select Add queue. The dashboard keeps messages 7 days, delivers each one up to 5 times and keeps the failed ones apart. The table shows, for each queue, the messages waiting, the ones a worker is handling now, and the ones given up on.
flui app messaging queues my-worker --create ORDERS --max-age 168 --max-deliver 5 --dead-letterflui app messaging queues my-workerDeleting a queue deletes every message in it. The dashboard asks
you to confirm; from the CLI it is
flui app messaging queues my-worker --delete ORDERS.
Scale on the queue
A worker can follow its queue instead of its CPU. You set how many waiting messages one replica should handle, and the lowest and highest number of replicas. With a minimum of 0 the worker stops while the queue is empty and starts again when a message arrives, so an idle worker costs nothing.
Under Scale on a queue, pick the queue, the messages per replica and the minimum and maximum, then select Scale. Stop scaling on the queue goes back to the normal scaling.
flui app messaging scale my-worker --stream ORDERS --consumer ORDERS --lag 20 --max 5flui app messaging scale my-worker --off--min defaults to 0.
When a queue keeps growing
Flui reads the queues of every connected application every few minutes. When a queue holds at least 100 messages and has grown at every reading for about a quarter of an hour, the application’s owner gets an alert. It usually means the worker is down, stuck or too small. The alert closes when the queue starts to shrink.