Added inline comments

This commit is contained in:
Ian Brown 2024-01-07 23:49:12 -08:00 committed by GitHub
parent 950da0ea02
commit cce734d70e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,48 +1,60 @@
module Invidious::Jobs module Invidious::Jobs
JOBS = [] of BaseJob # This line defines an empty array named JOBS to hold objects of type BaseJob.
SEMAPHORE = ::Channel(Nil).new(5) # Max 5 jobs running at once JOBS = [] of BaseJob
# Automatically generate a structure that wraps the various # SEMAPHORE is a Channel that allows up to 5 items (jobs) to be processed concurrently.
# jobs' configs, so that the following YAML config can be used: # This is a way to limit the maximum number of jobs running at the same time to 5.
# SEMAPHORE = ::Channel(Nil).new(5)
# jobs:
# job_name: # The `macro finished` block is executed once the module definition is complete.
# enabled: true
# some_property: "value"
#
macro finished macro finished
# Define a struct named JobsConfig inside the module.
# This struct is for storing configuration for different jobs.
struct JobsConfig struct JobsConfig
include YAML::Serializable include YAML::Serializable # Allows serialization and deserialization from YAML.
# This loop iterates over all subclasses of BaseJob.
{% for sc in BaseJob.subclasses %} {% for sc in BaseJob.subclasses %}
# Voodoo macro to transform `Some::Module::CustomJob` to `custom` # Generate a getter method for each job. The job's class name is transformed
# from something like `Some::Module::CustomJob` to a simpler form `custom`.
{% class_name = sc.id.split("::").last.id.gsub(/Job$/, "").underscore %} {% class_name = sc.id.split("::").last.id.gsub(/Job$/, "").underscore %}
getter {{ class_name }} = {{ sc.name }}::Config.new getter {{ class_name }} = {{ sc.name }}::Config.new
{% end %} {% end %}
# Define an empty initializer.
def initialize def initialize
end end
end end
end end
# This class method allows registration of a job to the JOBS array.
def self.register(job : BaseJob) def self.register(job : BaseJob)
JOBS << job JOBS << job
end end
# This class method starts all registered jobs.
def self.start_all def self.start_all
# Iterate over each job in the JOBS array.
JOBS.each do |job| JOBS.each do |job|
SEMAPHORE.send(nil) # Acquire a "slot" # Send a nil value to the SEMAPHORE channel. This is like acquiring a "slot".
# If the SEMAPHORE is full (5 jobs running), this line will block until a slot is free.
SEMAPHORE.send(nil)
# Start a new concurrent fiber (lightweight thread) for the job.
spawn do spawn do
begin begin
# Don't run the main routine if the job is disabled by config # If the job is disabled in its configuration, skip the execution.
next if job.disabled? next if job.disabled?
# Start the job by calling its 'begin' method.
job.begin job.begin
rescue ex rescue ex
# If an exception occurs, log the error.
Log.error { "Job failed: #{ex.message}" } Log.error { "Job failed: #{ex.message}" }
ensure ensure
SEMAPHORE.receive # Release the "slot" # After the job is finished or if an error occurred, release the "slot" in the semaphore.
SEMAPHORE.receive
end end
end end
end end