Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda