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

117
Visualizações
Error Using Firebase Emulators PubSub for Timed Function

I have a timed Cloud Function which I am trying to test in the Firebase Emulator. To do this, I am using the solution described here (How to invoke firebase Schedule functions locally using pubsub emulator) which triggers the scheduled function using a Pub/Sub message.

This solution successfully triggers the function, but it seems to be repeating its calls more than expected. For example, when I run the below code, it should be calling the scheduled function every minute: however, it runs the code once after a minute, then twice after 2 minutes (so it has run 3 times total), then three times after 3 times (so it has run 6 times total), and continues to increase its calls from there, which suggests setInterval() is being called more than once (I have setInterval() in my Firebase index.js file). Is there anyway to fix this? Thank you very much.

const pubsub = new PubSub({
  apiEndpoint: 'localhost:8085'
});

setInterval(() => {
  const SCHEDULED_FUNCTION_TOPIC = 'firebase-schedule-yourFunctionName';
  console.log(`Trigger sheduled function via PubSub topic: ${SCHEDULED_FUNCTION_TOPIC}`);
  const msg = await pubsub.topic(SCHEDULED_FUNCTION_TOPIC).publishJSON({
    foo: 'bar',
  }, { attr1: 'value1' });
}, 1 * 60 * 1000); // every 1 minute
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I replicated your case but there's no problem on my end. Here's my complete source code:

index.js:

const admin = require("firebase-admin");
const functions = require("firebase-functions");
const { PubSub } = require("@google-cloud/pubsub");

const pubsub = new PubSub();
admin.initializeApp();

const date_ob = new Date();


exports.pubsubScheduled = functions.pubsub.schedule("every mon 07:00").onRun((context) => {
    console.log("========== PUBSUB FUNCTION ==========");
    console.log("Time", new Date());
    return true;
});

trigger.js:

const admin = require("firebase-admin");
const functions = require("firebase-functions");
const { PubSub } = require("@google-cloud/pubsub");

admin.initializeApp();

var date_ob = new Date();
  
const pubsub = new PubSub({
 apiEndpoint: 'localhost:8085' // Change it to your PubSub emulator address and port
 });

  
    setInterval(() => {
    const SCHEDULED_FUNCTION_TOPIC = 'firebase-schedule-pubsubScheduled';
    console.log(`Trigger sheduled function via PubSub topic: ${SCHEDULED_FUNCTION_TOPIC} at ${new Date()}`);
    const msg =  pubsub.topic(SCHEDULED_FUNCTION_TOPIC).publishJSON({
      foo: 'bar',
    }, { attr1: 'value1' });
  }, 1 * 60 * 1000); // every 1 minute

After running the emulator then trigger the code(trigger.js) there's no problem and all is working well, no multi-output appeared. You can edit your question and add more details how we can reproduce your issue.

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