Skip to content

Commit 3017cfc

Browse files
committed
server: add endpoint for a factory to fail a worker
1 parent 189fa8f commit 3017cfc

3 files changed

Lines changed: 95 additions & 0 deletions

File tree

client/openapi.json

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,42 @@
922922
}
923923
}
924924
},
925+
"/0/factory/worker/{worker}/fail": {
926+
"post": {
927+
"operationId": "factory_worker_fail",
928+
"parameters": [
929+
{
930+
"in": "path",
931+
"name": "worker",
932+
"required": true,
933+
"schema": {
934+
"type": "string"
935+
}
936+
}
937+
],
938+
"requestBody": {
939+
"content": {
940+
"application/json": {
941+
"schema": {
942+
"$ref": "#/components/schemas/FactoryWorkerFail"
943+
}
944+
}
945+
},
946+
"required": true
947+
},
948+
"responses": {
949+
"204": {
950+
"description": "resource updated"
951+
},
952+
"4XX": {
953+
"$ref": "#/components/responses/Error"
954+
},
955+
"5XX": {
956+
"$ref": "#/components/responses/Error"
957+
}
958+
}
959+
}
960+
},
925961
"/0/factory/worker/{worker}/flush": {
926962
"post": {
927963
"operationId": "factory_worker_flush",
@@ -2943,6 +2979,17 @@
29432979
"target"
29442980
]
29452981
},
2982+
"FactoryWorkerFail": {
2983+
"type": "object",
2984+
"properties": {
2985+
"reason": {
2986+
"type": "string"
2987+
}
2988+
},
2989+
"required": [
2990+
"reason"
2991+
]
2992+
},
29462993
"FactoryWorkerResult": {
29472994
"type": "object",
29482995
"properties": {

server/src/api/factory.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,53 @@ pub(crate) async fn factory_worker_flush(
270270
Ok(HttpResponseUpdatedNoContent())
271271
}
272272

273+
#[derive(Deserialize, Serialize, JsonSchema)]
274+
pub(crate) struct FactoryWorkerFail {
275+
reason: String,
276+
}
277+
278+
#[endpoint {
279+
method = POST,
280+
path = "/0/factory/worker/{worker}/fail",
281+
}]
282+
pub(crate) async fn factory_worker_fail(
283+
rqctx: RequestContext<Arc<Central>>,
284+
path: TypedPath<WorkerPath>,
285+
body: TypedBody<FactoryWorkerFail>,
286+
) -> DSResult<HttpResponseUpdatedNoContent> {
287+
let c = rqctx.context();
288+
let log = &rqctx.log;
289+
290+
let worker_id = path.into_inner().worker()?;
291+
let reason = body.into_inner().reason;
292+
293+
let factory = c.require_factory(log, &rqctx.request).await?;
294+
let worker = c.db.worker(worker_id).or_500()?;
295+
factory.owns(log, &worker)?;
296+
297+
warn!(
298+
log, "worker failed!";
299+
"id" => worker.id.to_string(), "reason" => &reason,
300+
);
301+
302+
/*
303+
* Record in the database that the worker has failed. This routine will
304+
* take care of reporting failure in any assigned jobs, marking the worker
305+
* as held, etc.
306+
*/
307+
let failed_jobs = c.db.worker_mark_failed(worker.id, &reason).or_500()?;
308+
if !failed_jobs.is_empty() {
309+
let jobs = failed_jobs
310+
.into_iter()
311+
.map(|j| j.to_string())
312+
.collect::<Vec<_>>()
313+
.join(", ");
314+
warn!(log, "worker {} failing caused jobs {jobs} to fail", worker.id);
315+
}
316+
317+
Ok(HttpResponseUpdatedNoContent())
318+
}
319+
273320
#[derive(Debug, Deserialize, JsonSchema)]
274321
pub(crate) struct FactoryWorkerAssociate {
275322
private: String,

server/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,7 @@ async fn main() -> Result<()> {
10111011
ad.register(api::factory::factory_ping)?;
10121012
ad.register(api::factory::factory_worker_create)?;
10131013
ad.register(api::factory::factory_worker_append)?;
1014+
ad.register(api::factory::factory_worker_fail)?;
10141015
ad.register(api::factory::factory_worker_flush)?;
10151016
ad.register(api::factory::factory_worker_associate)?;
10161017
ad.register(api::factory::factory_worker_destroy)?;

0 commit comments

Comments
 (0)