Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

202
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda