Skip to content

Commit

Permalink
Sketch out how to use the jobs queue for trace insertion and deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
gravitystorm committed Jan 16, 2019
1 parent fbbabef commit e59f1b6
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 82 deletions.
2 changes: 2 additions & 0 deletions app/controllers/traces_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def create
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?
Expand Down Expand Up @@ -210,6 +211,7 @@ def delete
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
Expand Down
7 changes: 7 additions & 0 deletions app/jobs/application_job.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions app/jobs/trace_destroyer_job.rb
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions app/jobs/trace_importer_job.rb
Original file line number Diff line number Diff line change
@@ -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
59 changes: 0 additions & 59 deletions lib/daemons/gpx_import.rb

This file was deleted.

23 changes: 0 additions & 23 deletions lib/daemons/gpx_import_ctl

This file was deleted.

7 changes: 7 additions & 0 deletions test/jobs/trace_destroyer_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TraceDestroyerJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/jobs/trace_importer_job_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class TraceImporterJobTest < ActiveJob::TestCase
# test "the truth" do
# assert true
# end
end

0 comments on commit e59f1b6

Please sign in to comment.