Eben Oasis
Eben Oasis

Reputation: 187

Cloud Tasks does not updating from firestore timestamp

I'm trying to write a code for Cloud Tasks. In my task I need to involk a timestamp from Firestore collection with the field TimestampDeparture. The "TimestampDeparture" is a future time. I want that when the now = admin.firestore.Timestamp.now(); gets to the "TimestampDeparture", the field "PostStatus" should be updated to "Started".

Firestore database

This is what the scheduledTask function returns in the console logs when a post is made.

scheduledTask logs

The scheduledTask code

const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
const database = admin.firestore();

exports.scheduledTask = functions.firestore.document("ADSGHANA/{UID}")
    .onCreate(async (snapshot, context) => {
      const data = snapshot.data();
      const {UID, TimestampDeparture} = data;
      const now = admin.firestore.Timestamp.now();
      if (now == TimestampDeparture) {
        console.log(`Post with UID ${UID} should have already started.`);
        return null;
      }
      const project = JSON.parse(process.env.FIREBASE_CONFIG).projectId;
      const location = "us-central1";
      const queue = "scheduleAds";
      const task = {
        httpRequest: {
          httpMethod: "POST",
          url: `https://${location}/${project}.cloudfunctions.net/taskCallback?UID=${UID}`,
          body: Buffer.from(JSON.stringify({UID})).toString("base64"),
          headers: {
            "Content-Type": "application/json",
          },
        },
        scheduleTime: TimestampDeparture,

      };
      const {CloudTasksClient} = require("@google-cloud/tasks");
      const client = new CloudTasksClient();
      const parent = client.queuePath(project, location, queue);
      const [response] = await client.createTask({parent, task});
      console.log(`Created task ${response.name}`);
      return null;
    });

I however don't see see any logs in the taskCallback functions when a post is made even after the "TimestampDeparture" elapses. And also the "PostStatus" in the Firetstore database does not update when the "TimestampDeparture" elapses.

I only see the functions created.

taskCallback logs

The taskCallback code

exports.taskCallback = functions.https
    .onRequest(async (req, res) => {
      const {UID} = req.body;
      const docRef = database.collection("ADSGHANA").doc(UID);
      const doc = await docRef.get();
      if (!doc.exists) {
        console.log(`Document with UID ${UID} does not exist.`);
        res.status(404).send("Document not found.");
        return;
      }

      const {TimestampDeparture, PostStatus} = doc.data();
      const now = admin.firestore.Timestamp.now();
      if (now == TimestampDeparture && PostStatus !== "Cancelled") {
        await docRef.update({PostStatus: "Started"});
        console.log(`Post with UID ${UID} has been started.`);
        res.status(200).send("Post started successfully.");
      } else {
        console.log(`Post with UID ${UID} is not ready to start.`);
        res.status(200).send("Post not ready to start yet.");
      }
    });

Can you please help me with the code. Every help will be much appreciated. Thank you.

Upvotes: 0

Views: 50

Answers (0)

Related Questions