-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add route handlers for cancel and terminate
- Loading branch information
1 parent
4796389
commit 450b92b
Showing
8 changed files
with
212 additions
and
0 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
...p/api/domains/[domain]/[cluster]/workflows/[workflowId]/[runId]/(actions)/cancel/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
18 changes: 18 additions & 0 deletions
18
...pi/domains/[domain]/[cluster]/workflows/[workflowId]/[runId]/(actions)/terminate/route.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
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
17
src/route-handlers/cancel-workflow/cancel-workflow.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
10 changes: 10 additions & 0 deletions
10
src/route-handlers/cancel-workflow/schemas/cancel-workflow-request-body-schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
10 changes: 10 additions & 0 deletions
10
src/route-handlers/terminate-workflow/schemas/terminate-workflow-request-body-schema.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
61
src/route-handlers/terminate-workflow/terminate-workflow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
src/route-handlers/terminate-workflow/terminate-workflow.types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |