From 35fa271370c72a5f033951828633c4427145c13e Mon Sep 17 00:00:00 2001 From: Oleh Fedorenko Date: Tue, 14 May 2024 14:59:35 +0000 Subject: [PATCH] Fixes #37452 - Support Zeitwerk loader --- .../middleware/keep_current_request_id.rb | 2 +- lib/foreman_tasks/engine.rb | 206 +++++++++--------- .../keep_current_request_id_test.rb | 6 +- 3 files changed, 108 insertions(+), 106 deletions(-) diff --git a/app/lib/actions/middleware/keep_current_request_id.rb b/app/lib/actions/middleware/keep_current_request_id.rb index bb412d2d0..73efd6afb 100644 --- a/app/lib/actions/middleware/keep_current_request_id.rb +++ b/app/lib/actions/middleware/keep_current_request_id.rb @@ -1,6 +1,6 @@ module Actions module Middleware - class KeepCurrentRequestID < Dynflow::Middleware + class KeepCurrentRequestId < Dynflow::Middleware def delay(*args) pass(*args).tap { store_current_request_id } end diff --git a/lib/foreman_tasks/engine.rb b/lib/foreman_tasks/engine.rb index f5bcc7585..c2638339a 100644 --- a/lib/foreman_tasks/engine.rb +++ b/lib/foreman_tasks/engine.rb @@ -16,116 +16,118 @@ class Engine < ::Rails::Engine SETTINGS[:foreman_tasks] = { :assets => { :precompile => assets_to_precompile } } end - initializer 'foreman_tasks.register_plugin', :before => :finisher_hook do |_app| - Foreman::Plugin.register :"foreman-tasks" do - requires_foreman '>= 3.9' - divider :top_menu, :parent => :monitor_menu, :last => true, :caption => N_('Foreman Tasks') - menu :top_menu, :tasks, - :url_hash => { :controller => 'foreman_tasks/tasks', :action => :index }, - :caption => N_('Tasks'), - :parent => :monitor_menu, - :last => true - - menu :top_menu, :recurring_logics, - :url_hash => { :controller => 'foreman_tasks/recurring_logics', :action => :index }, - :caption => N_('Recurring Logics'), - :parent => :monitor_menu, - :last => true - - security_block :foreman_tasks do |_map| - permission :view_foreman_tasks, { :'foreman_tasks/tasks' => [:auto_complete_search, :sub_tasks, :index, :summary, :summary_sub_tasks, :show], - :'foreman_tasks/react' => [:index], - :'foreman_tasks/api/tasks' => [:bulk_search, :show, :index, :summary, :summary_sub_tasks, :details, :sub_tasks] }, :resource_type => 'ForemanTasks::Task' - permission :edit_foreman_tasks, { :'foreman_tasks/tasks' => [:resume, :unlock, :force_unlock, :cancel_step, :cancel, :abort], - :'foreman_tasks/api/tasks' => [:bulk_resume, :bulk_cancel, :bulk_stop] }, :resource_type => 'ForemanTasks::Task' - - permission :create_recurring_logics, {}, :resource_type => 'ForemanTasks::RecurringLogic' - - permission :view_recurring_logics, { :'foreman_tasks/recurring_logics' => [:auto_complete_search, :index, :show], - :'foreman_tasks/api/recurring_logics' => [:index, :show] }, :resource_type => 'ForemanTasks::RecurringLogic' - - permission :edit_recurring_logics, { :'foreman_tasks/recurring_logics' => [:cancel, :enable, :disable, :clear_cancelled], - :'foreman_tasks/api/recurring_logics' => [:cancel, :update, :bulk_destroy] }, :resource_type => 'ForemanTasks::RecurringLogic' - end + initializer 'foreman_tasks.register_plugin', :before => :finisher_hook do |app| + app.reloader.to_prepare do + Foreman::Plugin.register :"foreman-tasks" do + requires_foreman '>= 3.12' + divider :top_menu, :parent => :monitor_menu, :last => true, :caption => N_('Foreman Tasks') + menu :top_menu, :tasks, + :url_hash => { :controller => 'foreman_tasks/tasks', :action => :index }, + :caption => N_('Tasks'), + :parent => :monitor_menu, + :last => true + + menu :top_menu, :recurring_logics, + :url_hash => { :controller => 'foreman_tasks/recurring_logics', :action => :index }, + :caption => N_('Recurring Logics'), + :parent => :monitor_menu, + :last => true + + security_block :foreman_tasks do |_map| + permission :view_foreman_tasks, { :'foreman_tasks/tasks' => [:auto_complete_search, :sub_tasks, :index, :summary, :summary_sub_tasks, :show], + :'foreman_tasks/react' => [:index], + :'foreman_tasks/api/tasks' => [:bulk_search, :show, :index, :summary, :summary_sub_tasks, :details, :sub_tasks] }, :resource_type => 'ForemanTasks::Task' + permission :edit_foreman_tasks, { :'foreman_tasks/tasks' => [:resume, :unlock, :force_unlock, :cancel_step, :cancel, :abort], + :'foreman_tasks/api/tasks' => [:bulk_resume, :bulk_cancel, :bulk_stop] }, :resource_type => 'ForemanTasks::Task' + + permission :create_recurring_logics, {}, :resource_type => 'ForemanTasks::RecurringLogic' + + permission :view_recurring_logics, { :'foreman_tasks/recurring_logics' => [:auto_complete_search, :index, :show], + :'foreman_tasks/api/recurring_logics' => [:index, :show] }, :resource_type => 'ForemanTasks::RecurringLogic' + + permission :edit_recurring_logics, { :'foreman_tasks/recurring_logics' => [:cancel, :enable, :disable, :clear_cancelled], + :'foreman_tasks/api/recurring_logics' => [:cancel, :update, :bulk_destroy] }, :resource_type => 'ForemanTasks::RecurringLogic' + end - add_all_permissions_to_default_roles - - settings do - category(:tasks, N_('Tasks')) do - setting('foreman_tasks_sync_task_timeout', - type: :integer, - description: N_('Number of seconds to wait for synchronous task to finish.'), - default: 120, - full_name: N_('Sync task timeout')) - setting('dynflow_enable_console', - type: :boolean, - description: N_('Enable the dynflow console (/foreman_tasks/dynflow) for debugging'), - default: true, - full_name: N_('Enable dynflow console')) - setting('dynflow_console_require_auth', - type: :boolean, - description: N_('Require user to be authenticated as user with admin rights when accessing dynflow console'), - default: true, - full_name: N_('Require auth for dynflow console')) - setting('foreman_tasks_proxy_action_retry_count', - type: :integer, - description: N_('Number of attempts to start a task on the smart proxy before failing'), - default: 4, - full_name: N_('Proxy action retry count')) - setting('foreman_tasks_proxy_action_retry_interval', - type: :integer, - description: N_('Time in seconds between retries'), - default: 15, - full_name: N_('Proxy action retry interval')) - setting('foreman_tasks_proxy_batch_trigger', - type: :boolean, - description: N_('Allow triggering tasks on the smart proxy in batches'), - default: true, - full_name: N_('Allow proxy batch tasks')) - setting('foreman_tasks_proxy_batch_size', - type: :integer, - description: N_('Number of tasks which should be sent to the smart proxy in one request, if foreman_tasks_proxy_batch_trigger is enabled'), - default: 100, - full_name: N_('Proxy tasks batch size')) - setting('foreman_tasks_troubleshooting_url', - type: :string, - description: N_('Url pointing to the task troubleshooting documentation. '\ - 'It should contain %{label} placeholder, that will be replaced with normalized task label '\ - '(restricted to only alphanumeric characters)). %{version} placeholder is also available.'), - default: nil, - full_name: N_('Tasks troubleshooting URL')) - setting('foreman_tasks_polling_multiplier', - type: :integer, - description: N_('Polling multiplier which is used to multiply the default polling intervals. '\ - 'This can be used to prevent polling too frequently for long running tasks.'), - default: 1, - full_name: N_("Polling intervals multiplier"), - validate: { numericality: { greater_than: 0 } }) + add_all_permissions_to_default_roles + + settings do + category(:tasks, N_('Tasks')) do + setting('foreman_tasks_sync_task_timeout', + type: :integer, + description: N_('Number of seconds to wait for synchronous task to finish.'), + default: 120, + full_name: N_('Sync task timeout')) + setting('dynflow_enable_console', + type: :boolean, + description: N_('Enable the dynflow console (/foreman_tasks/dynflow) for debugging'), + default: true, + full_name: N_('Enable dynflow console')) + setting('dynflow_console_require_auth', + type: :boolean, + description: N_('Require user to be authenticated as user with admin rights when accessing dynflow console'), + default: true, + full_name: N_('Require auth for dynflow console')) + setting('foreman_tasks_proxy_action_retry_count', + type: :integer, + description: N_('Number of attempts to start a task on the smart proxy before failing'), + default: 4, + full_name: N_('Proxy action retry count')) + setting('foreman_tasks_proxy_action_retry_interval', + type: :integer, + description: N_('Time in seconds between retries'), + default: 15, + full_name: N_('Proxy action retry interval')) + setting('foreman_tasks_proxy_batch_trigger', + type: :boolean, + description: N_('Allow triggering tasks on the smart proxy in batches'), + default: true, + full_name: N_('Allow proxy batch tasks')) + setting('foreman_tasks_proxy_batch_size', + type: :integer, + description: N_('Number of tasks which should be sent to the smart proxy in one request, if foreman_tasks_proxy_batch_trigger is enabled'), + default: 100, + full_name: N_('Proxy tasks batch size')) + setting('foreman_tasks_troubleshooting_url', + type: :string, + description: N_('Url pointing to the task troubleshooting documentation. '\ + 'It should contain %{label} placeholder, that will be replaced with normalized task label '\ + '(restricted to only alphanumeric characters)). %{version} placeholder is also available.'), + default: nil, + full_name: N_('Tasks troubleshooting URL')) + setting('foreman_tasks_polling_multiplier', + type: :integer, + description: N_('Polling multiplier which is used to multiply the default polling intervals. '\ + 'This can be used to prevent polling too frequently for long running tasks.'), + default: 1, + full_name: N_("Polling intervals multiplier"), + validate: { numericality: { greater_than: 0 } }) + end end - end - register_graphql_query_field :task, '::Types::Task', :record_field - register_graphql_query_field :tasks, '::Types::Task', :collection_field - register_graphql_query_field :recurring_logic, '::Types::RecurringLogic', :record_field - register_graphql_query_field :recurring_logics, '::Types::RecurringLogic', :collection_field + register_graphql_query_field :task, '::Types::Task', :record_field + register_graphql_query_field :tasks, '::Types::Task', :collection_field + register_graphql_query_field :recurring_logic, '::Types::RecurringLogic', :record_field + register_graphql_query_field :recurring_logics, '::Types::RecurringLogic', :collection_field - register_graphql_mutation_field :cancel_recurring_logic, '::Mutations::RecurringLogics::Cancel' + register_graphql_mutation_field :cancel_recurring_logic, '::Mutations::RecurringLogics::Cancel' - logger :dynflow, :enabled => true - logger :action, :enabled => true + logger :dynflow, :enabled => true + logger :action, :enabled => true - role 'Tasks Manager', [:view_foreman_tasks, :edit_foreman_tasks], - 'Role granting permissions to inspect, cancel, resume and unlock tasks' - role 'Tasks Reader', [:view_foreman_tasks], - 'Role granting permissions to inspect tasks' + role 'Tasks Manager', [:view_foreman_tasks, :edit_foreman_tasks], + 'Role granting permissions to inspect, cancel, resume and unlock tasks' + role 'Tasks Reader', [:view_foreman_tasks], + 'Role granting permissions to inspect tasks' - widget 'foreman_tasks/tasks/dashboard/tasks_status', :sizex => 6, :sizey => 1, :name => N_('Task Status') - widget 'foreman_tasks/tasks/dashboard/latest_tasks_in_error_warning', :sizex => 6, :sizey => 1, :name => N_('Latest Warning/Error Tasks') + widget 'foreman_tasks/tasks/dashboard/tasks_status', :sizex => 6, :sizey => 1, :name => N_('Task Status') + widget 'foreman_tasks/tasks/dashboard/latest_tasks_in_error_warning', :sizex => 6, :sizey => 1, :name => N_('Latest Warning/Error Tasks') - register_gettext domain: "foreman_tasks" + register_gettext domain: "foreman_tasks" - ForemanTasks.dynflow.eager_load_actions! - extend_observable_events(::Dynflow::Action.descendants.select { |klass| klass <= ::Actions::ObservableAction }.map(&:namespaced_event_names)) + ForemanTasks.dynflow.eager_load_actions! + extend_observable_events(::Dynflow::Action.descendants.select { |klass| klass <= ::Actions::ObservableAction }.map(&:namespaced_event_names)) + end end end @@ -160,7 +162,7 @@ class Engine < ::Rails::Engine world.middleware.use Actions::Middleware::KeepCurrentTaxonomies world.middleware.use Actions::Middleware::KeepCurrentUser, :before => ::Dynflow::Middleware::Common::Transaction world.middleware.use Actions::Middleware::KeepCurrentTimezone - world.middleware.use Actions::Middleware::KeepCurrentRequestID + world.middleware.use Actions::Middleware::KeepCurrentRequestId world.middleware.use ::Actions::Middleware::LoadSettingValues ForemanTasks.register_scheduled_task(Actions::CheckLongRunningTasks, ENV['FOREMAN_TASKS_CHECK_LONG_RUNNING_TASKS_CRONLINE'] || '0 0 * * *') end diff --git a/test/lib/actions/middleware/keep_current_request_id_test.rb b/test/lib/actions/middleware/keep_current_request_id_test.rb index e7c3f9f8f..d702eaade 100644 --- a/test/lib/actions/middleware/keep_current_request_id_test.rb +++ b/test/lib/actions/middleware/keep_current_request_id_test.rb @@ -1,9 +1,9 @@ require 'foreman_tasks_test_helper' module ForemanTasks - class KeepCurrentRequestIDTest < ActiveSupport::TestCase + class KeepCurrentRequestIdTest < ActiveSupport::TestCase class DummyAction < Actions::EntryAction - middleware.use ::Actions::Middleware::KeepCurrentRequestID + middleware.use ::Actions::Middleware::KeepCurrentRequestId def plan(plan = false) plan_self if plan @@ -18,7 +18,7 @@ def finalize end end - describe Actions::Middleware::KeepCurrentRequestID do + describe Actions::Middleware::KeepCurrentRequestId do include ::Dynflow::Testing before { @old_id = ::Logging.mdc['request'] }