skip to Main Content

I have got one functions that triggers on db update:

exports.eventAddedTrigger = functions
  .region('europe-west6')
  .firestore
  .document('users/{user_id}/events/{event_id}')
  .onCreate(async (snap, context) => {
    const event = snap.data();

    if (event) {
      const { user_id, event_id } = context.params;
      const queue = getFunctions().taskQueue('enrol');
      const signupDate = DateTime.fromSeconds(event.signupDate.seconds).minus({minutes: 2});
      const now = DateTime.local({zone: 'Europe/Zurich'})
      let scheduleDelaySeconds = Math.floor(signupDate.diff(now, 'seconds').seconds);
      if (scheduleDelaySeconds < 0) {
        scheduleDelaySeconds = 10;
      }
      functions.logger.info(`Scheduling enrollment for ${signupDate.toISO()} in ${scheduleDelaySeconds} seconds`);
      await queue.enqueue(
        { user_id, event_id },
        {
          scheduleDelaySeconds
        }
      )
    }
  });

This function triggers fine, but when it comes to enqueue-ing, I always get the following error

Error: Queue does not exist

regardless of whether I run the function emulated or in production.

The enrol function looks like this:

exports.enrol = functions
  .region('europe-west6')
  .runWith({
    timeoutSeconds: 540,
    memory: '1GB',
  })
  .tasks
  .taskQueue()
  .onDispatch(async (data) => {
    const { user_id, event_id } = data.body;
    await _enrol(user_id, event_id, db);
    functions.logger.info(`Enrolled user ${user_id} to event ${event_id}`);
  });

I have initialised my app correctly to my best knowledge:

initializeApp({
  serviceAccountId: process.env.FIREBASE_SERVICE_ACCOUNT_ID,
});

Do I have to register the queue somewhere else?

2

Answers


  1. I have tried to reproduce this issue and find out you need to initializeApp with the following syntax:

    import {applicationDefault, initializeApp} from "firebase-admin/app";
    initializeApp({
      credential: applicationDefault(),
      databaseURL: "https://<DATABASE_NAME>.firebaseio.com",
    });
    

    Where DATABASE_NAME will be databaseURL from your firebaseConfig in the console.You can get the database by visiting project settings under your apps section.

    Plus you also have to enqueue the functions into queue as shown in the documentation, and make sure you enqueue your task in around 1 minute gap also you haven’t included dispatchDeadlineSeconds in queue.enqueue like the one which is provided in above documentaions.

    I have found the recommended doc which shows initialization of the SDK.

    Login or Signup to reply.
  2. I figured this out for me. If you’re using non default region for your function (i.e. not us-central1) than you need to specify your queue name including your target region.
    The schema is defined here https://github.com/firebase/firebase-admin-node/blob/master/src/utils/index.ts#L293

    So use your enqueue function like this:

    await this.functions 
        .taskQueue<INotification<any>[]>(`locations/${region}/functions/${queueName}`) 
        .enqueue(data);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search