Hi, based on your video tutorial like your code below, I'm getting "expired" right after 1 "processing" but never "complete". what could be the reasons?
thanks in advance!
Usage in home:
const generatedobjectUrl = await startEventHandler(objectUrls);
async function startEventHandler(objectUrls) {
const requestId = uuidv4();
try{
const response = await startTask(requestId, objectUrls);
console.log("response", response);
} catch (error) {
console.log(error);
}
let poll;
poll = setInterval(async () => {
const response = await getData(requestId);
console.log("response", response);
if (response.status === 'complete') {
// TODO add to my object
clearInterval(poll)
} else if (response.status === 'expired') {
//handle expiration
clearInterval(poll)
}
}, 3000)
}
Server.web.js:
import { Permissions, webMethod } from "wix-web-module";
import {analyzeobjectsAndGeneratePrompt} from 'backend/create_objects'
let cache = {};
export async function generateobject(objectUrls) {
console.log("converted urls: ", objectUrls)
if(objectUrls.length > 0) {
const generatedobjectUrl = await analyzeobjectsAndGeneratePrompt(objectUrls);
return generatedobjectUrl;
}
}
export const startTask = webMethod(
Permissions.Anyone,
async (requestId, objectUrls) => {
cache[requestId] = 'processing';
expire(30000, requestId);
let data = await generateobject(objectUrls);
if (cache[requestId]) cache[requestId] = data;
return {
status:'complete',
data,
}
}
);
export const getData = webMethod(
Permissions.Anyone,
async(requestId) => {
if(!cache[requestId]) return {status: 'expired'};
if (cache[requestId] === 'processing') return {status: 'processing'}
return {
status: 'complete',
data: cache[requestId],
}
}
)
async function expire(time, requestId) {
setTimeout(() => {
delete cache[requestId];
}, time)
}
// const delay = (time) => new Promise((resolve) => setTimeout(() => {
// resolve();
// }, time))
Hi Jacky, Thanks for your question. Have you tried increasing the time until expiration? It is currently set to 30 seconds which may be less than the time it takes for 'generateObject' to run. Just putting the link to the tutorial here for other readers: https://www.youtube.com/watch?v=dUZzp_jv8fI Best, Eitan