N
Nuxt3mo ago
lazygopher

Single Nitro task instance

I have a nitro task which I run every minute that performs some complex logic. I want to make sure there is only one instance running at a time, e.g if the task is running and a minute is passed by, nuxt will trigger another task, which I need to return immediately as there is another active task. If there are no active task - then it should run
6 Replies
kapa.ai
kapa.ai3mo ago
Hi @lazygopher. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To ensure that only one instance of your Nitro task runs at a time, you need a mechanism to track whether a task is currently running and prevent overlapping executions. While the knowledge sources do not provide a built-in "single instance task" pattern for Nitro, you can implement this logic yourself using a simple in-memory flag or, for distributed/multi-process environments, a shared storage like Redis. In-Memory Lock (Single Process) If your Nuxt/Nitro app runs as a single process (e.g., not clustered or serverless), you can use a module-scoped variable as a lock:
// server/api/my-task.ts
let isRunning = false;

export default defineEventHandler(async (event) => {
if (isRunning) {
// Task is already running, return immediately
return { status: 'busy' }
}
isRunning = true;
try {
// Your complex logic here
await doComplexTask();
return { status: 'done' }
} finally {
isRunning = false;
}
});
// server/api/my-task.ts
let isRunning = false;

export default defineEventHandler(async (event) => {
if (isRunning) {
// Task is already running, return immediately
return { status: 'busy' }
}
isRunning = true;
try {
// Your complex logic here
await doComplexTask();
return { status: 'done' }
} finally {
isRunning = false;
}
});
Distributed Lock (Multi-Process/Cluster/Serverless) If your app runs in multiple processes or across multiple servers, you need a distributed lock. Nitro supports using Redis as a storage backend, which you can use for locking. The knowledge sources show how to configure Redis storage in Nitro, but do not provide a locking implementation. You would need to implement a simple lock using Redis' SETNX command or a library. See how to configure Redis storage in Nitro:
Advanced Usage: Nitro Config & Storage Scheduling the Task You can use a server plugin or an external scheduler (like cron) to trigger the endpoint every minute. The endpoint itself should contain the locking logic as shown above. --- Note:
- In-memory locks only work reliably in single-process environments. - For production, especially with clustering or serverless, use a distributed lock (e.g., Redis). The knowledge sources do not provide a ready-made "single instance task" utility, but the above approach is consistent with Nitro's server API and storage capabilities as documented. If you need more advanced task management, consider using external job queues or schedulers. For more on Nitro server API and storage:
Advanced Usage: Nitro Config & Storage ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@693201794137981049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
lazygopher
lazygopherOP3mo ago
@kapa.ai The docs are not clear about it: https://nitro.build/guide/tasks#concurrency "Each task can have one running instance. Calling a task of same name multiple times in parallel, results in calling it once and all callers will get the same return value." And then right below it "Nitro tasks can be running multiple times and in parallel."
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- You're correct that the Nitro tasks documentation states:
"Each task can have one running instance. Calling a task of same name multiple times in parallel, results in calling it once and all callers will get the same return value."
>
And then right below it: "Nitro tasks can be running multiple times and in parallel."
Based on the knowledge sources provided, there is no explicit clarification or detailed explanation about this apparent contradiction in the official Nuxt or Nitro documentation. The sources do not elaborate on how Nitro tasks handle concurrency beyond the quoted statements, nor do they provide concrete examples or further technical details about task locking or parallel execution semantics. Therefore, I'm sorry, but the knowledge sources do not contain enough information to definitively clarify this aspect of Nitro task concurrency. For the most accurate and up-to-date answer, you may want to consult the official Nitro documentation directly or open an issue for clarification from the maintainers. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@693201794137981049> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
lazygopher
lazygopherOP3mo ago
I have just tested it. By default, nitro tasks runs a single instance. If another one is triggered, it's pending.

Did you find this page helpful?