Skip to content

Commit 1230871

Browse files
committed
Added runs to the batch.retrieve call/API
1 parent efc83c9 commit 1230871

File tree

4 files changed

+60
-7
lines changed

4 files changed

+60
-7
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { json } from "@remix-run/server-runtime";
2+
import { z } from "zod";
3+
import { $replica } from "~/db.server";
4+
import { createLoaderApiRoute } from "~/services/routeBuilders/apiBuilder.server";
5+
6+
const ParamsSchema = z.object({
7+
batchId: z.string(),
8+
});
9+
10+
export const loader = createLoaderApiRoute(
11+
{
12+
params: ParamsSchema,
13+
allowJWT: true,
14+
corsStrategy: "all",
15+
findResource: (params, auth) => {
16+
return $replica.batchTaskRun.findFirst({
17+
where: {
18+
friendlyId: params.batchId,
19+
runtimeEnvironmentId: auth.environment.id,
20+
},
21+
});
22+
},
23+
authorization: {
24+
action: "read",
25+
resource: (batch) => ({ batch: batch.friendlyId }),
26+
superScopes: ["read:runs", "read:all", "admin"],
27+
},
28+
},
29+
async ({ resource: batch }) => {
30+
return json({
31+
id: batch.friendlyId,
32+
status: batch.status,
33+
idempotencyKey: batch.idempotencyKey ?? undefined,
34+
createdAt: batch.createdAt,
35+
updatedAt: batch.updatedAt,
36+
runCount: batch.runCount,
37+
runs: batch.runIds,
38+
});
39+
}
40+
);

packages/core/src/v3/apiClient/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import { generateJWT } from "../jwt.js";
44
import {
55
AddTagsRequestBody,
66
BatchTaskRunExecutionResult,
7-
BatchTriggerTaskV2RequestBody,
8-
BatchTriggerTaskV2Response,
97
BatchTriggerTaskV3RequestBody,
108
BatchTriggerTaskV3Response,
119
CanceledRunResponse,
@@ -20,7 +18,7 @@ import {
2018
ListScheduleOptions,
2119
ReplayRunResponse,
2220
RescheduleRunRequestBody,
23-
RetrieveBatchResponse,
21+
RetrieveBatchV2Response,
2422
RetrieveRunResponse,
2523
ScheduleObject,
2624
TaskRunExecutionResult,
@@ -44,9 +42,9 @@ import {
4442
} from "./core.js";
4543
import { ApiError } from "./errors.js";
4644
import {
45+
AnyRealtimeRun,
4746
AnyRunShape,
4847
RealtimeRun,
49-
AnyRealtimeRun,
5048
RunShape,
5149
RunStreamCallback,
5250
RunSubscription,
@@ -97,10 +95,10 @@ const DEFAULT_ZOD_FETCH_OPTIONS: ZodFetchOptions = {
9795

9896
export { isRequestOptions };
9997
export type {
98+
AnyRealtimeRun,
10099
AnyRunShape,
101100
ApiRequestOptions,
102101
RealtimeRun,
103-
AnyRealtimeRun,
104102
RunShape,
105103
RunStreamCallback,
106104
RunSubscription,
@@ -675,8 +673,8 @@ export class ApiClient {
675673

676674
retrieveBatch(batchId: string, requestOptions?: ZodFetchOptions) {
677675
return zodfetch(
678-
RetrieveBatchResponse,
679-
`${this.baseUrl}/api/v1/batches/${batchId}`,
676+
RetrieveBatchV2Response,
677+
`${this.baseUrl}/api/v2/batches/${batchId}`,
680678
{
681679
method: "GET",
682680
headers: this.#getHeaders(false),

packages/core/src/v3/schemas/api.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,18 @@ export const RetrieveBatchResponse = z.object({
819819

820820
export type RetrieveBatchResponse = z.infer<typeof RetrieveBatchResponse>;
821821

822+
export const RetrieveBatchV2Response = z.object({
823+
id: z.string(),
824+
status: BatchStatus,
825+
idempotencyKey: z.string().optional(),
826+
createdAt: z.coerce.date(),
827+
updatedAt: z.coerce.date(),
828+
runCount: z.number(),
829+
runs: z.array(z.string()),
830+
});
831+
832+
export type RetrieveBatchV2Response = z.infer<typeof RetrieveBatchV2Response>;
833+
822834
export const SubscribeRealtimeStreamChunkRawShape = z.object({
823835
id: z.string(),
824836
runId: z.string(),

references/hello-world/src/trigger/idempotency.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ export const idempotencyBatch = task({
141141
);
142142
logger.log("Batch 1", { batch1 });
143143

144+
const b = await batch.retrieve(batch1.id);
145+
logger.log("Batch retrieve", { ...b });
146+
144147
const batch2 = await childTask.batchTriggerAndWait(
145148
[
146149
{

0 commit comments

Comments
 (0)