defer-run / defer.client

Zero infrastructure Node.js background jobs

Home Page:https://www.defer.run/docs/introduction

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Getting execution result back from defer

EricDotSmith opened this issue · comments

I'm using nextjs and trpc. I like that defer can take my expensive api call to openai and execute it for me, but I'm running into the issue where when that result is ready, how do I let my nextjs server know about it right away?

Hi @EricDotSmith,

You can use the getExecution() from @defer/client, as showcased here:

https://github.com/defer-run/defer-openai-github-profile/blob/33c5d454dae4d37dd233fe2faf160a80445bc78b/src/app/api/githubProfile/%5BusernameOrExecId%5D/route.ts#L20

Yeah that function will do the trick for getting the result. I guess my main concern is updating my database with the result of the execution right when it's available. As of right now I'm thinking of hacking in a communication between the defer function and a NextJS serverless function, using JWTs the pass the data. The NextJS serverless functions role would be just to update the database with finished job results. That way I don't need to expose my db connection string to defer. What are your thoughts on this approach?

As an update I ended up just adding my databases connection string as an environment variable and used prisma to store the result to my db immediately when it's available. All works well 💪 Defer is a really awesome service, great job!

P.S: I also experimented with sending an encrypted JWT to a NextJS serverless function which worked, but I prefer the convenience and efficiency of using prisma directly in a defer function.