Skip to content

Commit

Permalink
init job agent helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Nov 2, 2024
1 parent 22f68a9 commit cdf03b2
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions packages/node-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,67 @@ export class TargetProvider {
}
}

export class JobAgent {
constructor(
private options: { type: string; workspaceId: string; name: string },
private client: ReturnType<typeof createClient>,
) {}

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<typeof createClient>,
) {}

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<T>(arr: T[], iteratee: (item: T) => any): T[] {
const seen = new Map<any, boolean>();
return arr.filter((item) => {
Expand Down

0 comments on commit cdf03b2

Please sign in to comment.