Skip to content

Critical error on multi-tenant app related to i18n available_locales #284

@nflorentin

Description

@nflorentin

Hi,

First, thanks you for your work!

I’m running into an issue that causes mission_control-jobs to fail in a multi-tenant app.

My multi-tenant setup is custom-made, but I’m quite sure the problem would also occur with a gem like acts_as_tenant, since it uses the same pattern to set the current tenant in a job.

Chronology to trigger the problem:

  • access /jobs/applications/ubikly/finished/jobs
  • ActiveJob::JobProxy is initialized somewhere and calls deserialize
  • if the tenant is nil, Current#reset is called and the line I18n.default_locale = Rails.application.config.i18n.default_locale failed with the following error : I18n::InvalidLocale at /applications/ubikly/finished/jobs :"en-US" is not a valid locale

It seems that at the moment when default_locale is being set, :"en-US" is not yet available, so the error is raised. However, in my debug console, I18n.available_locale?(:"en-US") returns true.

It seems linked to issues #229 and #244.

config/initializers/i18n.rb:

Rails.application.configure do
  config.i18n.default_locale = :"en-US"

  config.i18n.available_locales = %i[en-GB en-US es-ES es-MX fr-FR fr-CH fr-CA]
end

app/models/current.rb:

# frozen_string_literal: true

class Current < ActiveSupport::CurrentAttributes
  attribute :tenant

  resets do
    I18n.default_locale = Rails.application.config.i18n.default_locale # => error is raised from here
  end

  def tenant=(tenant)
    super

    if tenant.nil?
      reset
      return
    end

    I18n.default_locale = tenant.default_locale
  end
end

active_job_extensions.rb:

# frozen_string_literal: true

module ActiveJobExtensions
  def serialize
    super.merge("current_tenant" => Current.tenant&.to_global_id&.to_s)
  end

  def deserialize(job_data)
    tenant_global_id = job_data["current_tenant"]
    tenant = tenant_global_id ? GlobalID::Locator.locate(tenant_global_id) : nil
    Current.tenant = tenant
    super
  end
end

config/initializers/active_job.rb:

# frozen_string_literal: true

ActiveSupport.on_load(:active_job) do |base|
  base.prepend ActiveJobExtensions
end

ruby & gems :

  • rails 8.0.2
  • ruby 3.4.5
  • solid_queue 1.2.1
  • mission_control-jobs 1.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions