Skip to content

Commit

Permalink
Add route handlers for cancel and terminate
Browse files Browse the repository at this point in the history
  • Loading branch information
adhityamamallan committed Jan 14, 2025
1 parent 4796389 commit 450b92b
Show file tree
Hide file tree
Showing 8 changed files with 212 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { type NextRequest } from 'next/server';

import { cancelWorkflow } from '@/route-handlers/cancel-workflow/cancel-workflow';
import { type RouteParams } from '@/route-handlers/cancel-workflow/cancel-workflow.types';
import { routeHandlerWithMiddlewares } from '@/utils/route-handlers-middleware';
import routeHandlersDefaultMiddlewares from '@/utils/route-handlers-middleware/config/route-handlers-default-middlewares.config';

export async function POST(
request: NextRequest,
options: { params: RouteParams }
) {
return routeHandlerWithMiddlewares(
cancelWorkflow,
request,
options,
routeHandlersDefaultMiddlewares
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { type NextRequest } from 'next/server';

import { terminateWorkflow } from '@/route-handlers/terminate-workflow/terminate-workflow';
import { type RouteParams } from '@/route-handlers/terminate-workflow/terminate-workflow.types';
import { routeHandlerWithMiddlewares } from '@/utils/route-handlers-middleware';
import routeHandlersDefaultMiddlewares from '@/utils/route-handlers-middleware/config/route-handlers-default-middlewares.config';

export async function POST(
request: NextRequest,
options: { params: RouteParams }
) {
return routeHandlerWithMiddlewares(
terminateWorkflow,
request,
options,
routeHandlersDefaultMiddlewares
);
}
61 changes: 61 additions & 0 deletions src/route-handlers/cancel-workflow/cancel-workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { type NextRequest, NextResponse } from 'next/server';

import decodeUrlParams from '@/utils/decode-url-params';
import { getHTTPStatusCode, GRPCError } from '@/utils/grpc/grpc-error';
import logger, { type RouteHandlerErrorPayload } from '@/utils/logger';

import {
type CancelWorkflowResponse,
type Context,
type RequestParams,
} from './cancel-workflow.types';
import cancelWorkflowRequestBodySchema from './schemas/cancel-workflow-request-body-schema';

export async function cancelWorkflow(
request: NextRequest,
requestParams: RequestParams,
ctx: Context
) {
const requestBody = await request.json();
const { data, error } =
cancelWorkflowRequestBodySchema.safeParse(requestBody);

if (error) {
return NextResponse.json(
{
message: 'Invalid values provided for workflow cancellation',
validationErrors: error.errors,
},
{ status: 400 }
);
}

const decodedParams = decodeUrlParams(requestParams.params);

try {
await ctx.grpcClusterMethods.requestCancelWorkflow({

Check failure on line 36 in src/route-handlers/cancel-workflow/cancel-workflow.ts

View workflow job for this annotation

GitHub Actions / build

Property 'requestCancelWorkflow' does not exist on type 'GRPCClusterMethods'.
domain: decodedParams.domain,
workflowExecution: {
workflowId: decodedParams.workflowId,
runId: decodedParams.runId,
},
cause: data.cause,
});

return NextResponse.json({} satisfies CancelWorkflowResponse);
} catch (e) {
logger.error<RouteHandlerErrorPayload>(
{ requestParams: decodedParams, cause: e },
'Error cancelling workflow'
);

return NextResponse.json(
{
message:
e instanceof GRPCError ? e.message : 'Error cancelling workflow',
cause: e,
},
{ status: getHTTPStatusCode(e) }
);
}
}
17 changes: 17 additions & 0 deletions src/route-handlers/cancel-workflow/cancel-workflow.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { type RequestCancelWorkflowExecutionResponse } from '@/__generated__/proto-ts/uber/cadence/api/v1/RequestCancelWorkflowExecutionResponse';
import { type DefaultMiddlewaresContext } from '@/utils/route-handlers-middleware';

export type RouteParams = {
domain: string;
cluster: string;
workflowId: string;
runId: string;
};

export type RequestParams = {
params: RouteParams;
};

export type CancelWorkflowResponse = RequestCancelWorkflowExecutionResponse;

export type Context = DefaultMiddlewaresContext;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from 'zod';

const cancelWorkflowRequestBodySchema = z.object({
cause: z
.string()
.optional()
.default('Requesting workflow cancellation from cadence-web UI'),
});

export default cancelWorkflowRequestBodySchema;
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from 'zod';

const terminateWorkflowRequestBodySchema = z.object({
reason: z
.string()
.optional()
.default('Terminating workflow from cadence-web UI'),
});

export default terminateWorkflowRequestBodySchema;
61 changes: 61 additions & 0 deletions src/route-handlers/terminate-workflow/terminate-workflow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { type NextRequest, NextResponse } from 'next/server';

import decodeUrlParams from '@/utils/decode-url-params';
import { getHTTPStatusCode, GRPCError } from '@/utils/grpc/grpc-error';
import logger, { type RouteHandlerErrorPayload } from '@/utils/logger';

import terminateWorkflowRequestBodySchema from './schemas/terminate-workflow-request-body-schema';
import {
type TerminateWorkflowResponse,
type Context,
type RequestParams,
} from './terminate-workflow.types';

export async function terminateWorkflow(
request: NextRequest,
requestParams: RequestParams,
ctx: Context
) {
const requestBody = await request.json();
const { data, error } =
terminateWorkflowRequestBodySchema.safeParse(requestBody);

if (error) {
return NextResponse.json(
{
message: 'Invalid values provided for workflow termination',
validationErrors: error.errors,
},
{ status: 400 }
);
}

const decodedParams = decodeUrlParams(requestParams.params);

try {
await ctx.grpcClusterMethods.terminateWorkflow({
domain: decodedParams.domain,
workflowExecution: {
workflowId: decodedParams.workflowId,
runId: decodedParams.runId,
},
reason: data.reason,
});

return NextResponse.json({} satisfies TerminateWorkflowResponse);
} catch (e) {
logger.error<RouteHandlerErrorPayload>(
{ requestParams: decodedParams, cause: e },
'Error terminating workflow'
);

return NextResponse.json(
{
message:
e instanceof GRPCError ? e.message : 'Error terminating workflow',
cause: e,
},
{ status: getHTTPStatusCode(e) }
);
}
}
17 changes: 17 additions & 0 deletions src/route-handlers/terminate-workflow/terminate-workflow.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { type TerminateWorkflowExecutionResponse } from '@/__generated__/proto-ts/uber/cadence/api/v1/TerminateWorkflowExecutionResponse';
import { type DefaultMiddlewaresContext } from '@/utils/route-handlers-middleware';

export type RouteParams = {
domain: string;
cluster: string;
workflowId: string;
runId: string;
};

export type RequestParams = {
params: RouteParams;
};

export type TerminateWorkflowResponse = TerminateWorkflowExecutionResponse;

export type Context = DefaultMiddlewaresContext;

0 comments on commit 450b92b

Please sign in to comment.