Effective PDF Download and Distribution with Cloudflare Workers

Hey, I am trying to figure out the best way to download a PDF from an external API, save it to an R2 instance, and then send it to all of my users. I am currently using Cloudflare Workers to handle this process, but I'm running into issues with the worker running out of processing time. Would switching to an unbound worker mode help with this issue? Are there any other suggestions or tips for handling this process effectively and efficiently? Any input would be greatly appreciated. Thank you!
1 Reply
Pato
PatoOP•2y ago
Sure, give me a sec its a pretty rough code that used to work with a little amont ouf users, but now its running out of cpu time let me get a snippet of my code This is my hanlder function:
const accounts = await fetchAccounts();
const accounts_ids = accounts.map((account: { id: string; }) => account.id);


// for all account id send email
for (let i = 0; i < accounts_ids.length; i++) {
await sendAllClientsAccountStatement(accounts_ids[i]);
}
const accounts = await fetchAccounts();
const accounts_ids = accounts.map((account: { id: string; }) => account.id);


// for all account id send email
for (let i = 0; i < accounts_ids.length; i++) {
await sendAllClientsAccountStatement(accounts_ids[i]);
}
and this is my sendAllClientsAccountStatement function
async function sendAllClientsAccountStatement(account_id: string){

const statement = await getAccountStatementAll(account_id);

const accountData = await getAccountDataAll(account_id);
const email = accountData.contact.email_address;
const name = accountData.identity.given_name + ' ' + accountData.identity.family_name
const account_number = accountData.account_number;



if(statement != null){
const message = '';

await sendEmail(email, '[email protected]', 'Test', message)
console.log("Account statement sent correctly to " + email);
}

}
async function sendAllClientsAccountStatement(account_id: string){

const statement = await getAccountStatementAll(account_id);

const accountData = await getAccountDataAll(account_id);
const email = accountData.contact.email_address;
const name = accountData.identity.given_name + ' ' + accountData.identity.family_name
const account_number = accountData.account_number;



if(statement != null){
const message = '';

await sendEmail(email, '[email protected]', 'Test', message)
console.log("Account statement sent correctly to " + email);
}

}
It now it's pretty unefficient 🤣 yep, I need to fetch a lot of stuff Could you roughly explain a basic pipeline with queues? I see, sounds like a prefect use case for my problem thanks~ I've been a little hesitant to use queues since they are still in beta but I;ll give them a try Nice! Thanks a lot!
Want results from more Discord servers?
Add your server