From 950da0ea024fe1e292603aa84a96f4bc5fea0417 Mon Sep 17 00:00:00 2001 From: Ian Brown Date: Sun, 31 Dec 2023 09:27:03 -0800 Subject: [PATCH] Limit jobs to 5 and wrap in exception handler to release Signed-off-by: Ian Brown --- src/invidious/jobs.cr | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/invidious/jobs.cr b/src/invidious/jobs.cr index b6b673f7..b415f62b 100644 --- a/src/invidious/jobs.cr +++ b/src/invidious/jobs.cr @@ -1,5 +1,6 @@ module Invidious::Jobs - JOBS = [] of BaseJob + JOBS = [] of BaseJob + SEMAPHORE = ::Channel(Nil).new(5) # Max 5 jobs running at once # Automatically generate a structure that wraps the various # jobs' configs, so that the following YAML config can be used: @@ -31,10 +32,19 @@ module Invidious::Jobs def self.start_all JOBS.each do |job| - # Don't run the main rountine if the job is disabled by config - next if job.disabled? + SEMAPHORE.send(nil) # Acquire a "slot" + spawn do + begin + # Don't run the main routine if the job is disabled by config + next if job.disabled? - spawn { job.begin } + job.begin + rescue ex + Log.error { "Job failed: #{ex.message}" } + ensure + SEMAPHORE.receive # Release the "slot" + end + end end end end