From 39e997d0ca8ad2d41f7c361f458c08f669c679dd Mon Sep 17 00:00:00 2001 From: f Date: Tue, 28 Mar 2023 20:21:52 -0300 Subject: [PATCH 1/8] BREAKING CHANGE: deprecar sucker punch en favor de que --- Gemfile | 2 +- Gemfile.lock | 8 ++------ app/jobs/application_job.rb | 2 +- config/environments/production.rb | 2 +- config/initializers/sucker_punch.rb | 6 ------ db/migrate/20230328231029_create_que_tables.rb | 12 ++++++++++++ 6 files changed, 17 insertions(+), 15 deletions(-) delete mode 100644 config/initializers/sucker_punch.rb create mode 100644 db/migrate/20230328231029_create_que_tables.rb diff --git a/Gemfile b/Gemfile index f3799638..7ff904dd 100644 --- a/Gemfile +++ b/Gemfile @@ -68,7 +68,7 @@ gem 'rollups', git: 'https://github.com/fauno/rollup.git', branch: 'update' gem 'rubyzip' gem 'rugged' gem 'concurrent-ruby-ext' -gem 'sucker_punch' +gem 'que' gem 'symbol-fstring', require: 'fstring/all' gem 'terminal-table' gem 'validates_hostname' diff --git a/Gemfile.lock b/Gemfile.lock index abaf45c9..ac8f6870 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -119,9 +119,6 @@ GEM colorator (1.1.0) commonmarker (0.21.2-x86_64-linux-musl) ruby-enum (~> 0.5) - concurrent-ruby (1.1.9) - concurrent-ruby-ext (1.1.9-x86_64-linux-musl) - concurrent-ruby (= 1.1.9) crass (1.0.6) database_cleaner (2.0.1) database_cleaner-active_record (~> 2.0.0) @@ -374,6 +371,7 @@ GEM pundit (2.1.1) activesupport (>= 3.0.0) racc (1.6.0-x86_64-linux-musl) + que (2.2.0) rack (2.2.3) rack-cors (1.1.1) rack (>= 2.0.0) @@ -510,8 +508,6 @@ GEM sprockets (>= 3.0.0) sqlite3 (1.4.2-x86_64-linux-musl) stackprof (0.2.17-x86_64-linux-musl) - sucker_punch (3.0.1) - concurrent-ruby (~> 1.0) sutty-archives (2.5.4) jekyll (>= 3.6, < 5.0) sutty-liquid (0.7.4) @@ -619,6 +615,7 @@ DEPENDENCIES pry puma pundit + que rack-cors rack-mini-profiler rails (~> 6) @@ -638,7 +635,6 @@ DEPENDENCIES spring-watcher-listen (~> 2.0.0) sqlite3 stackprof - sucker_punch sutty-liquid (>= 0.7.3) symbol-fstring terminal-table diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb index f3673f0a..06690c53 100644 --- a/app/jobs/application_job.rb +++ b/app/jobs/application_job.rb @@ -2,7 +2,7 @@ # Base para trabajos class ApplicationJob < ActiveJob::Base - include SuckerPunch::Job + include Que::ActiveJob::JobExtensions private diff --git a/config/environments/production.rb b/config/environments/production.rb index d121bdbd..ed737146 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -66,7 +66,7 @@ Rails.application.configure do # Use a real queuing backend for Active Job (and separate queues per # environment) - config.active_job.queue_adapter = :sucker_punch + config.active_job.queue_adapter = :que config.active_job.queue_name_prefix = "sutty_#{Rails.env}" config.action_mailer.perform_caching = false diff --git a/config/initializers/sucker_punch.rb b/config/initializers/sucker_punch.rb deleted file mode 100644 index 865af32d..00000000 --- a/config/initializers/sucker_punch.rb +++ /dev/null @@ -1,6 +0,0 @@ -# frozen_string_literal: true - -# Enviar una notificación cuando falla una tarea -SuckerPunch.exception_handler = lambda { |ex, _klass, _args| - ExceptionNotifier.notify_exception(ex) -} diff --git a/db/migrate/20230328231029_create_que_tables.rb b/db/migrate/20230328231029_create_que_tables.rb new file mode 100644 index 00000000..1ed929f7 --- /dev/null +++ b/db/migrate/20230328231029_create_que_tables.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +# Que +class CreateQueTables < ActiveRecord::Migration[6.1] + def up + Que.migrate! version: 7 + end + + def down + Que.migrate! version: 0 + end +end From cfc85e6356384e8e5f118badc677aa76fec217fa Mon Sep 17 00:00:00 2001 From: f Date: Tue, 28 Mar 2023 21:15:55 -0300 Subject: [PATCH 2/8] feat: iniciar que en segundo plano --- Procfile | 1 + config/application.rb | 7 +++++++ config/environments/production.rb | 5 ----- monit.conf | 4 ++++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Procfile b/Procfile index 45fe1df7..84842dff 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1,3 @@ cleanup: bundle exec rake cleanup:everything stats: bundle exec rake stats:process_all +que: daemonize -c /srv/ -p /srv/tmp/que.pid -u rails /usr/local/bin/syslogize bundle exec que diff --git a/config/application.rb b/config/application.rb index 97ab244c..86265b24 100644 --- a/config/application.rb +++ b/config/application.rb @@ -38,6 +38,13 @@ module Sutty config.active_storage.variant_processor = :vips + # Que + config.action_mailer.deliver_later_queue_name = :default + config.active_storage.queues.analysis = :default + config.active_storage.queues.purge = :default + config.active_job.queue_adapter = :que + config.active_job.queue_name_prefix = "sutty_#{Rails.env}" + config.to_prepare do # Load application's model / class decorators Dir.glob(File.join(File.dirname(__FILE__), '..', 'app', '**', '*_decorator.rb')).sort.each do |c| diff --git a/config/environments/production.rb b/config/environments/production.rb index ed737146..f13488b8 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -64,11 +64,6 @@ Rails.application.configure do # Use a different cache store in production. config.cache_store = :redis_cache_store, { url: ENV['REDIS_SERVER'] } - # Use a real queuing backend for Active Job (and separate queues per - # environment) - config.active_job.queue_adapter = :que - config.active_job.queue_name_prefix = "sutty_#{Rails.env}" - config.action_mailer.perform_caching = false # Ignore bad email addresses and do not raise email delivery errors. diff --git a/monit.conf b/monit.conf index 39f45d6d..4c7b345a 100644 --- a/monit.conf +++ b/monit.conf @@ -13,3 +13,7 @@ check program stats with path "/usr/bin/foreman run -f /srv/Procfile -d /srv stats" as uid "rails" gid "www-data" every "0 1 * * *" if status != 0 then alert + +check process que with pidfile /srv/tmp/que.pid + start program = "/usr/bin/foreman run -f /srv/Procfile -d /srv que" + stop program = "/bin/sh -c 'cat /srv/tmp/que.pid | xargs -r kill'" From d5db27ac95e296d2c9a9503756276cfef4b37a8d Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 12:34:57 -0300 Subject: [PATCH 3/8] fix: sin prefijo para las colas --- config/application.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/config/application.rb b/config/application.rb index 86265b24..e02ddc8b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -43,7 +43,6 @@ module Sutty config.active_storage.queues.analysis = :default config.active_storage.queues.purge = :default config.active_job.queue_adapter = :que - config.active_job.queue_name_prefix = "sutty_#{Rails.env}" config.to_prepare do # Load application's model / class decorators From fd56ee4c3ece7d4aa26b070b1494423c1f848388 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:35:00 -0300 Subject: [PATCH 4/8] fix: usar perform_later --- app/controllers/api/v1/contact_controller.rb | 2 +- app/jobs/maintenance_job.rb | 2 +- app/lib/exception_notifier/gitlab_notifier.rb | 2 +- app/models/log_entry.rb | 2 +- app/services/site_service.rb | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/contact_controller.rb b/app/controllers/api/v1/contact_controller.rb index deacf4a7..d949dc30 100644 --- a/app/controllers/api/v1/contact_controller.rb +++ b/app/controllers/api/v1/contact_controller.rb @@ -18,7 +18,7 @@ module Api # Si todo salió bien, enviar los correos y redirigir al sitio. # El sitio nos dice a dónde tenemos que ir. - ContactJob.perform_async site.id, + ContactJob.perform_later site.id, params[:form], contact_params.to_h.symbolize_keys, params[:redirect] diff --git a/app/jobs/maintenance_job.rb b/app/jobs/maintenance_job.rb index 4c411d0e..c7a962f9 100644 --- a/app/jobs/maintenance_job.rb +++ b/app/jobs/maintenance_job.rb @@ -10,7 +10,7 @@ # bundle exec rails c # m = Maintenance.create message_en: 'reason', message_es: 'razón', # estimated_from: Time.now, estimated_to: Time.now + 1.hour -# MaintenanceJob.perform_async(maintenance_id: m.id) +# MaintenanceJob.perform_later(maintenance_id: m.id) # # Lo mismo para salir de mantenimiento, agregando el atributo # are_we_back: true al crear el Maintenance. diff --git a/app/lib/exception_notifier/gitlab_notifier.rb b/app/lib/exception_notifier/gitlab_notifier.rb index 18bfc6d4..b596bcf4 100644 --- a/app/lib/exception_notifier/gitlab_notifier.rb +++ b/app/lib/exception_notifier/gitlab_notifier.rb @@ -11,7 +11,7 @@ module ExceptionNotifier # @param [Exception] # @param [Hash] def call(exception, **options) - GitlabNotifierJob.perform_async(exception, **options) + GitlabNotifierJob.perform_later(exception, **options) end end end diff --git a/app/models/log_entry.rb b/app/models/log_entry.rb index 1824da55..9685e0d0 100644 --- a/app/models/log_entry.rb +++ b/app/models/log_entry.rb @@ -11,7 +11,7 @@ class LogEntry < ApplicationRecord def resend return if sent - ContactJob.perform_async site_id, params[:form], params + ContactJob.perform_later site_id, params[:form], params end def params diff --git a/app/services/site_service.rb b/app/services/site_service.rb index f5f415e7..3d971b89 100644 --- a/app/services/site_service.rb +++ b/app/services/site_service.rb @@ -5,7 +5,7 @@ SiteService = Struct.new(:site, :usuarie, :params, keyword_init: true) do def deploy site.enqueue! - DeployJob.perform_async site.id + DeployJob.perform_later site.id end # Crea un sitio, agrega un rol nuevo y guarda los cambios a la From fff499d1f823345f6dfd258730633124be0ab787 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:40:57 -0300 Subject: [PATCH 5/8] fix: correr todo en la misma cola --- app/jobs/backtrace_job.rb | 2 -- app/jobs/gitlab_notifier_job.rb | 2 -- 2 files changed, 4 deletions(-) diff --git a/app/jobs/backtrace_job.rb b/app/jobs/backtrace_job.rb index 86a9b2a6..97e6007b 100644 --- a/app/jobs/backtrace_job.rb +++ b/app/jobs/backtrace_job.rb @@ -6,8 +6,6 @@ class BacktraceJob < ApplicationJob EMPTY_SOURCEMAP = { 'mappings' => '' }.freeze - queue_as :low_priority - attr_reader :params, :site_id def perform(site_id:, params:) diff --git a/app/jobs/gitlab_notifier_job.rb b/app/jobs/gitlab_notifier_job.rb index 701c6789..cefe02ab 100644 --- a/app/jobs/gitlab_notifier_job.rb +++ b/app/jobs/gitlab_notifier_job.rb @@ -10,8 +10,6 @@ class GitlabNotifierJob < ApplicationJob # Variables que vamos a acceder luego attr_reader :exception, :options, :issue_data, :cached - queue_as :low_priority - # @param [Exception] la excepción lanzada # @param [Hash] opciones de ExceptionNotifier def perform(exception, **options) From 71afdc16ae8565323788f3e8e95e1d91f62bca36 Mon Sep 17 00:00:00 2001 From: f Date: Wed, 29 Mar 2023 15:41:11 -0300 Subject: [PATCH 6/8] fix: aumentar la prioridad de las publicaciones --- app/jobs/deploy_job.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/jobs/deploy_job.rb b/app/jobs/deploy_job.rb index e018533c..b3cb2b67 100644 --- a/app/jobs/deploy_job.rb +++ b/app/jobs/deploy_job.rb @@ -7,6 +7,9 @@ class DeployJob < ApplicationJob discard_on ActiveRecord::RecordNotFound + # Lanzar lo antes posible + self.priority = 10 + # rubocop:disable Metrics/MethodLength def perform(site, notify: true, time: Time.now, output: false) @output = output From d3166f2c5a27bf4d1602ac92273236ccc92019ab Mon Sep 17 00:00:00 2001 From: f Date: Thu, 30 Mar 2023 13:07:25 -0300 Subject: [PATCH 7/8] feat: poder serializar las excepciones --- config/initializers/que.rb | 8 +++++++ .../serializers/exception_serializer.rb | 22 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 config/initializers/que.rb create mode 100644 lib/active_job/serializers/exception_serializer.rb diff --git a/config/initializers/que.rb b/config/initializers/que.rb new file mode 100644 index 00000000..579f57d5 --- /dev/null +++ b/config/initializers/que.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +ActiveJob::Serializers.add_serializers ActiveJob::Serializers::ExceptionSerializer + +# Notificar los errores +Que.error_notifier = proc do |error, job| + ExceptionNotifier.notify_exception(error, data: job) +end diff --git a/lib/active_job/serializers/exception_serializer.rb b/lib/active_job/serializers/exception_serializer.rb new file mode 100644 index 00000000..42b55835 --- /dev/null +++ b/lib/active_job/serializers/exception_serializer.rb @@ -0,0 +1,22 @@ +# frozen_string_literal: true + +require 'json/add/exception' + +module ActiveJob + module Serializers + class ExceptionSerializer < ObjectSerializer # :nodoc: + def serialize(ex) + super('value' => { 'class' => ex.class.name, 'exception' => ex.as_json }) + end + + def deserialize(hash) + hash.dig('value', 'class').constantize.json_create(hash.dig('value', 'exception')) + end + + private + def klass + Exception + end + end + end +end From 691b87b9116a623add90f4add0d0d03e7378ffa0 Mon Sep 17 00:00:00 2001 From: f Date: Thu, 30 Mar 2023 13:11:37 -0300 Subject: [PATCH 8/8] fixup! feat: poder serializar las excepciones --- {lib => app/lib}/active_job/serializers/exception_serializer.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename {lib => app/lib}/active_job/serializers/exception_serializer.rb (100%) diff --git a/lib/active_job/serializers/exception_serializer.rb b/app/lib/active_job/serializers/exception_serializer.rb similarity index 100% rename from lib/active_job/serializers/exception_serializer.rb rename to app/lib/active_job/serializers/exception_serializer.rb