From: Andy Allan Date: Wed, 31 Oct 2018 16:51:30 +0000 (+0100) Subject: Sketch out how to use the jobs queue for trace insertion and deletion X-Git-Tag: live~3251^2~4 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/e59f1b610817ad8442bc639d479cebe0a851c05a?ds=sidebyside Sketch out how to use the jobs queue for trace insertion and deletion Refs #1852 --- diff --git a/app/controllers/traces_controller.rb b/app/controllers/traces_controller.rb index 253bc4160..d06c05b20 100644 --- a/app/controllers/traces_controller.rb +++ b/app/controllers/traces_controller.rb @@ -127,6 +127,7 @@ class TracesController < ApplicationController flash[:notice] = t ".trace_uploaded" flash[:warning] = t ".traces_waiting", :count => current_user.traces.where(:inserted => false).count if current_user.traces.where(:inserted => false).count > 4 + TraceImporterJob.perform_later(@trace) redirect_to :action => :index, :display_name => current_user.display_name else flash[:error] = t("traces.create.upload_failed") if @trace.valid? @@ -210,6 +211,7 @@ class TracesController < ApplicationController trace.visible = false trace.save flash[:notice] = t ".scheduled_for_deletion" + TraceDestroyerJob.perform_later(trace) redirect_to :action => :index, :display_name => trace.user.display_name end rescue ActiveRecord::RecordNotFound diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb new file mode 100644 index 000000000..d394c3d10 --- /dev/null +++ b/app/jobs/application_job.rb @@ -0,0 +1,7 @@ +class ApplicationJob < ActiveJob::Base + # Automatically retry jobs that encountered a deadlock + # retry_on ActiveRecord::Deadlocked + + # Most jobs are safe to ignore if the underlying records are no longer available + # discard_on ActiveJob::DeserializationError +end diff --git a/app/jobs/trace_destroyer_job.rb b/app/jobs/trace_destroyer_job.rb new file mode 100644 index 000000000..ff5da5b0a --- /dev/null +++ b/app/jobs/trace_destroyer_job.rb @@ -0,0 +1,10 @@ +class TraceDestroyerJob < ApplicationJob + queue_as :default + + def perform(trace) + trace.destroy + rescue StandardError => ex + logger.info ex.to_s + ex.backtrace.each { |l| logger.info l } + end +end diff --git a/app/jobs/trace_importer_job.rb b/app/jobs/trace_importer_job.rb new file mode 100644 index 000000000..d41de5836 --- /dev/null +++ b/app/jobs/trace_importer_job.rb @@ -0,0 +1,19 @@ +class TraceImporterJob < ApplicationJob + queue_as :default + + def perform(trace) + gpx = trace.import + + if gpx.actual_points.positive? + Notifier.gpx_success(trace, gpx.actual_points).deliver_later + else + Notifier.gpx_failure(trace, "0 points parsed ok. Do they all have lat,lng,alt,timestamp?").deliver_later + trace.destroy + end + rescue StandardError => ex + logger.info ex.to_s + ex.backtrace.each { |l| logger.info l } + Notifier.gpx_failure(trace, ex.to_s + "\n" + ex.backtrace.join("\n")).deliver_later + trace.destroy + end +end diff --git a/lib/daemons/gpx_import.rb b/lib/daemons/gpx_import.rb deleted file mode 100755 index a0344b58c..000000000 --- a/lib/daemons/gpx_import.rb +++ /dev/null @@ -1,59 +0,0 @@ -#!/usr/bin/env ruby - -# You might want to change this -# ENV["RAILS_ENV"] ||= "development" - -require File.dirname(__FILE__) + "/../../config/environment" - -terminated = false - -logger = ActiveRecord::Base.logger - -loop do - ActiveRecord::Base.logger.info("GPX Import daemon wake @ #{Time.now}.") - - Trace.find(:all, :conditions => { :inserted => false, :visible => true }, :order => "id").each do |trace| - Signal.trap("TERM") do - terminated = true - end - - begin - gpx = trace.import - - if gpx.actual_points.positive? - Notifier.gpx_success(trace, gpx.actual_points).deliver - else - Notifier.gpx_failure(trace, "0 points parsed ok. Do they all have lat,lng,alt,timestamp?").deliver - trace.destroy - end - rescue StandardError => ex - logger.info ex.to_s - ex.backtrace.each { |l| logger.info l } - Notifier.gpx_failure(trace, ex.to_s + "\n" + ex.backtrace.join("\n")).deliver - trace.destroy - end - - Signal.trap("TERM", "DEFAULT") - - exit if terminated - end - - Trace.find(:all, :conditions => { :visible => false }, :order => "id").each do |trace| - Signal.trap("TERM") do - terminated = true - end - - begin - trace.destroy - rescue StandardError => ex - logger.info ex.to_s - ex.backtrace.each { |l| logger.info l } - end - - Signal.trap("TERM", "DEFAULT") - - exit if terminated - end - - sleep 5.minutes.value -end diff --git a/lib/daemons/gpx_import_ctl b/lib/daemons/gpx_import_ctl deleted file mode 100755 index 495ce1fb1..000000000 --- a/lib/daemons/gpx_import_ctl +++ /dev/null @@ -1,23 +0,0 @@ -#!/usr/bin/env ruby -require "rubygems" -require "daemons" -require "yaml" -require "erb" - -class Hash - def with_symbols! - each_key { |key| self[key.to_s.to_sym] = self[key] } - self - end -end - -options = YAML.safe_load( - ERB.new( - IO.read( - File.dirname(__FILE__) + "/../../config/daemons.yml" - ) - ).result -).with_symbols! -options[:dir_mode] = options[:dir_mode].to_sym - -Daemons.run File.dirname(__FILE__) + "/gpx_import.rb", options diff --git a/test/jobs/trace_destroyer_job_test.rb b/test/jobs/trace_destroyer_job_test.rb new file mode 100644 index 000000000..0c62716a0 --- /dev/null +++ b/test/jobs/trace_destroyer_job_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TraceDestroyerJobTest < ActiveJob::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/jobs/trace_importer_job_test.rb b/test/jobs/trace_importer_job_test.rb new file mode 100644 index 000000000..9de0f64e0 --- /dev/null +++ b/test/jobs/trace_importer_job_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TraceImporterJobTest < ActiveJob::TestCase + # test "the truth" do + # assert true + # end +end