1
votes

I've got a function that create topics and subscriptions when my server boots.

The problem is that I can't create subscription linked to a newly created topic, the Google PubSub server throws a NOT FOUND error.

I've even added a 20 second timeout after the topic creation but nothing changed T_T

export async function init() {
  // promises.topics is an array of promises
  const topics = await Promise.all(promises.topics);
  console.log('YEAH !!! TOPIC IS GOOD !', topics);
  // That works ! And it is created in my remote google PubSub
  await timeout(20000);

  console.log('Lets Check if it exists !!!');

  // promises.getTopics is an array of :
  // getTopics.push(pubSubClient.topic(topic.name).get());
  // That throws a NOT FOUND
  const get = await Promise.all(promises.getTopics);
  // Failes because Topics are not found
  const subscriptions = await Promise.all(promises.subscriptions);
}

When I check in the Google logs, the created topic is created AFTER the subscriptions oO

My Promise array inject first topics Then the subscriptions promises.

When adding a retry function that try again 10 times every 2 second, it don't work either.

It's no even showing in google logs, I think they have a cache ton their API that just return newly processed data without re-trying it.

Do you have an idea ?

Thanks :)

TOPICS : enter image description here

SUBSCRIPTION ERROR :

enter image description here

EDIT :

I Managed to fix the problem by adding a timeout('2000') after pushing the topic promise creation in my promise array.

1
Would you mind posting that last edit as an answer? For future reference. - amport
In fact I will add more info about it. I still have a unedrlying question on it ;) Thanks for the reminder - Luna
Good to know! Let's see which question is it. - amport
Seems that my question is interesting for google staff as well :D - Luna

1 Answers

1
votes

So, when I creates my Promise Array like that :

 async function constructPromises() {
  const topics = [];
  const subscriptions = [];
   for (const topic of pubSubConfig.topics) {      
      subscriptions.push(createTopic(topic.name));

      // Have to add this timeout, otherwise the topic is not found oO
      await timeout(1000);
   }
   for (const topic of pubSubConfig.topics) {
     for (const sub of topic.subscriptions) {        
       subscriptions.push(createSubscription(topic.name, sub));
     }       
   }
   return { topics, subscriptions };
}

I had to add this timeout, otherwise gcloud is not able to find the topics.

I think it's because gcloud send the resolve Promise before treating the request. So when you create the subscription, he's not able to find the topic. But I can't validate this, does anyone knows why ??!!

I wanted to remove this timeout to have a more agnostic implementation. And even more stranger, I find that if you just call the promises in for loop, no need for timeout !

for (const topic of pubSubConfig.topics) {
    const topicResponse = await createTopic(topic.name);
    topics.push(topicResponse);
    for (const sub of topic.subscriptions) {
      const subResponse = await createSubscription(topic.name, sub);
      subscriptions.push(subResponse);
    }
  }
  return { topics, subscriptions };
}

So my problem is solved, but I still have this question in my mind :P