]> git.openstreetmap.org Git - rails.git/commitdiff
Sketch out how to use the jobs queue for trace insertion and deletion
authorAndy Allan <git@gravitystorm.co.uk>
Wed, 31 Oct 2018 16:51:30 +0000 (17:51 +0100)
committerAndy Allan <git@gravitystorm.co.uk>
Wed, 16 Jan 2019 09:49:11 +0000 (10:49 +0100)
Refs #1852

app/controllers/traces_controller.rb
app/jobs/application_job.rb [new file with mode: 0644]
app/jobs/trace_destroyer_job.rb [new file with mode: 0644]
app/jobs/trace_importer_job.rb [new file with mode: 0644]
lib/daemons/gpx_import.rb [deleted file]
lib/daemons/gpx_import_ctl [deleted file]
test/jobs/trace_destroyer_job_test.rb [new file with mode: 0644]
test/jobs/trace_importer_job_test.rb [new file with mode: 0644]

index 253bc4160aed3295937cd41227910e04c8b14cdf..d06c05b2064ab9514bf96d8f8d333188ee7cfff0 100644 (file)
@@ -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
 
         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?
         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"
       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
       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 (file)
index 0000000..d394c3d
--- /dev/null
@@ -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 (file)
index 0000000..ff5da5b
--- /dev/null
@@ -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 (file)
index 0000000..d41de58
--- /dev/null
@@ -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 (executable)
index a0344b5..0000000
+++ /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 (executable)
index 495ce1f..0000000
+++ /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 (file)
index 0000000..0c62716
--- /dev/null
@@ -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 (file)
index 0000000..9de0f64
--- /dev/null
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class TraceImporterJobTest < ActiveJob::TestCase
+  # test "the truth" do
+  #   assert true
+  # end
+end