Skip to content

Commit

Permalink
Ensure reset state of syncing when complete sync
Browse files Browse the repository at this point in the history
  • Loading branch information
aidewoode committed Nov 30, 2022
1 parent 7fa139a commit 10fb16d
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 21 deletions.
12 changes: 0 additions & 12 deletions app/jobs/media_sync_job.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
class MediaSyncJob < ApplicationJob
queue_as :default
before_enqueue :start_syncing
after_perform :stop_syncing

def perform(type = :all, file_paths = [])
Media.sync(type, file_paths)
end

private

def start_syncing
Media.syncing = true
end

def stop_syncing
Media.syncing = false
end
end
4 changes: 4 additions & 0 deletions app/models/media.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ class Media

class << self
def sync(type = :all, file_paths = [])
self.syncing = true
file_paths = MediaFile.file_paths if type == :all

return if file_paths.blank?

case type
Expand All @@ -25,6 +27,8 @@ def sync(type = :all, file_paths = [])
remove_files(file_paths)
add_files(file_paths)
end
ensure
self.syncing = false
end

def syncing?
Expand Down
8 changes: 0 additions & 8 deletions test/jobs/media_sync_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@
require "test_helper"

class MediaSyncJobTest < ActiveJob::TestCase
setup do
Media.syncing = false
end

test "sync media" do
mock = MiniTest::Mock.new
mock.expect(:call, true, [:all, []])

Setting.update(media_path: Rails.root.join("test/fixtures/files"))
assert_not Media.syncing?

MediaSyncJob.perform_later
assert Media.syncing?

Media.stub(:sync, mock) do
perform_enqueued_jobs
mock.verify
end

assert_not Media.syncing?
end
end
1 change: 0 additions & 1 deletion test/models/media_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ class MediaTest < ActiveSupport::TestCase

setup do
clear_media_data
Media.syncing = false

Setting.update(media_path: Rails.root.join("test/fixtures/files"))
Media.sync
Expand Down

0 comments on commit 10fb16d

Please sign in to comment.