Add reverse geocoding job to background jobs controller

This commit is contained in:
Eugene Burmakin 2024-07-09 23:50:19 +02:00
parent b648c18f9d
commit ac36a505dd
10 changed files with 106 additions and 13 deletions

File diff suppressed because one or more lines are too long

View file

@ -16,11 +16,23 @@ class Settings::BackgroundJobsController < ApplicationController
def edit; end def edit; end
def create def create
EnqueueReverseGeocodingJob.perform_later(params[:job_name], current_user.id)
flash.now[:notice] = 'Job was successfully created.'
redirect_to settings_background_jobs_path, notice: 'Job was successfully created.'
end end
def update def update
end end
def destroy def destroy
# Clear all jobs in the queue, params[:id] contains queue name
queue = Sidekiq::Queue.new(params[:id])
queue.clear
flash.now[:notice] = 'Queue was successfully cleared.'
redirect_to settings_background_jobs_path, notice: 'Queue was successfully cleared.'
end end
end end

View file

@ -0,0 +1,9 @@
# frozen_string_literal: true
class EnqueueReverseGeocodingJob < ApplicationJob
queue_as :reverse_geocoding
def perform(job_name, user_id)
Jobs::Create.new(job_name, user_id).call
end
end

View file

@ -14,6 +14,9 @@ class Point < ApplicationRecord
}, _suffix: true }, _suffix: true
enum connection: { mobile: 0, wifi: 1, offline: 2 }, _suffix: true enum connection: { mobile: 0, wifi: 1, offline: 2 }, _suffix: true
scope :reverse_geocoded, -> { where.not(city: nil, country: nil) }
scope :not_reverse_geocoded, -> { where(city: nil, country: nil) }
after_create :async_reverse_geocode after_create :async_reverse_geocode
def self.without_raw_data def self.without_raw_data
@ -24,8 +27,6 @@ class Point < ApplicationRecord
Time.zone.at(timestamp) Time.zone.at(timestamp)
end end
private
def async_reverse_geocode def async_reverse_geocode
return unless REVERSE_GEOCODING_ENABLED return unless REVERSE_GEOCODING_ENABLED

View file

@ -0,0 +1,25 @@
# frozen_string_literal: true
class Jobs::Create
class InvalidJobName < StandardError; end
attr_reader :job_name, :user
def initialize(job_name, user_id)
@job_name = job_name
@user = User.find(user_id)
end
def call
points =
case job_name
when 'start_reverse_geocoding'
user.tracked_points
when 'continue_reverse_geocoding'
user.tracked_points.not_reverse_geocoded
else
raise InvalidJobName, 'Invalid job name'
end
points.each(&:async_reverse_geocode)
end
end

View file

@ -1,4 +1,4 @@
<div role="<%= notification.kind %>" class="<%= notification.kind %> shadow-lg p-5 flex justify-between items-center mb-4 rounded-lg bg-neutral" id="<%= dom_id notification %>"> <div role="<%= notification.kind %>" class="<%= notification.kind %> shadow-lg p-5 flex justify-between items-center mb-4 rounded-lg bg-base-200" id="<%= dom_id notification %>">
<div class="flex-1"> <div class="flex-1">
<h3 class="font-bold text-xl"> <h3 class="font-bold text-xl">
<%= link_to notification.title, notification, class: 'link hover:no-underline text-blue-600' %> <%= link_to notification.title, notification, class: 'link hover:no-underline text-blue-600' %>

View file

@ -0,0 +1,4 @@
<div role="tablist" class="tabs tabs-lifted tabs-lg">
<%= link_to 'Main', settings_path, role: 'tab', class: "tab #{active_tab?(settings_path)}" %>
<%= link_to 'Background Jobs', settings_background_jobs_path, role: 'tab', class: "tab #{active_tab?(settings_background_jobs_path)}" %>
</div>

View file

@ -1,16 +1,56 @@
<% content_for :title, "Background jobs" %> <% content_for :title, "Background jobs" %>
<div class="w-full">
<div class="flex justify-between items-center"> <div class="min-h-content w-full">
<%= render 'settings/navigation' %>
<div class="flex justify-between items-center mt-5">
<h1 class="font-bold text-4xl">Background jobs</h1> <h1 class="font-bold text-4xl">Background jobs</h1>
</div> </div>
<div role="alert" class="alert my-5">
<svg
xmlns="http://www.w3.org/2000/svg"
fill="none"
viewBox="0 0 24 24"
class="stroke-info h-6 w-6 shrink-0">
<path
stroke-linecap="round"
stroke-linejoin="round"
stroke-width="2"
d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
</svg>
<span>Spamming new many jobs at once is a bad idea. Let them work or clear the queue beforehand.</span>
</div>
<div class='flex'>
<div class="card bg-base-300 w-96 shadow-xl m-5">
<div class="card-body">
<h2 class="card-title">Start Reverse Geocoding</h2>
<p>This job will re-run reverse geocoding process for all the points in your database. Might take a few days or even weeks based on the amount of points you have!</p>
<div class="card-actions justify-end">
<%= link_to 'Start Job', settings_background_jobs_path(job_name: 'start_reverse_geocoding'), method: :post, data: { confirm: 'Are you sure?', turbo_confirm: 'Are you sure?', turbo_method: :post }, class: 'btn btn-primary' %>
</div>
</div>
</div>
<div class="card bg-base-300 w-96 shadow-xl m-5">
<div class="card-body">
<h2 class="card-title">Continue Reverse Geocoding</h2>
<p>This job will process reverse geocoding for all points that don't have geocoding data yet.</p>
<div class="card-actions justify-end">
<%= link_to 'Start Job', settings_background_jobs_path(job_name: 'continue_reverse_geocoding'), method: :post, data: { confirm: 'Are you sure?', turbo_confirm: 'Are you sure?', turbo_method: :post }, class: 'btn btn-primary' %>
</div>
</div>
</div>
</div>
<div id="settings_background_jobs" class="min-w-full"> <div id="settings_background_jobs" class="min-w-full">
<% @queues.each do |queue| %> <% @queues.each do |queue| %>
<div class="card shadow-2xl bg-base-100 p-5 my-5"> <div class="card shadow-2xl bg-base-300 p-5 my-5">
<h2 class="text-2xl font-bold"><%= queue.name %></h2> <h2 class="text-2xl font-bold"><%= queue.name %></h2>
<div class="flex justify-between items-center"> <div class="flex justify-between items-center">
<p class="text-lg">Jobs in queue: <%= queue.size %></p> <p class="text-lg">Jobs in queue: <%= queue.size %></p>
<a href="<%= 'settings_background_jobs_clear_queue_path(queue)' %>" class="btn btn-primary">Clear queue</a> <%= link_to 'Clear queue', settings_background_job_path(queue.name), method: :delete, data: { confirm: 'Are you sure?', turbo_confirm: 'Are you sure?', turbo_method: :delete }, class: 'btn btn-primary' %>
</div> </div>
</div> </div>
<% end %> <% end %>

View file

@ -1,10 +1,7 @@
<% content_for :title, 'Settings' %> <% content_for :title, 'Settings' %>
<div class="min-h-content bg-base-200 w-full"> <div class="min-h-content w-full">
<div role="tablist" class="tabs tabs-lifted tabs-lg"> <%= render 'settings/navigation' %>
<%= link_to 'Main', settings_path, role: 'tab', class: "tab #{active_tab?(settings_path)}" %>
<%= link_to 'Background Jobs', settings_background_jobs_path, role: 'tab', class: "tab #{active_tab?(settings_background_jobs_path)}" %>
</div>
<div class="flex flex-col lg:flex-row w-full my-10 space-x-4"> <div class="flex flex-col lg:flex-row w-full my-10 space-x-4">
<div class="card flex-shrink-0 w-full max-w-sm shadow-2xl bg-base-100 px-5 py-5 mx-5"> <div class="card flex-shrink-0 w-full max-w-sm shadow-2xl bg-base-100 px-5 py-5 mx-5">

View file

@ -0,0 +1,5 @@
require 'rails_helper'
RSpec.describe EnqueueReverseGeocodingJob, type: :job do
pending "add some examples to (or delete) #{__FILE__}"
end