Move import job scheduling to controller from model callback

This commit is contained in:
Eugene Burmakin 2024-04-06 22:31:33 +02:00
parent bd5259ddb1
commit ce93aa159e
15 changed files with 67 additions and 37 deletions

View file

@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/) The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/). and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.1.6] - 2024-04-06 ## [0.1.6] 2024-04-06
You can now use [Overland](https://overland.p3k.app/) mobile app to track your location. You can now use [Overland](https://overland.p3k.app/) mobile app to track your location.
@ -17,7 +17,7 @@ You can now use [Overland](https://overland.p3k.app/) mobile app to track your l
### Fixed ### Fixed
## [0.1.5] - 2024-04-05 ## [0.1.5] 2024-04-05
You can now specify the host of the application by setting the `APPLICATION_HOST` environment variable in the `docker-compose.yml` file. You can now specify the host of the application by setting the `APPLICATION_HOST` environment variable in the `docker-compose.yml` file.

View file

@ -15,13 +15,18 @@ class ImportsController < ApplicationController
def create def create
files = import_params[:files].reject(&:blank?) files = import_params[:files].reject(&:blank?)
files.each do |file| import_ids = files.map do |file|
import = current_user.imports.create( import = current_user.imports.create(
name: file.original_filename, name: file.original_filename,
source: params[:import][:source] source: params[:import][:source]
) )
import.file.attach(file) import.file.attach(file)
import.id
end
import_ids.each do |import_id|
ImportJob.set(wait: 5.seconds).perform_later(current_user.id, import_id)
end end
redirect_to imports_url, notice: "#{files.size} files are queued to be imported in background", status: :see_other redirect_to imports_url, notice: "#{files.size} files are queued to be imported in background", status: :see_other

View file

@ -5,12 +5,4 @@ class Import < ApplicationRecord
has_one_attached :file has_one_attached :file
enum source: { google: 0, owntracks: 1 } enum source: { google: 0, owntracks: 1 }
after_create_commit :async_import
private
def async_import
ImportJob.perform_later(user.id, self.id)
end
end end

View file

@ -56,6 +56,7 @@ services:
tty: true tty: true
entrypoint: dev-entrypoint.sh entrypoint: dev-entrypoint.sh
command: ['sidekiq'] command: ['sidekiq']
restart: on-failure
environment: environment:
RAILS_ENV: development RAILS_ENV: development
REDIS_URL: redis://dawarich_redis:6379/0 REDIS_URL: redis://dawarich_redis:6379/0

View file

@ -1,9 +0,0 @@
# frozen_string_literal: true
old_snake_case_name = ARGV[0]
old_camel_case_name = ARGV[0].to_s.split('_').collect(&:capitalize).join
new_snake_case_name = ARGV[1]
new_camel_case_name = ARGV[1].to_s.split('_').collect(&:capitalize).join
`sed -i '' -e 's/#{old_snake_case_name}/#{new_snake_case_name}/g' $(find . -type f)`
`sed -i '' -e 's/#{old_camel_case_name}/#{new_camel_case_name}/g' $(find . -type f)`

View file

@ -4,7 +4,7 @@ RSpec.describe ImportJob, type: :job do
describe '#perform' do describe '#perform' do
subject(:perform) { described_class.new.perform(user.id, import.id) } subject(:perform) { described_class.new.perform(user.id, import.id) }
let(:file_path) { 'spec/fixtures/owntracks_export.json' } let(:file_path) { 'spec/fixtures/files/owntracks/export.json' }
let(:file) { fixture_file_upload(file_path) } let(:file) { fixture_file_upload(file_path) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:import) { create(:import, user: user, file: file, name: File.basename(file.path)) } let(:import) { create(:import, user: user, file: file, name: File.basename(file.path)) }

View file

@ -4,7 +4,7 @@ RSpec.describe Overland::BatchCreatingJob, type: :job do
describe '#perform' do describe '#perform' do
subject(:perform) { described_class.new.perform(json) } subject(:perform) { described_class.new.perform(json) }
let(:file_path) { 'spec/fixtures/overland/geodata.json' } let(:file_path) { 'spec/fixtures/files/overland/geodata.json' }
let(:file) { File.open(file_path) } let(:file) { File.open(file_path) }
let(:json) { JSON.parse(file.read) } let(:json) { JSON.parse(file.read) }

View file

@ -2,7 +2,7 @@ require 'rails_helper'
RSpec.describe "Api::V1::Overland::Batches", type: :request do RSpec.describe "Api::V1::Overland::Batches", type: :request do
describe "POST /index" do describe "POST /index" do
let(:file_path) { 'spec/fixtures/overland/geodata.json' } let(:file_path) { 'spec/fixtures/files/overland/geodata.json' }
let(:file) { File.open(file_path) } let(:file) { File.open(file_path) }
let(:json) { JSON.parse(file.read) } let(:json) { JSON.parse(file.read) }
let(:params) { json } let(:params) { json }

View file

@ -0,0 +1,52 @@
require 'rails_helper'
RSpec.describe "Imports", type: :request do
describe "GET /imports" do
context 'when user is logged in' do
let(:user) { create(:user) }
before do
sign_in user
end
it "returns http success" do
get imports_path
expect(response).to have_http_status(200)
end
context 'when user has imports' do
let!(:import) { create(:import, user: user) }
it 'displays imports' do
get imports_path
expect(response.body).to include(import.name)
end
end
end
end
describe "POST /imports" do
context 'when user is logged in' do
let(:user) { create(:user) }
let(:file) { fixture_file_upload('owntracks/export.json', 'application/json') }
before { sign_in user }
it 'queues import job' do
expect {
post imports_path, params: { import: { source: 'owntracks', files: [file] } }
}.to have_enqueued_job(ImportJob).on_queue('default').at_least(1).times
end
it 'creates a new import' do
expect {
post imports_path, params: { import: { source: 'owntracks', files: [file] } }
}.to change(user.imports, :count).by(1)
expect(response).to redirect_to(imports_path)
end
end
end
end

View file

@ -2,7 +2,7 @@ require 'rails_helper'
RSpec.describe Overland::Params do RSpec.describe Overland::Params do
describe '#call' do describe '#call' do
let(:file_path) { 'spec/fixtures/overland/geodata.json' } let(:file_path) { 'spec/fixtures/files/overland/geodata.json' }
let(:file) { File.open(file_path) } let(:file) { File.open(file_path) }
let(:json) { JSON.parse(file.read) } let(:json) { JSON.parse(file.read) }

View file

@ -4,7 +4,7 @@ RSpec.describe OwnTracks::ExportParser do
describe '#call' do describe '#call' do
subject(:parser) { described_class.new(import).call } subject(:parser) { described_class.new(import).call }
let(:file_path) { 'spec/fixtures/owntracks_export.json' } let(:file_path) { 'spec/fixtures/files/owntracks/export.json' }
let(:file) { fixture_file_upload(file_path) } let(:file) { fixture_file_upload(file_path) }
let(:user) { create(:user) } let(:user) { create(:user) }
let(:import) { create(:import, user: user, file: file, name: File.basename(file.path)) } let(:import) { create(:import, user: user, file: file, name: File.basename(file.path)) }

View file

@ -4,7 +4,7 @@ RSpec.describe OwnTracks::Params do
describe '#call' do describe '#call' do
subject(:params) { described_class.new(raw_point_params).call } subject(:params) { described_class.new(raw_point_params).call }
let(:file_path) { 'spec/fixtures/owntracks_export.json' } let(:file_path) { 'spec/fixtures/files/owntracks/export.json' }
let(:file) { File.open(file_path) } let(:file) { File.open(file_path) }
let(:json) { JSON.parse(file.read) } let(:json) { JSON.parse(file.read) }
let(:user) { json.keys.first } let(:user) { json.keys.first }

View file

@ -1,11 +0,0 @@
#!/bin/sh
set -e
echo "Environment: $RAILS_ENV"
# Check if we need to install new gems
bundle check || bundle install --jobs 20 --retry 5
# Then run any passed command
bundle exec ${@}