From 1aeb0a9ba53832d02a30d469e06be5d232371142 Mon Sep 17 00:00:00 2001 From: Thorsten Eckel Date: Fri, 20 Mar 2020 06:11:50 +0100 Subject: [PATCH] Performance: Parallelize Session.jobs if ENV 'ZAMMAD_SESSION_JOBS_CONCURRENT' is provided with number of dedicated worker node processes to spawn. --- .rubocop_todo.yml | 2 +- lib/sessions.rb | 34 ++++++++++++++++++++++++++++++++-- script/scheduler.rb | 10 +++++++++- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 1431a0e63..8f398071e 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -42,7 +42,7 @@ Metrics/CyclomaticComplexity: # Offense count: 27 # Configuration parameters: CountComments. Metrics/ModuleLength: - Max: 559 + Max: 578 # Offense count: 274 Metrics/PerceivedComplexity: diff --git a/lib/sessions.rb b/lib/sessions.rb index f4f1d29e7..f139d64d0 100644 --- a/lib/sessions.rb +++ b/lib/sessions.rb @@ -616,11 +616,41 @@ delete spool messages # just make sure that spool path exists if !File.exist?(@path) - FileUtils.mkpath @path + FileUtils.mkpath(@path) end # dispatch sessions - if node_id&.zero? + if node_id.blank? && ENV['ZAMMAD_SESSION_JOBS_CONCURRENT'].to_i.positive? + + dispatcher_pid = Process.pid + node_count = ENV['ZAMMAD_SESSION_JOBS_CONCURRENT'].to_i + node_pids = [] + (1..node_count).each do |worker_node_id| + node_pids << fork do + title = "Zammad Session Jobs Node ##{worker_node_id}: dispatch_pid:#{dispatcher_pid} -> worker_pid:#{Process.pid}" + $PROGRAM_NAME = title + + log('info', "#{title} started.") + + ::Sessions.jobs(worker_node_id) + sleep node_count + rescue Interrupt # rubocop:disable Layout/RescueEnsureAlignment + nil + end + end + + Signal.trap 'SIGTERM' do + + node_pids.each do |node_pid| + Process.kill 'TERM', node_pid + end + + Process.waitall + + raise SignalException, 'SIGTERM' + end + + # displatch client_ids to nodes loop do # nodes diff --git a/script/scheduler.rb b/script/scheduler.rb index c92a95efa..c85b15615 100755 --- a/script/scheduler.rb +++ b/script/scheduler.rb @@ -59,7 +59,15 @@ Daemons.run_proc('scheduler', daemon_options) do Rails.logger.info 'Scheduler started.' at_exit do - Rails.logger.info 'Scheduler stopped.' + + # use process title for stop log entry + # if differs from default process title + title = 'Scheduler' + if $PROGRAM_NAME != 'scheduler.rb' + title = $PROGRAM_NAME + end + + Rails.logger.info "#{title} stopped." end begin