Has any one recently encountered this? I have been stuck with this for so long and it is so frustrating.
I am right now doing this: Create a task, and execute onTaskDispatched at the schedule time which just logs in the consol for now.
I don't get the permission_denied error, so my service account has all t he permission. I only get INVALID_ARGUMENT
This is my code snippet:
// [NOTIFICATION TASK CREATION START]
const scheduledTime = notification.scheduledTime.toDate();
const now = new Date();
// Use Cloud Tasks
const client = await getCloudTasksClient();
const parent = client.queuePath(PROJECT_ID, LOCATION, QUEUE_NAME);
const url = `https://${LOCATION}-${PROJECT_ID}.cloudfunctions.net/processScheduledNotification`;
// Calculate schedule time
const date_diff_in_seconds = (scheduledTime.getTime() - now.getTime()) / 1000;
const MAX_SCHEDULE_LIMIT = 30 * 24 * 60 * 60; // 30 days in seconds
let scheduledSeconds;
// If scheduled time is in the past or very near future (< 5 mins), schedule for 30 seconds from now
// Otherwise, schedule for the calculated time, capped at MAX_SCHEDULE_LIMIT
if (date_diff_in_seconds <= 0 || date_diff_in_seconds < 300) {
scheduledSeconds = Math.floor(Date.now() / 1000) + 30;
} else {
scheduledSeconds = Math.floor(Math.min(date_diff_in_seconds, MAX_SCHEDULE_LIMIT) + Date.now() / 1000);
}
const payload = JSON.stringify({ notificationId: notification.id })
const body = Buffer.from(payload).toString('base64')
// Create the task payload - send only the notification ID
const task = {
httpRequest: {
httpMethod: 'POST',
url,
headers: {
'Content-Type': 'application/json',
},
// Send only the notification ID in the body
body:body,
oidcToken: {
serviceAccountEmail: `{PROJECT-NUMBER}[email protected]`, // Use PROJECT_ID variable
audience: url // To my service function below
}
},
scheduleTime: {
seconds: scheduledSeconds,
nanos: 0
}
};
const [response] = await client.createTask({ parent, task });
/**
* Cloud Task handler for processing scheduled notifications
*/
export const processScheduledNotification = onTaskDispatched({
retryConfig: {
maxAttempts: 5,
minBackoffSeconds: 30,
},
queueName: 'notification-queue',
}, async (data) => {
console.log('⏲️ Received Cloud Task for scheduled notification');
console.log('Received req object:', data);
});
What am I doing wrong? Any pointers? Whats up with gprc as well? i think its correctly set up. Please help.