Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 33 additions & 42 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,59 +8,54 @@ class ApplicationJob < ActiveJob::Base

include GoodJob::ActiveJobExtensions::Concurrency

before_enqueue :add_good_job_labels

good_job_control_concurrency_with(
# Maximum number of unfinished jobs to allow with the concurrency key
# Can be an Integer or Lambda/Proc that is invoked in the context of the job
# total_limit: 1,
# Define one or more concurrency rules. Each rule is scoped to a label,
# which is a value derived from the job's arguments at enqueue time and
# stored on the job record. Jobs must be enqueued with the matching label
# via `good_job_labels:` for the rule to apply.
#
# Multiple rules can be defined; they are evaluated in order and the first
# exceeded rule short-circuits the rest.
good_job_concurrency_rule(
# A label that scopes this rule. Can be a static String or a Lambda/Proc
# invoked in the context of the job instance (see "Dynamic labels" below).
# The rule only applies to jobs that were enqueued with this label in
# `good_job_labels`.
label: -> { "#{self.class.name}-#{queue_name}-#{digest_arguments(arguments)}" },

# Apply the rule's label to every job of this class (and its subclasses),
# so the jobs don't need to be enqueued with it. Defaults to false.
# A Lambda/Proc label that returns nil is not applied.
apply_label: true,

# Maximum number of unfinished jobs with this label to allow.
# Can be an Integer or Lambda/Proc invoked in the context of the job.
total_limit: 1,

# Or, if more control is needed:
# Maximum number of jobs with the concurrency key to be
# concurrently enqueued (excludes performing jobs)
# Can be an Integer or Lambda/Proc that is invoked in the context of the job
# Maximum number of jobs with this label to be concurrently enqueued
# (excludes performing jobs). Can be an Integer or Lambda/Proc.
enqueue_limit: 1,

# Maximum number of jobs with the concurrency key to be
# concurrently performed (excludes enqueued jobs)
# Can be an Integer or Lambda/Proc that is invoked in the context of the job
# Maximum number of jobs with this label to be concurrently performed
# (excludes enqueued jobs). Can be an Integer or Lambda/Proc.
perform_limit: 1,

# Maximum number of jobs with the concurrency key to be enqueued within
# the time period, looking backwards from the current time. Must be an array
# with two elements: the number of jobs and the time period.
# Maximum number of jobs with this label to be enqueued within the time
# period, looking backwards from now. Must be [count, period].
enqueue_throttle: [10, 1.minute],

# Maximum number of jobs with the concurrency key to be performed within
# the time period, looking backwards from the current time. Must be an array
# with two elements: the number of jobs and the time period.
# Maximum number of jobs with this label to be performed within the time
# period, looking backwards from now. Must be [count, period].
perform_throttle: [100, 1.hour],

# Note: Under heavy load, the total number of jobs may exceed the
# sum of `enqueue_limit` and `perform_limit` because of race conditions
# caused by imperfectly disjunctive states. If you need to constrain
# the total number of jobs, use `total_limit` instead. See #378.

# A unique key to be globally locked against.
# Can be String or Lambda/Proc that is invoked in the context of the job.
#
# If a key is not provided GoodJob will use the job class name.
#
# To disable concurrency control, for example in a subclass, set the
# key explicitly to nil (e.g. `key: nil` or `key: -> { nil }`)
#
# If you provide a custom concurrency key (for example, if concurrency is supposed
# to be controlled by the first job argument) make sure that it is sufficiently unique across
# jobs and queues by adding the job class or queue to the key yourself, if needed.
#
# Note: When using a model instance as part of your custom concurrency key, make sure
# to explicitly use its `id` or `to_global_id` because otherwise it will not stringify as expected.
#
# Note: Arguments passed to #perform_later can be accessed through Active Job's `arguments` method
# which is an array containing positional arguments and, optionally, a kwarg hash.
key: -> { "#{self.class.name}-#{queue_name}-#{digest_arguments(arguments)}" }
)

before_enqueue :add_good_job_labels

protected

def good_job_additional_labels
Expand All @@ -71,11 +66,7 @@ def good_job_additional_labels

def add_good_job_labels
return if good_job_additional_labels.empty?
if @good_job_labels.nil?
@good_job_labels = good_job_additional_labels
else
@good_job_labels += good_job_additional_labels
end
self.good_job_labels |= good_job_additional_labels
end

def digest_arguments(arguments)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
class Communication::Website::GitFile::GenerateContentJob < ApplicationJob
queue_as :cats

def perform(git_file)
git_file.generate_content_safely
end

good_job_control_concurrency_with(
good_job_concurrency_rule(
label: -> { "#{self.class.name}-#{queue_name}-#{arguments.first.to_global_id}" },
enqueue_limit: 1,
perform_limit: 1,
key: -> { "#{self.class.name}-#{queue_name}-#{arguments.first.to_global_id}" }
)

end
def perform(git_file)
git_file.generate_content_safely
end

end
2 changes: 1 addition & 1 deletion app/models/communication/block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# migration_identifier :string
# position :integer not null
# published :boolean default(TRUE)
# template_kind :integer default(0), not null, indexed => [university_id]
# template_kind :integer default(NULL), not null, indexed => [university_id]
# title :string
# created_at :datetime not null
# updated_at :datetime not null
Expand Down
2 changes: 1 addition & 1 deletion app/models/communication/extranet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# sso_cert :text
# sso_mapping :jsonb
# sso_name_identifier_format :string
# sso_provider :integer default(0)
# sso_provider :integer default("saml")
# sso_target_url :string
# upper_menu :text default("")
# created_at :datetime not null
Expand Down
2 changes: 1 addition & 1 deletion app/models/communication/media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# id :uuid not null, primary key
# deleted_at :datetime indexed
# origin :integer default(1), not null
# origin :integer default("upload"), not null
# original_byte_size :bigint
# original_checksum :string
# original_content_type :string
Expand Down
4 changes: 2 additions & 2 deletions app/models/communication/website.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@
# git_branch :string
# git_endpoint :string
# git_files_analysed_at :datetime
# git_provider :integer default(0)
# git_provider :integer default("github")
# highlighted_in_showcase :boolean default(FALSE)
# hosting :integer default(1), not null
# hosting :integer default("deuxfleurs"), not null
# in_production :boolean default(FALSE)
# in_production_at :datetime
# in_showcase :boolean default(TRUE)
Expand Down
2 changes: 1 addition & 1 deletion app/models/communication/website/alert.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#
# id :uuid not null, primary key
# deleted_at :datetime
# kind :integer default(0), not null
# kind :integer default("info"), not null
# created_at :datetime not null
# updated_at :datetime not null
# communication_website_id :uuid not null, indexed
Expand Down
2 changes: 1 addition & 1 deletion app/models/communication/website/menu/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# id :uuid not null, primary key
# about_type :string indexed => [about_id]
# html_class :string
# kind :integer default(0)
# kind :integer default("blank")
# position :integer not null
# position_in_tree :integer
# should_open_new_tab :boolean default(FALSE)
Expand Down
2 changes: 1 addition & 1 deletion app/models/education/diploma.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# certification :string
# deleted_at :datetime
# ects :integer
# level :integer default(0)
# level :integer default("not_applicable")
# position :integer not null
# created_at :datetime not null
# updated_at :datetime not null
Expand Down
2 changes: 1 addition & 1 deletion app/models/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# kind :integer
# number_of_lines :integer
# processing_errors :jsonb
# status :integer default(0)
# status :integer default("pending")
# created_at :datetime not null
# updated_at :datetime not null
# language_id :uuid not null, indexed
Expand Down
2 changes: 1 addition & 1 deletion app/models/research/publication.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
# publication_date :date
# ref :string
# slug :string indexed
# source :integer default(0)
# source :integer default("osuny")
# title :string
# url :string
# created_at :datetime not null
Expand Down
4 changes: 2 additions & 2 deletions app/models/university.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# sso_cert :text
# sso_mapping :jsonb
# sso_name_identifier_format :string
# sso_provider :integer default(0)
# sso_provider :integer default("saml")
# sso_target_url :string
# zipcode :string
# created_at :datetime not null
Expand All @@ -32,7 +32,7 @@
# Indexes
#
# index_universities_on_default_language_id (default_language_id)
# index_universities_on_name (name) USING gin
# index_universities_on_name (name gin_trgm_ops) USING gin
#
# Foreign Keys
#
Expand Down
2 changes: 1 addition & 1 deletion app/models/university/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
# country :string
# deleted_at :datetime
# email :string
# kind :integer default(10)
# kind :integer default("company")
# latitude :float
# longitude :float
# migration_identifier :string
Expand Down
16 changes: 8 additions & 8 deletions app/models/university/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#
# id :uuid not null, primary key
# address :string
# address_visibility :integer default(0)
# address_visibility :integer default("private")
# birthdate :date
# bodyclass :string
# city :string
# country :string
# deleted_at :datetime
# email :string
# email_visibility :integer default(0)
# email_visibility :integer default("private")
# gender :integer
# habilitation :boolean default(FALSE)
# invitation_sent_at :datetime
Expand All @@ -20,16 +20,16 @@
# is_author :boolean
# is_researcher :boolean
# is_teacher :boolean
# linkedin_visibility :integer default(0)
# mastodon_visibility :integer default(0)
# linkedin_visibility :integer default("private")
# mastodon_visibility :integer default("private")
# phone_mobile :string
# phone_mobile_visibility :integer default(0)
# phone_mobile_visibility :integer default("private")
# phone_personal :string
# phone_personal_visibility :integer default(0)
# phone_personal_visibility :integer default("private")
# phone_professional :string
# phone_professional_visibility :integer default(0)
# phone_professional_visibility :integer default("private")
# tenure :boolean default(FALSE)
# twitter_visibility :integer default(0)
# twitter_visibility :integer default("private")
# zipcode :string
# created_at :datetime not null
# updated_at :datetime not null
Expand Down
16 changes: 8 additions & 8 deletions app/models/university/person/alumnus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
#
# id :uuid not null, primary key
# address :string
# address_visibility :integer default(0)
# address_visibility :integer default("private")
# birthdate :date
# bodyclass :string
# city :string
# country :string
# deleted_at :datetime
# email :string
# email_visibility :integer default(0)
# email_visibility :integer default("private")
# gender :integer
# habilitation :boolean default(FALSE)
# invitation_sent_at :datetime
Expand All @@ -20,16 +20,16 @@
# is_author :boolean
# is_researcher :boolean
# is_teacher :boolean
# linkedin_visibility :integer default(0)
# mastodon_visibility :integer default(0)
# linkedin_visibility :integer default("private")
# mastodon_visibility :integer default("private")
# phone_mobile :string
# phone_mobile_visibility :integer default(0)
# phone_mobile_visibility :integer default("private")
# phone_personal :string
# phone_personal_visibility :integer default(0)
# phone_personal_visibility :integer default("private")
# phone_professional :string
# phone_professional_visibility :integer default(0)
# phone_professional_visibility :integer default("private")
# tenure :boolean default(FALSE)
# twitter_visibility :integer default(0)
# twitter_visibility :integer default("private")
# zipcode :string
# created_at :datetime not null
# updated_at :datetime not null
Expand Down
2 changes: 1 addition & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
# remember_created_at :datetime
# reset_password_sent_at :datetime
# reset_password_token :string uniquely indexed
# role :integer default(0)
# role :integer default("visitor")
# second_factor_attempts_count :integer default(0)
# session_token :string
# sign_in_count :integer default(0), not null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class AddIndexGoodJobsConcurrencyKeyCreatedAt < ActiveRecord::Migration[8.1]
disable_ddl_transaction!

def change
reversible do |dir|
dir.up do
# Ensure this incremental update migration is idempotent
# with monolithic install migration.
return if connection.index_exists? :good_jobs, [:concurrency_key, :created_at]
end
end

add_index :good_jobs, [:concurrency_key, :created_at], algorithm: :concurrently
end
end
17 changes: 17 additions & 0 deletions db/migrate/20261019092220_add_index_good_jobs_job_class.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class AddIndexGoodJobsJobClass < ActiveRecord::Migration[8.1]
disable_ddl_transaction!

def change
reversible do |dir|
dir.up do
# Ensure this incremental update migration is idempotent
# with monolithic install migration.
return if connection.index_exists? :good_jobs, :job_class
end
end

add_index :good_jobs, :job_class, algorithm: :concurrently
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

class AddIndexGoodJobsFinishedAtForCleanup < ActiveRecord::Migration[8.1]
disable_ddl_transaction!

def change
reversible do |dir|
dir.up do
# Ensure this incremental update migration is idempotent
# with monolithic install migration.
return if connection.index_exists? :good_jobs, [:finished_at], name: :index_good_jobs_jobs_on_finished_at_only
end
end

add_index :good_jobs, [:finished_at], where: "finished_at IS NOT NULL", name: :index_good_jobs_jobs_on_finished_at_only, algorithm: :concurrently
end
end
Loading
Loading