1use std::collections::{BTreeMap, BTreeSet};
9use std::sync::{Arc, Mutex};
10use std::time::Duration;
11
12use tokio::sync::{Notify, mpsc, oneshot, watch};
13use tracing::{info, warn};
14
15use super::records::RecordSelection;
16use super::stop::Stop;
17use super::{Engine, Occupant, Slot};
18use crate::actions::policy::{Allowed, Policies, Policy};
19use crate::api;
20use crate::cache;
21use crate::config::{ByteSize, Config};
22use crate::docker::{DockerApi, Tty, TtySize};
23use crate::github::GithubApi;
24use crate::isolation;
25use crate::scheduler::{SlotRef, StopReason};
26use crate::sizing::auto;
27use crate::store::Store;
28use crate::sync::lock;
29use crate::time;
30
31pub(super) const CANCEL_GRACE: Duration = Duration::from_secs(90);
34
35#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
38pub enum Shutdown {
39 No,
41 Graceful,
43 Detach,
46 Force,
48}
49
50impl Shutdown {
51 pub fn stopping(self) -> Option<api::Stopping> {
53 match self {
54 Shutdown::No => None,
55 Shutdown::Graceful => Some(api::Stopping::WaitsForJobs),
56 Shutdown::Detach => Some(api::Stopping::LeavesJobs),
57 Shutdown::Force => Some(api::Stopping::KillsJobs),
58 }
59 }
60}
61
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub enum RunningJobs {
65 Keep,
67 Kill,
69}
70
71#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub enum RunningSlots {
74 Drain,
76 Keep,
78}
79
80#[derive(Debug, Clone, Copy, PartialEq, Eq)]
82pub enum Author {
83 Owner,
86 AutoSizing,
88}
89
90pub fn same_slice(running: &Config, new: &Config) -> Result<(), ControlError> {
93 if new.host.slice == running.host.slice {
94 return Ok(());
95 }
96 Err(ControlError::Invalid(format!(
97 "restart runnerd to change the slice: it runs in {}, and host.slice is {} in the new config",
98 running.host.slice, new.host.slice
99 )))
100}
101
102#[derive(Debug, Clone, PartialEq, Eq)]
104pub enum Target {
105 Repository(String),
107 All,
109}
110
111#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
113pub enum ControlError {
114 #[error("{0}")]
116 NotFound(String),
117 #[error("{message}")]
119 Busy {
120 message: String,
122 jobs: Vec<api::RunningJob>,
124 },
125 #[error("{0}")]
127 Conflict(String),
128 #[error("{0}")]
130 Invalid(String),
131 #[error("{0}")]
133 Failed(String),
134 #[error("{0}")]
136 NotRecorded(String),
137 #[error("runnerd is stopping")]
139 Stopped,
140}
141
142#[derive(Debug)]
144pub enum Command {
145 Pause {
147 target: Target,
149 jobs: RunningJobs,
151 reply: oneshot::Sender<Result<api::Paused, ControlError>>,
153 },
154 Resume {
156 target: Target,
158 reply: oneshot::Sender<Result<(), ControlError>>,
160 },
161 Reseed {
163 profile: String,
165 reply: oneshot::Sender<Result<(), ControlError>>,
167 },
168 CleanCache {
170 name: String,
172 reply: oneshot::Sender<Result<(), ControlError>>,
174 },
175 ReplaceConfig {
177 config: Box<Config>,
179 memory_max: Option<u64>,
181 cpus: Option<u64>,
183 commit: Commit,
185 running: RunningSlots,
187 author: Author,
189 reply: oneshot::Sender<Result<Vec<String>, ControlError>>,
192 },
193 RemoveRecords {
195 selection: RecordSelection,
197 reply: oneshot::Sender<Result<api::RemovedRecords, ControlError>>,
199 },
200 OpenShell {
202 job: u64,
204 size: TtySize,
206 reply: oneshot::Sender<Result<Tty, ControlError>>,
208 },
209 CloseShell {
211 container: String,
213 },
214}
215
216pub struct Commit(Box<dyn FnOnce() -> Result<Option<String>, String> + Send>);
219
220impl Commit {
221 pub fn new(write: impl FnOnce() -> Result<Option<String>, String> + Send + 'static) -> Commit {
223 Commit(Box::new(write))
224 }
225
226 pub async fn run(self) -> Result<Option<String>, String> {
228 tokio::task::spawn_blocking(self.0)
229 .await
230 .unwrap_or_else(|join_error| Err(join_error.to_string()))
231 }
232}
233
234impl std::fmt::Debug for Commit {
235 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
236 f.write_str("Commit")
237 }
238}
239
240#[derive(Debug)]
242pub struct Commands(mpsc::UnboundedReceiver<Command>);
243
244impl Commands {
245 pub async fn recv(&mut self) -> Option<Command> {
247 self.0.recv().await
248 }
249}
250
251#[derive(Debug)]
253pub struct Control {
254 repositories: Mutex<BTreeSet<String>>,
257 paused: Mutex<BTreeSet<String>>,
258 actions: Policies,
261 store: Store,
263 shutdown: watch::Sender<Stop>,
264 pub(super) wake: Notify,
265 commands: mpsc::UnboundedSender<Command>,
266}
267
268impl Control {
269 pub fn new(
273 repositories: impl IntoIterator<Item = String>,
274 paused: impl IntoIterator<Item = String>,
275 store: Store,
276 ) -> (Control, Commands) {
277 let repositories: BTreeSet<String> = repositories.into_iter().collect();
278 let paused: BTreeSet<String> = paused
279 .into_iter()
280 .filter(|repository| repositories.contains(repository))
281 .collect();
282 drop(store.paused(paused.iter().cloned().collect()));
284 let (commands, receiver) = mpsc::unbounded_channel();
285 let control = Control {
286 actions: Policies::new(Allowed {
287 setting: api::JobActions::On,
288 repositories: repositories.clone(),
289 reruns: BTreeSet::new(),
291 }),
292 repositories: Mutex::new(repositories),
293 paused: Mutex::new(paused),
294 store,
295 shutdown: watch::Sender::new(Stop::none()),
296 wake: Notify::new(),
297 commands,
298 };
299 (control, Commands(receiver))
300 }
301
302 pub fn mode(&self) -> Shutdown {
304 self.shutdown.borrow().mode()
305 }
306
307 pub(super) fn docker_gone(&self) -> bool {
309 self.shutdown.borrow().docker_gone()
310 }
311
312 pub(super) fn note_docker_gone(&self) -> bool {
315 let news = self.shutdown.send_if_modified(Stop::note_docker_gone);
316 if news {
317 self.wake.notify_one();
318 }
319 news
320 }
321
322 pub(super) async fn hurried(&self) {
324 let mut mode = self.shutdown.subscribe();
325 let _ = mode.wait_for(|stop| stop.hurried()).await;
327 }
328
329 pub async fn set_paused(&self, target: &Target, paused: bool) -> Result<(), ControlError> {
335 let recorded = {
336 let repositories = lock(&self.repositories);
337 let names: Vec<&String> = match target {
338 Target::Repository(repository) => {
339 let Some(name) = repositories.get(repository) else {
340 return Err(not_configured(repository, repositories.iter()));
341 };
342 vec![name]
343 }
344 Target::All => repositories.iter().collect(),
345 };
346 let mut set = lock(&self.paused);
347 for name in names {
348 if paused {
349 set.insert(name.clone());
350 } else {
351 set.remove(name);
352 }
353 }
354 self.store.paused(set.iter().cloned().collect())
356 };
357 self.wake.notify_one();
358 if recorded.await {
359 return Ok(());
360 }
361 let name = match target {
362 Target::Repository(repository) => repository.as_str(),
363 Target::All => "every repository",
364 };
365 let (done, undone) = if paused {
366 ("paused", "resumes it")
367 } else {
368 ("resumed", "pauses it again")
369 };
370 let message = format!(
371 "{name} is {done} until runnerd restarts, but the job history cannot record it, so a restart {undone}"
372 );
373 warn!("{message}");
374 Err(ControlError::NotRecorded(message))
375 }
376
377 pub(crate) fn set_repositories(&self, repositories: BTreeSet<String>) {
379 let mut configured = lock(&self.repositories);
380 let mut paused = lock(&self.paused);
381 let before = paused.len();
382 paused.retain(|repository| repositories.contains(repository));
383 if paused.len() != before {
384 drop(self.store.paused(paused.iter().cloned().collect()));
386 }
387 *configured = repositories;
388 }
389
390 pub fn shutdown(&self, mode: Shutdown) -> Shutdown {
393 self.shutdown.send_if_modified(|stop| stop.ask(mode));
394 self.wake.notify_one();
395 self.mode()
396 }
397
398 pub fn is_paused(&self, repository: &str) -> bool {
400 lock(&self.paused).contains(repository)
401 }
402
403 pub fn action_policy(&self) -> Arc<Policy> {
405 self.actions.now()
406 }
407
408 pub fn set_action_policy(&self, allowed: Allowed) {
411 self.actions.replace(allowed);
412 }
413
414 pub async fn policy_moved(&self, revision: u64) {
416 self.actions.moved(revision).await;
417 }
418
419 pub async fn stop_asked(&self) {
421 let mut mode = self.shutdown.subscribe();
422 let _ = mode.wait_for(|stop| stop.mode() != Shutdown::No).await;
424 }
425
426 async fn send<T>(
427 &self,
428 command: impl FnOnce(oneshot::Sender<T>) -> Command,
429 ) -> Result<T, ControlError> {
430 let (reply, answer) = oneshot::channel();
431 self.commands
432 .send(command(reply))
433 .map_err(|_| ControlError::Stopped)?;
434 answer.await.map_err(|_| ControlError::Stopped)
435 }
436
437 pub async fn pause(
441 &self,
442 target: Target,
443 jobs: RunningJobs,
444 ) -> Result<api::Paused, ControlError> {
445 self.send(|reply| Command::Pause {
446 target,
447 jobs,
448 reply,
449 })
450 .await?
451 }
452
453 pub async fn resume(&self, target: Target) -> Result<(), ControlError> {
456 self.send(|reply| Command::Resume { target, reply }).await?
457 }
458
459 pub async fn reseed(&self, profile: &str) -> Result<(), ControlError> {
461 self.send(|reply| Command::Reseed {
462 profile: profile.to_owned(),
463 reply,
464 })
465 .await?
466 }
467
468 pub async fn clean_cache(&self, name: &str) -> Result<(), ControlError> {
470 self.send(|reply| Command::CleanCache {
471 name: name.to_owned(),
472 reply,
473 })
474 .await?
475 }
476
477 pub async fn remove_records(
479 &self,
480 selection: RecordSelection,
481 ) -> Result<api::RemovedRecords, ControlError> {
482 self.send(|reply| Command::RemoveRecords { selection, reply })
483 .await?
484 }
485
486 pub async fn open_shell(&self, job: u64, size: TtySize) -> Result<Tty, ControlError> {
489 self.send(|reply| Command::OpenShell { job, size, reply })
490 .await?
491 }
492
493 pub fn close_shell(&self, container: String) {
495 let _ = self.commands.send(Command::CloseShell { container });
496 }
497
498 pub async fn replace_config(
506 &self,
507 config: Config,
508 memory_max: Option<u64>,
509 cpus: Option<u64>,
510 commit: Commit,
511 running: RunningSlots,
512 author: Author,
513 ) -> Result<Vec<String>, ControlError> {
514 self.send(|reply| Command::ReplaceConfig {
515 config: Box::new(config),
516 memory_max,
517 cpus,
518 commit,
519 running,
520 author,
521 reply,
522 })
523 .await?
524 }
525}
526
527fn not_configured<'name>(
529 repository: &str,
530 configured: impl Iterator<Item = &'name String>,
531) -> ControlError {
532 let configured: Vec<&str> = configured.map(String::as_str).collect();
533 let configured = if configured.is_empty() {
534 "none is configured".to_owned()
535 } else {
536 format!("configured: {}", configured.join(", "))
537 };
538 ControlError::NotFound(format!(
539 "{repository} is not a configured repository; {configured}"
540 ))
541}
542
543fn list(jobs: &[api::RunningJob]) -> String {
545 jobs.iter()
546 .map(ToString::to_string)
547 .collect::<Vec<_>>()
548 .join(", ")
549}
550
551impl<G: GithubApi + 'static, D: DockerApi + 'static> Engine<G, D> {
552 pub(super) async fn command(&mut self, command: Command) {
554 match command {
555 Command::Pause {
556 target,
557 jobs,
558 reply,
559 } => {
560 let _ = reply.send(self.pause(&target, jobs).await);
561 }
562 Command::Resume { target, reply } => {
563 let _ = reply.send(self.control.set_paused(&target, false).await);
564 }
565 Command::Reseed { profile, reply } => {
566 let _ = reply.send(self.request_reseed(&profile));
567 }
568 Command::CleanCache { name, reply } => {
569 let _ = reply.send(self.clean_cache(&name).await);
570 }
571 Command::ReplaceConfig {
572 config,
573 memory_max,
574 cpus,
575 commit,
576 running,
577 author,
578 reply,
579 } => {
580 if let Err(refused) = same_slice(&self.config, &config) {
581 let _ = reply.send(Err(refused));
582 return;
583 }
584 if running == RunningSlots::Keep
585 && let Some(occupied) = self.occupied_past(&config)
586 {
587 let _ = reply.send(Err(ControlError::Conflict(occupied)));
588 return;
589 }
590 let undone = match author {
591 Author::Owner => self.undone_raises(&config).await,
592 Author::AutoSizing => Vec::new(),
593 };
594 let answer = match commit.run().await {
595 Ok(warning) => {
596 for line in &undone {
597 warn!("config replaced: {line}");
598 }
599 self.reload(*config, memory_max, cpus);
600 Ok(warning.into_iter().chain(undone).collect())
601 }
602 Err(message) => Err(ControlError::Failed(message)),
603 };
604 let _ = reply.send(answer);
605 }
606 Command::RemoveRecords { selection, reply } => {
607 let _ = reply.send(self.remove_records(&selection).await);
608 }
609 Command::OpenShell { job, size, reply } => {
610 let opened = self.open_shell(job, size).await;
611 if let Err(Ok(tty)) = reply.send(opened) {
613 self.close_shell(&tty.container).await;
614 }
615 }
616 Command::CloseShell { container } => self.close_shell(&container).await,
617 }
618 }
619
620 pub(super) async fn pause(
624 &mut self,
625 target: &Target,
626 running: RunningJobs,
627 ) -> Result<api::Paused, ControlError> {
628 let repositories: BTreeSet<String> = match target {
629 Target::Repository(repository) if self.repositories.listed(repository).is_none() => {
630 return Err(not_configured(repository, self.repositories.listed_names()));
631 }
632 Target::Repository(repository) => BTreeSet::from([repository.clone()]),
633 Target::All => self.repositories.listed_names().cloned().collect(),
634 };
635 self.take_found();
636 let runners: Vec<(SlotRef, Occupant)> = self
637 .occupied()
638 .filter(|(_, occupant)| repositories.contains(&occupant.repository))
639 .map(|(slot, occupant)| (slot, occupant.clone()))
640 .collect();
641 let jobs: Vec<api::RunningJob> = runners
642 .iter()
643 .filter(|(_, occupant)| occupant.engaged())
644 .map(|(_, occupant)| occupant.running_job())
645 .collect();
646 if running == RunningJobs::Keep && !jobs.is_empty() {
647 let mut by_repository: BTreeMap<&str, Vec<api::RunningJob>> = BTreeMap::new();
648 for job in &jobs {
649 by_repository
650 .entry(&job.repository)
651 .or_default()
652 .push(job.clone());
653 }
654 let runs: Vec<String> = by_repository
655 .iter()
656 .map(|(repository, jobs)| format!("{repository} runs {}", list(jobs)))
657 .collect();
658 return Err(ControlError::Busy {
660 message: runs.join("; "),
661 jobs,
662 });
663 }
664 let recorded = self.control.set_paused(target, true).await;
666 match target {
667 Target::Repository(repository) => info!("{repository}: paused"),
668 Target::All => info!("paused every repository"),
669 }
670 let mut stopped = Vec::new();
671 if running == RunningJobs::Keep {
672 return recorded.map(|()| api::Paused { stopped });
673 }
674 for (slot, occupant) in runners {
675 let repository = &occupant.repository;
676 let cancelled = if let Some(job) = occupant.named_job() {
677 self.cancel(repository, job).await
678 } else {
679 if occupant.listed_busy() {
680 warn!(
681 "{repository}: {} runs a job its log has not named yet; GitHub fails it once the runner is gone",
682 occupant.runner_name
683 );
684 }
685 false
686 };
687 if cancelled {
688 if let Some(cancelled) = self
690 .slots
691 .values_mut()
692 .flatten()
693 .filter_map(Slot::occupant_mut)
694 .find(|candidate| candidate.runner_name == occupant.runner_name)
695 {
696 cancelled
697 .engagement
698 .cancelled(tokio::time::Instant::now() + CANCEL_GRACE);
699 }
700 self.store.runner_cancel_by(
702 &occupant.runner_name,
703 time::now_ms() + u64::try_from(CANCEL_GRACE.as_millis()).unwrap_or(u64::MAX),
704 );
705 } else {
706 self.kill(&slot).await;
707 if self
708 .occupied()
709 .any(|(_, candidate)| candidate.runner_name == occupant.runner_name)
710 {
711 continue;
712 }
713 }
714 stopped.push(occupant.running_job());
715 }
716 match recorded {
717 Ok(()) => Ok(api::Paused { stopped }),
718 Err(ControlError::NotRecorded(message)) if !stopped.is_empty() => Err(
719 ControlError::NotRecorded(format!("{message}; stopped {}", list(&stopped))),
720 ),
721 Err(error) => Err(error),
722 }
723 }
724
725 pub(super) async fn kill_cancelled(&mut self) {
727 let now = tokio::time::Instant::now();
728 let overdue: Vec<(SlotRef, String)> = self
729 .occupied()
730 .filter(|(_, occupant)| occupant.engagement.cancel_overdue(now))
731 .map(|(slot, occupant)| (slot, occupant.runner_name.clone()))
732 .collect();
733 for (slot, name) in overdue {
734 warn!(
735 "{name} did not exit {}s after its run was cancelled",
736 CANCEL_GRACE.as_secs()
737 );
738 self.kill(&slot).await;
740 }
741 }
742
743 async fn cancel(&self, repository: &str, job: u64) -> bool {
745 let run = match self.github.job(repository, job).await {
746 Ok(details) => details.run_id,
747 Err(error) => {
748 warn!("{repository}: cannot read job {job} to cancel its run: {error}");
749 return false;
750 }
751 };
752 match self.github.cancel_run(repository, run).await {
753 Ok(()) => {
754 info!("{repository}: cancelled run {run} of job {job}");
755 true
756 }
757 Err(error) => {
758 warn!("{repository}: cannot cancel run {run}: {error}");
759 false
760 }
761 }
762 }
763
764 pub(super) fn request_reseed(&mut self, profile: &str) -> Result<(), ControlError> {
766 if !self.config.profiles.contains_key(profile) {
767 return Err(ControlError::NotFound(format!(
768 "{profile} is not a configured profile"
769 )));
770 }
771 if self.is_reseeding(profile) {
772 return Err(ControlError::Conflict(format!(
773 "{profile} is reseeding already"
774 )));
775 }
776 if self.shell_profiles().contains(profile) {
778 return Err(ControlError::Conflict(format!(
779 "a shell is open in a record of {profile}, which mounts its snapshot; reseed it once the shell exits"
780 )));
781 }
782 info!("{profile}: reseeding once its slots are free");
783 self.upkeep.request(profile);
784 Ok(())
785 }
786
787 pub(super) async fn clean_cache(&mut self, name: &str) -> Result<(), ControlError> {
790 let Some(volume) = cache::volumes(&self.names, &self.config)
791 .into_iter()
792 .find(|volume| volume.name == name)
793 else {
794 return Err(ControlError::NotFound(format!(
795 "{name} is not a cache volume of this config; `runnerctl caches` lists them"
796 )));
797 };
798 let using: Vec<SlotRef> = match &volume.kind {
799 api::CacheKind::Snapshot { profile } => {
800 return Err(ControlError::Invalid(format!(
801 "{name} is {profile}'s snapshot, which every slot's layers sit on; reseed {profile} to rebuild it"
802 )));
803 }
804 api::CacheKind::Slot { profile, slot } => {
805 if self.upkeep.reseed_runs(profile) {
806 return Err(ControlError::Conflict(format!(
807 "{profile} is reseeding, which writes {name}"
808 )));
809 }
810 self.occupied()
811 .filter(|(at, _)| at.profile == *profile && at.index + 1 == *slot)
812 .map(|(at, _)| at)
813 .collect()
814 }
815 api::CacheKind::Pnpm
816 | api::CacheKind::Maven
817 | api::CacheKind::Mirror
818 | api::CacheKind::Build => self.occupied().map(|(at, _)| at).collect(),
819 };
820 let shared = matches!(volume.kind, api::CacheKind::Pnpm | api::CacheKind::Maven);
821 if shared && self.records.has_shells() {
822 return Err(ControlError::Conflict(format!(
823 "a shell open in a record mounts {name}; clean it once the shell exits"
824 )));
825 }
826 let idle = using
829 .iter()
830 .filter(|slot| self.occupant(slot).is_some_and(Occupant::idle))
831 .map(|slot| (slot.clone(), StopReason::Clean))
832 .collect();
833 self.remove_idle(idle).await;
834 let users: Vec<api::RunningJob> = using
835 .iter()
836 .filter_map(|slot| self.occupant(slot))
837 .map(Occupant::running_job)
838 .collect();
839 if !users.is_empty() {
840 return Err(ControlError::Busy {
841 message: format!(
842 "{name} is in use by {}; clean it once they finish",
843 list(&users)
844 ),
845 jobs: users,
846 });
847 }
848 if volume.kind == api::CacheKind::Build {
849 let Some(cache) = self.build_cache.clone() else {
850 return Err(ControlError::Failed(
851 "the build cache did not open when runnerd started; its log says why"
852 .to_owned(),
853 ));
854 };
855 let trimmed = tokio::task::spawn_blocking(move || cache.clear())
856 .await
857 .map_err(|error| ControlError::Failed(format!("cannot empty {name}: {error}")))?;
858 info!(
859 "emptied {name}: {} entries, {}",
860 trimmed.before.entries,
861 ByteSize(trimmed.before.bytes)
862 );
863 self.trim_due = true;
864 return Ok(());
865 }
866 if volume.kind == api::CacheKind::Mirror && self.mirror.may_run() {
867 cache::mirror_down(&*self.docker, &self.names)
868 .await
869 .map_err(|error| {
870 ControlError::Failed(format!("cannot stop the mirror: {error}"))
871 })?;
872 self.mirror.stopped();
873 }
874 self.docker
875 .remove_volume(name)
876 .await
877 .map_err(|error| ControlError::Failed(format!("cannot remove {name}: {error}")))?;
878 info!("removed {name}");
879 if let api::CacheKind::Slot { profile, slot } = &volume.kind {
881 self.ran.remove(&SlotRef {
882 profile: profile.clone(),
883 index: slot.saturating_sub(1),
884 });
885 }
886 self.trim_due = true;
888 Ok(())
889 }
890
891 async fn undone_raises(&self, config: &Config) -> Vec<String> {
894 let now = time::now_ms() / 1000;
895 let since = now.saturating_sub(auto::UNDONE_RAISES).saturating_mul(1000);
896 match self.store.sizing_changes(since).await {
897 Ok(changes) => auto::undone_raises(&self.config, config, &changes, now),
898 Err(store_error) => {
899 warn!(
900 "cannot read auto sizing's changes to compare a new config with: {store_error}"
901 );
902 Vec::new()
903 }
904 }
905 }
906
907 fn occupied_past(&self, config: &Config) -> Option<String> {
909 self.slots.iter().find_map(|(name, slots)| {
910 let kept = config
911 .profiles
912 .get(name)
913 .map_or(0, |profile| profile.slots as usize);
914 slots
915 .iter()
916 .enumerate()
917 .skip(kept)
918 .find(|(_, slot)| !slot.is_free())
919 .map(|(index, _)| {
920 format!(
921 "{name}'s slot {} has a runner, and the new config keeps {kept} slots",
922 index + 1
923 )
924 })
925 })
926 }
927
928 pub(super) fn reload(&mut self, config: Config, memory_max: Option<u64>, cpus: Option<u64>) {
933 let mut old = std::mem::replace(&mut self.config, config);
934 self.control.set_action_policy(Allowed::of(&self.config));
936 for (name, state) in self.repositories.iter_listed_mut() {
939 let new = &self.config;
940 state.forget_health(|profile| same_runners(&old, new, name, profile));
941 }
942 if let Some(cache) = &self.build_cache {
943 cache.set_max_size(self.config.caches.build.max_size.0);
944 }
945 self.archives.reload(&self.config);
946 for (name, profile) in &self.config.profiles {
947 let slots = self.slots.entry(name.clone()).or_default();
948 if slots.len() < profile.slots as usize {
949 slots.resize_with(profile.slots as usize, Slot::default);
950 }
951 self.retired.remove(name);
952 }
953 for (name, profile) in std::mem::take(&mut old.profiles) {
954 if !self.config.profiles.contains_key(&name) {
955 self.retired.insert(name, profile);
956 }
957 }
958 self.drop_closed_slots();
959 self.images.stale_due = true;
960 let names: BTreeSet<String> = self
961 .config
962 .repositories
963 .iter()
964 .map(|repository| repository.name.clone())
965 .collect();
966 self.repositories.reload(&names);
967 let config = &self.config;
969 for (name, state) in self.repositories.iter_listed_mut() {
970 state.forget_waits(|profile| config.pool_in(name, profile).is_some());
971 }
972 self.control.set_repositories(names);
973 let profiles = &self.config.profiles;
974 self.upkeep
975 .forget_removed(|name| profiles.contains_key(name));
976 self.prepare_failures.retain(|slot, _| {
977 profiles
978 .get(&slot.profile)
979 .is_some_and(|profile| slot.index < profile.slots as usize)
980 });
981 self.isolation = isolation::backend(self.config.host.isolation);
982 let pooled = |config: &Config| {
985 config.host.admission == crate::config::Admission::Measured && config.host.pooled_count
986 };
987 if pooled(&self.config)
988 && (!pooled(&old)
989 || memory_max != self.memory_max
990 || old.host.memory_ceiling != self.config.host.memory_ceiling)
991 {
992 self.measured.restart_growth(self.now());
993 }
994 self.memory_max = memory_max;
995 self.cpus = cpus;
996 self.caches
997 .send_replace(cache::volumes(&self.names, &self.config));
998 self.reread_warm();
1000 info!("reloaded the config");
1001 }
1002}
1003
1004fn same_runners(old: &Config, new: &Config, repository: &str, profile: &str) -> bool {
1007 let labels = |pool: &crate::config::Repository| -> BTreeSet<String> {
1008 pool.labels
1009 .iter()
1010 .map(|label| label.to_ascii_lowercase())
1011 .collect()
1012 };
1013 let (Some(was), Some(is)) = (
1014 old.pool_in(repository, profile),
1015 new.pool_in(repository, profile),
1016 ) else {
1017 return false;
1018 };
1019 let (Some(was_made), Some(is_made)) = (old.profiles.get(profile), new.profiles.get(profile))
1020 else {
1021 return false;
1022 };
1023 labels(was) == labels(is)
1024 && was_made.image == is_made.image
1025 && was_made.memory.0 == is_made.memory.0
1026 && was_made.cpus == is_made.cpus
1027 && old.host.isolation == new.host.isolation
1028}