Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

201
Views
How to stop node.js functions/loop via request

I'm running on a fastify server and when I send a request, I get a ton of data from mongodb and start a for loop to process each item. This can take ~ 30 minutes. Each item is "processed" by sending to ffmpeg, redis pub->sub, and then a socket to the client.

 // Streams controller
exports.renderStreams = async function (req, reply) {
    const streams = await Stream.find({}).sort({ createdAt: -1 })//.limit(5)
    const renderedStreams = renderStreams(this, streams);
    return { success: true, streams: streams.length };
}

// renderStreams
const renderStreams = (fastify, streams = []) => {
    const { redis } = fastify;
    const channel = "streams";
    for (let i = 0; i < streams.length; i++) {
        setTimeout(async () => {
            const stream = streams[i];
            await renderStream(redis, channel, stream);
        }, i * 200)
    }
}

I am wondering in this for loop, how can I either "pause" it or stop it completely (or both?) via another request, maybe when I call /api/streams/stop.

How would this be possible?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use socket.io to do communications with your script while it is still running, so you would create a function to stop the loop and when you get the notification from socket.io you would run it. Documentation link: https://socket.io/docs/v4/

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!