From cdf03b21542e8e1f1d0a61c6fdb5898d9751968c Mon Sep 17 00:00:00 2001 From: Justin Brooks Date: Sat, 2 Nov 2024 14:26:21 -0400 Subject: [PATCH] init job agent helper --- packages/node-sdk/src/index.ts | 61 ++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/packages/node-sdk/src/index.ts b/packages/node-sdk/src/index.ts index 8eb2deb0..65599df6 100644 --- a/packages/node-sdk/src/index.ts +++ b/packages/node-sdk/src/index.ts @@ -72,6 +72,67 @@ export class TargetProvider { } } +export class JobAgent { + constructor( + private options: { type: string; workspaceId: string; name: string }, + private client: ReturnType, + ) {} + + private agent: + | operations["updateJobAgent"]["responses"]["200"]["content"]["application/json"] + | null = null; + + async get() { + if (this.agent != null) { + return this.agent; + } + + const { data } = await this.client.PATCH("/v1/job-agents/name", { + body: this.options, + }); + this.agent = data; + return this.agent; + } + + async next() { + const { data } = await this.client.GET( + "/v1/job-agents/{agentId}/queue/next", + { params: { path: { agentId: this.agent.id } } }, + ); + return data.jobs.map((job) => new Job(job, this.client)) ?? []; + } +} + +class Job { + constructor( + private job: { id: string }, + private client: ReturnType, + ) {} + + acknowledge() { + return this.client.POST("/v1/jobs/{jobId}/acknowledge", { + params: { path: { jobId: this.job.id } }, + }); + } + + get() { + return this.client + .GET("/v1/jobs/{jobId}", { + params: { path: { jobId: this.job.id } }, + }) + .then(({ data }) => data); + } + + update( + update: operations["updateJob"]["requestBody"]["content"]["application/json"], + ) { + return this.client.PATCH("/v1/jobs/{jobId}", { + params: { path: { jobId: this.job.id } }, + body: update, + }); + } +} + function uniqBy(arr: T[], iteratee: (item: T) => any): T[] { const seen = new Map(); return arr.filter((item) => {