@@ -7,8 +7,9 @@ use axum::{
77use axum_accept:: AcceptExtractor ;
88use concepts:: {
99 ExecutionId , FinishedExecutionError , FunctionFqn , SupportedFunctionReturnValue ,
10+ prefixed_ulid:: DelayId ,
1011 storage:: {
11- CancelOutcome , DbErrorRead , DbErrorWrite , DbErrorWriteNonRetriable , DbPool ,
12+ self , CancelOutcome , DbErrorRead , DbErrorWrite , DbErrorWriteNonRetriable , DbPool ,
1213 ExecutionEventInner ,
1314 } ,
1415 time:: { ClockFn as _, Now } ,
@@ -40,6 +41,7 @@ pub(crate) fn app_router(state: WebApiState) -> Router {
4041
4142fn v1_router ( ) -> Router < Arc < WebApiState > > {
4243 Router :: new ( )
44+ . route ( "/delays/{delay-id}/cancel" , routing:: put ( delay_cancel) )
4345 . route ( "/execution-id" , routing:: get ( execution_id_generate) )
4446 . route (
4547 "/executions/{execution-id}/cancel" ,
@@ -62,6 +64,19 @@ async fn execution_id_generate(_: State<Arc<WebApiState>>, accept: AcceptHeader)
6264 }
6365}
6466
67+ async fn delay_cancel (
68+ Path ( delay_id) : Path < DelayId > ,
69+ state : State < Arc < WebApiState > > ,
70+ accept : AcceptHeader ,
71+ ) -> Result < Response , HttpResponse > {
72+ let conn = state. db_pool . connection ( ) ;
73+ let executed_at = Now . now ( ) ;
74+ let outcome = storage:: cancel_delay ( conn. as_ref ( ) , delay_id, executed_at)
75+ . await
76+ . map_err ( |e| ErrorWrapper ( e, accept) ) ?;
77+ Ok ( HttpResponse :: from_cancel_outcome ( outcome, accept) . into_response ( ) )
78+ }
79+
6580async fn execution_cancel (
6681 Path ( execution_id) : Path < ExecutionId > ,
6782 state : State < Arc < WebApiState > > ,
@@ -86,19 +101,7 @@ async fn execution_cancel(
86101 . cancel ( conn. as_ref ( ) , & execution_id, executed_at)
87102 . await
88103 . map_err ( |e| ErrorWrapper ( e, accept) ) ?;
89- Ok ( match outcome {
90- CancelOutcome :: Cancelled => HttpResponse {
91- status : StatusCode :: OK ,
92- message : "cancelled" . to_string ( ) ,
93- accept,
94- } ,
95- CancelOutcome :: AlreadyFinished => HttpResponse {
96- status : StatusCode :: CONFLICT ,
97- message : "already finished" . to_string ( ) ,
98- accept,
99- } ,
100- }
101- . into_response ( ) )
104+ Ok ( HttpResponse :: from_cancel_outcome ( outcome, accept) . into_response ( ) )
102105}
103106
104107async fn execution_status_get (
@@ -380,6 +383,22 @@ struct HttpResponse {
380383 message : String ,
381384 accept : AcceptHeader ,
382385}
386+ impl HttpResponse {
387+ fn from_cancel_outcome ( outcome : CancelOutcome , accept : AcceptHeader ) -> Self {
388+ match outcome {
389+ CancelOutcome :: Cancelled => HttpResponse {
390+ status : StatusCode :: OK ,
391+ message : "cancelled" . to_string ( ) ,
392+ accept,
393+ } ,
394+ CancelOutcome :: AlreadyFinished => HttpResponse {
395+ status : StatusCode :: CONFLICT ,
396+ message : "already finished" . to_string ( ) ,
397+ accept,
398+ } ,
399+ }
400+ }
401+ }
383402
384403impl IntoResponse for HttpResponse {
385404 fn into_response ( self ) -> Response {
0 commit comments