Limit jobs to 5 and wrap in exception handler to release

Signed-off-by: Ian Brown <ian@zestysoft.com>
This commit is contained in:
Ian Brown 2023-12-31 09:27:03 -08:00
parent 97c4165f55
commit 950da0ea02
No known key found for this signature in database
GPG Key ID: D9923DD6508A15B9
1 changed files with 14 additions and 4 deletions

View File

@ -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