Refactor production initiation logic in DirectorWorker: Update the create_single_production method to return the produced quantity, enhancing the loop's decision-making process. Improve error handling and logging for production failures, ensuring better capacity management and preventing race conditions during production checks.

This commit is contained in:
Torsten Schulz (local)
2026-01-12 16:52:18 +01:00
parent 40df17ee91
commit 1505d3d004

View File

@@ -432,12 +432,32 @@ impl DirectorWorker {
base_plan.running_productions_quantity = running_productions_quantity; base_plan.running_productions_quantity = running_productions_quantity;
// Eine neue Produktion starten (max. 100 Stück) // Eine neue Produktion starten (max. 100 Stück)
if let Err(err) = self.create_single_production(&mut conn, &base_plan) { let produced_quantity = match self.create_single_production(&mut conn, &base_plan) {
Ok(qty) => qty,
Err(err) => {
eprintln!(
"[DirectorWorker] Fehler beim Starten einer Produktion: {err}"
);
break;
}
};
// WICHTIG: Wenn wir die gesamte verfügbare Kapazität verwendet haben, breche ab.
// Sonst könnte die nächste Iteration fälschlicherweise noch Platz sehen, wenn
// die gerade gestartete Produktion noch nicht in running_productions_quantity enthalten ist.
if produced_quantity >= free_capacity {
eprintln!( eprintln!(
"[DirectorWorker] Fehler beim Starten einer Produktion: {err}" "[DirectorWorker] Produktion mit {} Stück gestartet, was die gesamte freie Kapazität ({}) ausnutzt. Breche ab.",
produced_quantity, free_capacity
); );
break; break;
} }
// Wenn wir weniger als 10% der freien Kapazität produziert haben, könnte es sein,
// dass wir noch mehr Platz haben. Aber sicherheitshalber brechen wir nach einer
// Produktion ab, um Race Conditions zu vermeiden.
// Die nächste Iteration (beim nächsten Director-Check) wird dann wieder prüfen.
break;
} }
Ok(()) Ok(())
@@ -499,7 +519,7 @@ impl DirectorWorker {
&mut self, &mut self,
conn: &mut DbConnection, conn: &mut DbConnection,
plan: &ProductionPlan, plan: &ProductionPlan,
) -> Result<(), DbError> { ) -> Result<i32, DbError> {
// WICHTIG: Kapazität direkt aus dem Plan berechnen (wurde gerade in der Schleife aktualisiert) // WICHTIG: Kapazität direkt aus dem Plan berechnen (wurde gerade in der Schleife aktualisiert)
let free_capacity = Self::calc_free_capacity(plan); let free_capacity = Self::calc_free_capacity(plan);
let one_piece_cost = Self::calc_one_piece_cost(plan); let one_piece_cost = Self::calc_one_piece_cost(plan);
@@ -526,7 +546,7 @@ impl DirectorWorker {
plan.running_productions, plan.running_productions,
plan.running_productions_quantity plan.running_productions_quantity
); );
return Ok(()); return Ok(0);
} }
// Sicherheitsprüfung: to_produce darf niemals größer sein als free_capacity // Sicherheitsprüfung: to_produce darf niemals größer sein als free_capacity
@@ -535,7 +555,7 @@ impl DirectorWorker {
"[DirectorWorker] FEHLER: to_produce ({}) > free_capacity ({})! Das sollte nicht passieren. Breche Produktion ab.", "[DirectorWorker] FEHLER: to_produce ({}) > free_capacity ({})! Das sollte nicht passieren. Breche Produktion ab.",
to_produce, free_capacity to_produce, free_capacity
); );
return Ok(()); return Ok(0);
} }
let production_cost = to_produce as f64 * one_piece_cost; let production_cost = to_produce as f64 * one_piece_cost;
@@ -576,7 +596,8 @@ impl DirectorWorker {
); );
self.base.broker.publish(message); self.base.broker.publish(message);
Ok(()) // Rückgabe der produzierten Menge, damit die Schleife entscheiden kann, ob sie weiterläuft
Ok(to_produce)
} }
fn calc_free_capacity(plan: &ProductionPlan) -> i32 { fn calc_free_capacity(plan: &ProductionPlan) -> i32 {