1use std::collections::{BTreeMap, BTreeSet};
6use std::time::{Duration, SystemTime};
7
8use crate::admission::Key;
9use crate::api::Waiting;
10use crate::github::QueuedJob;
11
12pub mod engine;
13pub mod warm;
14
15pub const IDLE_TIMEOUT: Duration = Duration::from_secs(300);
18
19pub const CPU_PRIORITY_AFTER: Duration = Duration::from_secs(120);
22
23#[derive(Debug, Clone, Copy)]
25pub struct Hold {
26 pub until: SystemTime,
28 pub cause: crate::api::HoldCause,
30}
31
32#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
35pub struct Pool {
36 pub repository: String,
38 pub profile: String,
40}
41
42impl From<&crate::config::Repository> for Pool {
43 fn from(repository: &crate::config::Repository) -> Pool {
44 Pool {
45 repository: repository.name.clone(),
46 profile: repository.profile.clone(),
47 }
48 }
49}
50
51#[derive(Debug, Clone)]
54pub struct Repository {
55 pub name: String,
57 pub profile: String,
59 pub labels: Vec<String>,
61 pub paused: bool,
63 pub held: Option<Hold>,
65 pub queued: Vec<QueuedJob>,
67 pub runner_memory: Option<u64>,
70 pub runner_cpu: Option<u64>,
73 pub cpu_waiting_since: Option<SystemTime>,
75 pub warm: usize,
77}
78
79impl Repository {
80 pub fn pool(&self) -> Pool {
82 Pool {
83 repository: self.name.clone(),
84 profile: self.profile.clone(),
85 }
86 }
87
88 fn starved_of_cpu(&self, now: SystemTime) -> bool {
90 self.cpu_waiting_since
91 .and_then(|since| now.duration_since(since).ok())
92 .is_some_and(|waited| waited >= CPU_PRIORITY_AFTER)
93 }
94}
95
96#[derive(Debug, Clone)]
98pub struct Profile {
99 pub memory: u64,
101 pub cpus: u64,
104 pub preparing: Option<Preparing>,
106 pub upkeep_due: bool,
109 pub count: usize,
111 pub slots: Vec<Slot>,
113 pub ran: Vec<Option<Key>>,
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub enum Preparing {
121 Reseeding,
123 PullingImage,
125}
126
127#[derive(Debug, Clone, PartialEq, Eq)]
129pub enum Slot {
130 Free,
132 Runner(RunnerSlot),
134 Closed,
137}
138
139#[derive(Debug, Clone, PartialEq, Eq)]
141pub struct RunnerSlot {
142 pub repository: String,
144 pub busy: bool,
146 pub started_at: SystemTime,
148 pub memory: u64,
150 pub cpu: u64,
152}
153
154#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
156pub struct SlotRef {
157 pub profile: String,
159 pub index: usize,
161}
162
163#[derive(Debug, Clone, PartialEq, Eq)]
165pub enum Decision {
166 Start {
168 slot: SlotRef,
170 repository: String,
172 memory: u64,
174 cpu: u64,
176 warm: bool,
178 replacing: Vec<SlotRef>,
181 },
182 HeldForCpu {
185 repository: String,
187 profile: String,
189 },
190 Stop {
193 slot: SlotRef,
195 },
196}
197
198#[derive(Debug, Clone, Default, PartialEq, Eq)]
200pub struct Plan {
201 pub decisions: Vec<Decision>,
203 pub waiting: BTreeMap<u64, Waiting>,
207 pub warm: BTreeSet<SlotRef>,
209}
210
211#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
213pub struct Budget {
214 pub memory: Option<i64>,
217 pub cpu: Option<i64>,
220 pub host: Option<i64>,
223 pub held: bool,
226}
227
228const GITHUB_HOSTED: &[&str] = &[
234 "ubuntu-slim",
235 "ubuntu-latest",
236 "ubuntu-26.04",
237 "ubuntu-24.04",
238 "ubuntu-22.04",
239 "ubuntu-26.04-arm",
240 "ubuntu-24.04-arm",
241 "ubuntu-22.04-arm",
242 "windows-latest",
243 "windows-2025",
244 "windows-2025-vs2026",
245 "windows-2022",
246 "windows-11-arm",
247 "windows-11-vs2026-arm",
248 "macos-latest",
249 "macos-26",
250 "macos-15",
251 "macos-14",
252 "macos-26-intel",
253 "macos-15-intel",
254 "xcode-27",
255 "macos-latest-large",
256 "macos-26-large",
257 "macos-15-large",
258 "macos-14-large",
259 "macos-latest-xlarge",
260 "macos-26-xlarge",
261 "macos-15-xlarge",
262 "macos-14-xlarge",
263 "xcode-27-xlarge",
264];
265
266fn hosted_by_github(job: &QueuedJob) -> bool {
270 matches!(&job.labels[..], [label] if GITHUB_HOSTED.iter().any(|hosted| hosted.eq_ignore_ascii_case(label)))
271}
272
273pub fn matches(job: &QueuedJob, labels: &[String]) -> bool {
276 crate::config::labels_match(&job.labels, labels)
277}
278
279pub fn plan(
294 repositories: &[Repository],
295 profiles: &BTreeMap<String, Profile>,
296 budget: Budget,
297 now: SystemTime,
298) -> Plan {
299 let mut decided = Plan::default();
300 unserved(repositories, &mut decided);
301 let mut wanted = BTreeMap::new();
302 let mut warm_wanted = Vec::new();
303 let mut jobs = Vec::new();
304 for repository in repositories {
305 if let Some(sorted) = sort_out(repository, profiles, now, &mut decided) {
306 wanted.insert(pool_key(repository), sorted.serve.len());
307 jobs.extend(sorted.serve.into_iter().map(|job| (job, repository)));
308 warm_wanted.push((repository, sorted.warm));
309 }
310 }
311 jobs.sort_by_key(|(id, _)| *id);
312
313 let mut warm = Warm::new(&decided.warm, profiles, budget.held);
314 let mut taken = taken_slots(profiles);
315 let mut left = Left {
316 memory: budget.memory,
317 cpu: budget.cpu,
318 host: budget.host,
319 };
320 let mut started = Started::new(&taken, left);
321 let mut first_for_cpu = None;
323 for (position, &(job, repository)) in jobs.iter().enumerate() {
324 let Some(missing) = wanted.get_mut(&pool_key(repository)) else {
325 continue;
326 };
327 if *missing == 0 {
328 continue;
329 }
330 let (Some(profile), Some(taken)) = (
331 profiles.get(&repository.profile),
332 taken.get_mut(repository.profile.as_str()),
333 ) else {
334 continue;
335 };
336 if let Some(older) = first_for_cpu {
337 decided
338 .waiting
339 .insert(job, Waiting::BehindCpu { job: older });
340 continue;
341 }
342 let memory = repository.runner_memory.unwrap_or(profile.memory);
343 let cpu = repository.runner_cpu.unwrap_or(profile.cpus);
344 let free_index = free_slot(taken, profile, repository, job, &jobs[position + 1..]);
345 let fits = free_index.is_some() && left.fits(memory, cpu);
346 let room = if fits || profile.preparing.is_some() {
347 None
348 } else {
349 warm.make_room(repository, free_index.is_none(), left, memory, cpu)
350 };
351 let index = match (room.as_ref(), free_index) {
352 (Some(room), free) => free.or(room.slot).unwrap_or_default(),
353 (None, Some(index)) if fits => index,
354 (None, free) => {
355 let (reason, short) = why_waits(
356 &repository.profile,
357 profile,
358 free.is_some(),
359 memory,
360 cpu,
361 left,
362 &started,
363 );
364 decided.waiting.insert(job, reason);
365 if short == Short::Cpu {
366 let held = held_for_cpu(repository);
367 if !decided.decisions.contains(&held) {
368 decided.decisions.push(held);
369 }
370 if repository.starved_of_cpu(now) {
372 first_for_cpu = Some(job);
373 }
374 }
375 continue;
376 }
377 };
378 let replacing = room.map_or_else(Vec::new, |room| {
380 left.give(room.memory, room.cpu);
381 for slot in &room.stopped {
382 decided.warm.remove(slot);
383 decided
384 .decisions
385 .push(Decision::Stop { slot: slot.clone() });
386 }
387 room.stopped
388 });
389 taken[index] = true;
390 *missing -= 1;
391 started.note(&repository.profile, job);
392 left.take(memory, cpu);
393 decided
394 .decisions
395 .push(start(repository, index, memory, cpu, replacing));
396 }
397 if !budget.held {
398 start_warm(&warm_wanted, profiles, &mut taken, left, &mut decided);
399 }
400 decided
401}
402
403fn free_slot(
409 taken: &[bool],
410 profile: &Profile,
411 repository: &Repository,
412 job: u64,
413 later: &[(u64, &Repository)],
414) -> Option<usize> {
415 let ran = |index: usize| profile.ran.get(index).and_then(Option::as_ref);
416 let ran_it = |index: usize, repository: &Repository, job: u64| {
417 let queued = repository.queued.iter().find(|queued| queued.id == job);
418 queued.zip(ran(index)).is_some_and(|(queued, key)| {
419 key.repository == repository.name
420 && key.workflow == queued.workflow
421 && key.name == queued.name
422 })
423 };
424 taken
425 .iter()
426 .enumerate()
427 .filter(|(_, taken)| !**taken)
428 .map(|(index, _)| index)
429 .min_by_key(|&index| {
430 let wanted_later = later.iter().any(|&(id, other)| {
431 other.profile == repository.profile && ran_it(index, other, id)
432 });
433 let same_repository = ran(index).is_some_and(|key| key.repository == repository.name);
434 (
435 !ran_it(index, repository, job),
436 wanted_later,
437 !same_repository,
438 index,
439 )
440 })
441}
442
443fn held_for_cpu(repository: &Repository) -> Decision {
445 Decision::HeldForCpu {
446 repository: repository.name.clone(),
447 profile: repository.profile.clone(),
448 }
449}
450
451fn pool_key(repository: &Repository) -> (&str, &str) {
453 (&repository.name, &repository.profile)
454}
455
456fn unserved(repositories: &[Repository], decided: &mut Plan) {
458 let mut seen = BTreeSet::new();
459 for repository in repositories {
460 if !seen.insert(repository.name.as_str()) {
461 continue;
462 }
463 let pools: Vec<&Repository> = repositories
464 .iter()
465 .filter(|pool| pool.name == repository.name)
466 .collect();
467 decided.waiting.extend(
468 repository
469 .queued
470 .iter()
471 .filter(|job| {
472 !hosted_by_github(job) && !pools.iter().any(|pool| matches(job, &pool.labels))
473 })
474 .map(|job| {
475 (
476 job.id,
477 Waiting::NoPool {
478 labels: job.labels.clone(),
479 },
480 )
481 }),
482 );
483 }
484}
485
486#[derive(Debug, Clone, Copy, PartialEq, Eq)]
488enum Short {
489 Slot,
490 Memory,
491 Cpu,
492}
493
494fn why_waits(
497 name: &str,
498 profile: &Profile,
499 free_slot: bool,
500 memory: u64,
501 cpu: u64,
502 left: Left,
503 started: &Started<'_>,
504) -> (Waiting, Short) {
505 let profile_name = || name.to_owned();
506 if !free_slot {
507 let reason = match profile.preparing {
508 Some(Preparing::Reseeding) => Waiting::Reseeding {
509 profile: profile_name(),
510 },
511 Some(Preparing::PullingImage) => Waiting::PullingImage {
512 profile: profile_name(),
513 },
514 None => started.for_slot(
515 name,
516 Waiting::NoSlot {
517 profile: profile_name(),
518 },
519 ),
520 };
521 return (reason, Short::Slot);
522 }
523 match (left.memory, left.host, left.cpu) {
524 (Some(free), _, _) if free < signed(memory) => (
525 started.for_memory(
526 memory,
527 Waiting::Memory {
528 needs: memory,
529 free: unsigned(free),
530 },
531 ),
532 Short::Memory,
533 ),
534 (_, Some(free), _) if free < signed(memory) => (
535 started.for_host(
536 memory,
537 Waiting::HostMemory {
538 needs: memory,
539 free: unsigned(free),
540 },
541 ),
542 Short::Memory,
543 ),
544 (_, _, Some(free)) => (
545 started.for_cpu(
546 cpu,
547 Waiting::Cpu {
548 needs: cpu,
549 free: unsigned(free),
550 },
551 ),
552 Short::Cpu,
553 ),
554 (_, _, None) => (
556 Waiting::NoSlot {
557 profile: profile_name(),
558 },
559 Short::Slot,
560 ),
561 }
562}
563
564fn start(
567 repository: &Repository,
568 index: usize,
569 memory: u64,
570 cpu: u64,
571 replacing: Vec<SlotRef>,
572) -> Decision {
573 Decision::Start {
574 slot: SlotRef {
575 profile: repository.profile.clone(),
576 index,
577 },
578 repository: repository.name.clone(),
579 memory,
580 cpu,
581 warm: false,
582 replacing,
583 }
584}
585
586fn start_warm(
590 wanted: &[(&Repository, usize)],
591 profiles: &BTreeMap<String, Profile>,
592 taken: &mut BTreeMap<&str, Vec<bool>>,
593 mut left: Left,
594 decided: &mut Plan,
595) {
596 let room_wanted = decided.waiting.values().any(|reason| {
597 matches!(
598 reason,
599 Waiting::NoSlot { .. }
600 | Waiting::Memory { .. }
601 | Waiting::HostMemory { .. }
602 | Waiting::Cpu { .. }
603 | Waiting::BehindCpu { .. }
604 | Waiting::BehindStart { .. }
605 )
606 });
607 if room_wanted {
608 return;
609 }
610 for round in 0.. {
611 let mut any = false;
612 for (repository, wants) in wanted {
613 if *wants <= round {
614 continue;
615 }
616 let (Some(profile), Some(taken)) = (
617 profiles.get(&repository.profile),
618 taken.get_mut(repository.profile.as_str()),
619 ) else {
620 continue;
621 };
622 let (memory, cpu) = (profile.memory, profile.cpus);
623 let Some(index) = taken.iter().position(|taken| !taken) else {
624 continue;
625 };
626 if !left.fits(memory, cpu) {
627 continue;
628 }
629 taken[index] = true;
630 left.take(memory, cpu);
631 any = true;
632 decided.decisions.push(Decision::Start {
633 slot: SlotRef {
634 profile: repository.profile.clone(),
635 index,
636 },
637 repository: repository.name.clone(),
638 memory,
639 cpu,
640 warm: true,
641 replacing: Vec::new(),
642 });
643 }
644 if !any {
645 break;
646 }
647 }
648}
649
650#[derive(Debug, Clone, Copy)]
653struct Left {
654 memory: Option<i64>,
655 cpu: Option<i64>,
656 host: Option<i64>,
658}
659
660impl Left {
661 fn fits(self, memory: u64, cpu: u64) -> bool {
662 let fits = |free: Option<i64>, needs: u64| free.is_none_or(|free| free >= signed(needs));
663 fits(self.memory, memory) && fits(self.host, memory) && fits(self.cpu, cpu)
664 }
665
666 fn short(self, memory: u64, cpu: u64) -> Left {
669 let lacks = |free: Option<i64>, needs: u64| {
670 free.map(|free| signed(needs).saturating_sub(free).max(0))
671 };
672 Left {
673 memory: lacks(self.memory, memory).max(lacks(self.host, memory)),
674 cpu: lacks(self.cpu, cpu),
675 host: None,
676 }
677 }
678
679 fn take(&mut self, memory: u64, cpu: u64) {
680 let take = |free: &mut Option<i64>, used: u64| {
681 *free = free.map(|free| free.saturating_sub(signed(used)));
682 };
683 take(&mut self.memory, memory);
684 take(&mut self.host, memory);
685 take(&mut self.cpu, cpu);
686 }
687
688 fn give(&mut self, memory: u64, cpu: u64) {
689 let give = |free: &mut Option<i64>, freed: u64| {
690 *free = free.map(|free| free.saturating_add(signed(freed)));
691 };
692 give(&mut self.memory, memory);
693 give(&mut self.host, memory);
694 give(&mut self.cpu, cpu);
695 }
696}
697
698fn left(budget: u64, in_use: u64) -> i64 {
701 signed(budget).saturating_sub(signed(in_use))
702}
703
704fn signed(amount: u64) -> i64 {
706 i64::try_from(amount).unwrap_or(i64::MAX)
707}
708
709fn unsigned(free: i64) -> u64 {
711 u64::try_from(free).unwrap_or(0)
712}
713
714struct Sorted {
716 serve: Vec<u64>,
717 warm: usize,
718}
719
720fn sort_out(
724 repository: &Repository,
725 profiles: &BTreeMap<String, Profile>,
726 now: SystemTime,
727 decided: &mut Plan,
728) -> Option<Sorted> {
729 let mut matching: Vec<&QueuedJob> = repository
730 .queued
731 .iter()
732 .filter(|job| matches(job, &repository.labels))
733 .collect();
734 let profile = profiles.get(&repository.profile);
735 let mut warm = repository.warm;
736 if repository.paused {
737 decided
738 .waiting
739 .extend(matching.iter().map(|job| (job.id, Waiting::Paused)));
740 matching.clear();
742 warm = 0;
743 }
744 let upkeep_due = profile.is_some_and(|profile| profile.upkeep_due);
747 let stop_now = upkeep_due || repository.paused;
748 if upkeep_due {
749 warm = 0;
750 }
751 let (idle, closing): (Vec<(SlotRef, &RunnerSlot)>, Vec<_>) = profile
752 .into_iter()
753 .flat_map(|profile| profile.slots.iter().enumerate())
754 .filter_map(|(index, slot)| match slot {
755 Slot::Runner(runner) if runner.repository == repository.name && !runner.busy => Some((
756 SlotRef {
757 profile: repository.profile.clone(),
758 index,
759 },
760 runner,
761 )),
762 _ => None,
763 })
764 .partition(|(slot, _)| profile.is_none_or(|profile| slot.index < profile.count));
765 decided
767 .decisions
768 .extend(closing.into_iter().map(|(slot, _)| Decision::Stop { slot }));
769 let surplus = idle.len().saturating_sub(matching.len());
770 let stops: Vec<SlotRef> = idle
771 .iter()
772 .filter(|(_, runner)| {
773 stop_now
774 || now
775 .duration_since(runner.started_at)
776 .is_ok_and(|waited| waited >= IDLE_TIMEOUT)
777 })
778 .take(surplus.saturating_sub(warm))
779 .map(|(slot, _)| slot.clone())
780 .collect();
781 let kept: Vec<&SlotRef> = idle
783 .iter()
784 .map(|(slot, _)| slot)
785 .filter(|slot| !stops.contains(slot))
786 .collect();
787 decided
788 .warm
789 .extend(kept.iter().skip(matching.len()).map(|slot| (*slot).clone()));
790 let warm = warm.saturating_sub(kept.len().saturating_sub(matching.len()));
791 decided
792 .decisions
793 .extend(stops.into_iter().map(|slot| Decision::Stop { slot }));
794 let missing = matching.len().saturating_sub(idle.len());
795 let skip = matching.len() - missing;
797 decided.waiting.extend(
798 matching
799 .iter()
800 .take(skip)
801 .map(|job| (job.id, Waiting::RunnerStarting)),
802 );
803 let serve = matching.iter().skip(skip).map(|job| job.id);
804 match repository.held.filter(|held| held.until > now) {
805 Some(held) => {
806 let until = crate::time::ms(held.until) / 1000;
807 decided.waiting.extend(serve.map(|job| {
808 (
809 job,
810 Waiting::Backoff {
811 until,
812 cause: held.cause,
813 },
814 )
815 }));
816 None
817 }
818 None => Some(Sorted {
819 serve: serve.collect(),
820 warm,
821 }),
822 }
823}
824
825struct Warm<'plan> {
827 runners: Vec<Option<(SlotRef, &'plan RunnerSlot)>>,
829}
830
831struct Room {
833 stopped: Vec<SlotRef>,
835 slot: Option<usize>,
837 memory: u64,
838 cpu: u64,
839}
840
841impl<'plan> Warm<'plan> {
842 fn new(
844 warm: &BTreeSet<SlotRef>,
845 profiles: &'plan BTreeMap<String, Profile>,
846 held: bool,
847 ) -> Warm<'plan> {
848 let runners = warm
849 .iter()
850 .filter(|_| !held)
851 .filter_map(
852 |slot| match profiles.get(&slot.profile)?.slots.get(slot.index)? {
853 Slot::Runner(runner) => Some(Some((slot.clone(), runner))),
854 Slot::Free | Slot::Closed => None,
855 },
856 )
857 .collect();
858 Warm { runners }
859 }
860
861 fn make_room(
866 &mut self,
867 repository: &Repository,
868 slot: bool,
869 left: Left,
870 needs_memory: u64,
871 needs_cpu: u64,
872 ) -> Option<Room> {
873 let short = left.short(needs_memory, needs_cpu);
874 let (memory, cpu) = (
875 unsigned(short.memory.unwrap_or(0)),
876 unsigned(short.cpu.unwrap_or(0)),
877 );
878 let mut candidates: Vec<usize> = self
879 .runners
880 .iter()
881 .enumerate()
882 .filter_map(|(at, runner)| {
883 runner
884 .as_ref()
885 .filter(|(slot, runner)| {
886 runner.repository != repository.name || slot.profile != repository.profile
887 })
888 .map(|_| at)
889 })
890 .collect();
891 candidates.sort_by_key(|at| {
893 self.runners[*at]
894 .as_ref()
895 .is_some_and(|(slot, _)| slot.profile != repository.profile)
896 });
897 let mut room = Room {
898 stopped: Vec::new(),
899 slot: None,
900 memory: 0,
901 cpu: 0,
902 };
903 let mut picked = Vec::new();
904 for at in candidates {
905 let short = (slot && room.slot.is_none()) || room.memory < memory || room.cpu < cpu;
906 if !short {
907 break;
908 }
909 let Some((ref_, runner)) = &self.runners[at] else {
910 continue;
911 };
912 let gives_slot = ref_.profile == repository.profile;
913 let helps = (gives_slot && slot && room.slot.is_none())
915 || (room.memory < memory && runner.memory > 0)
916 || (room.cpu < cpu && runner.cpu > 0);
917 if !helps {
918 continue;
919 }
920 if gives_slot && room.slot.is_none() {
921 room.slot = Some(ref_.index);
922 }
923 room.memory += runner.memory;
924 room.cpu += runner.cpu;
925 room.stopped.push(ref_.clone());
926 picked.push(at);
927 }
928 let mut after = left;
930 after.give(room.memory, room.cpu);
931 if (slot && room.slot.is_none()) || !after.fits(needs_memory, needs_cpu) {
932 return None;
933 }
934 for at in picked {
935 self.runners[at] = None;
936 }
937 Some(room)
938 }
939}
940
941struct Started<'plan> {
944 free_slots: BTreeSet<&'plan str>,
946 before: Left,
948 by_profile: BTreeMap<&'plan str, u64>,
950 first: Option<u64>,
951}
952
953impl<'plan> Started<'plan> {
954 fn new(taken: &BTreeMap<&'plan str, Vec<bool>>, before: Left) -> Started<'plan> {
955 Started {
956 free_slots: taken
957 .iter()
958 .filter(|(_, slots)| slots.contains(&false))
959 .map(|(name, _)| *name)
960 .collect(),
961 before,
962 by_profile: BTreeMap::new(),
963 first: None,
964 }
965 }
966
967 fn note(&mut self, profile: &'plan str, job: u64) {
969 self.by_profile.entry(profile).or_insert(job);
970 self.first.get_or_insert(job);
971 }
972
973 fn for_slot(&self, profile: &str, reason: Waiting) -> Waiting {
975 match self.by_profile.get(profile) {
976 Some(older) if self.free_slots.contains(profile) => {
977 Waiting::BehindStart { job: *older }
978 }
979 _ => reason,
980 }
981 }
982
983 fn for_memory(&self, needs: u64, reason: Waiting) -> Waiting {
985 Self::behind(self.first, self.before.memory, needs).unwrap_or(reason)
986 }
987
988 fn for_host(&self, needs: u64, reason: Waiting) -> Waiting {
990 Self::behind(self.first, self.before.host, needs).unwrap_or(reason)
991 }
992
993 fn for_cpu(&self, needs: u64, reason: Waiting) -> Waiting {
995 Self::behind(self.first, self.before.cpu, needs).unwrap_or(reason)
996 }
997
998 fn behind(first: Option<u64>, free: Option<i64>, needs: u64) -> Option<Waiting> {
999 first
1000 .filter(|_| free.is_some_and(|free| free >= signed(needs)))
1001 .map(|job| Waiting::BehindStart { job })
1002 }
1003}
1004
1005fn taken_slots(profiles: &BTreeMap<String, Profile>) -> BTreeMap<&str, Vec<bool>> {
1007 profiles
1008 .iter()
1009 .map(|(name, profile)| {
1010 let taken = profile
1011 .slots
1012 .iter()
1013 .map(|slot| profile.preparing.is_some() || *slot != Slot::Free)
1014 .collect();
1015 (name.as_str(), taken)
1016 })
1017 .collect()
1018}
1019
1020#[cfg(test)]
1021mod tests;