-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2641 from MushroomObserver/nimmo-recurring-cron
Use SolidQueue to run a cron job repopulating Location and Observation center_lat/center_lng
- Loading branch information
Showing
4 changed files
with
75 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# frozen_string_literal: true | ||
|
||
class UpdateBoxAreaAndCenterColumnsJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(**args) | ||
args ||= {} | ||
log("Starting UpdateBoxAreaAndCenterColumnsJob.perform") | ||
# This should be zero, but count just in case. | ||
loc_count = Location.where(box_area: nil).count | ||
log("Found #{loc_count} locations without a box_area.") | ||
# Count the observations associated with locations that are under the | ||
# max area, but haven't got a center lat/lng, and log what we're updating. | ||
obs_count = Observation.in_box_of_max_area.where(location_lat: nil).count | ||
log("Found #{obs_count} observations where the associated location was " \ | ||
"small enough, but the obs didn't have a location_lat/lng.") | ||
return [loc_count, obs_count] if args[:dry_run] | ||
|
||
# Do the update. This returns counts of locations/observations updated. | ||
loc_updated, obs_centered, obs_center_nulled = | ||
Location.update_box_area_and_center_columns | ||
|
||
log("Updated #{loc_updated} locations' box_area and center_lat/lng.") | ||
log("Updated #{obs_centered} observations' location_lat/lng.") | ||
log("Nulled #{obs_center_nulled} observations' location_lat/lng.") | ||
# Return the values for debugging | ||
[loc_updated, obs_centered, obs_center_nulled] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require "test_helper" | ||
|
||
class UpdateBoxAreaAndCenterColumnsJobTest < ActiveJob::TestCase | ||
def test_update_box_area_and_center_columns | ||
# Check that we have some obs that need updating | ||
assert_not_empty(Observation.in_box_of_max_area.where(location_lat: nil)) | ||
|
||
job = UpdateBoxAreaAndCenterColumnsJob.new | ||
job.perform | ||
|
||
assert_empty(Observation.in_box_of_max_area.where(location_lat: nil)) | ||
# NOTE: All locations may already have a box_area, so probably nil before | ||
assert_empty(Location.where(box_area: nil)) | ||
end | ||
|
||
def test_update_box_area_dry_run | ||
# Check that we have some obs that need updating | ||
assert_not_empty(Observation.in_box_of_max_area.where(location_lat: nil)) | ||
|
||
job = UpdateBoxAreaAndCenterColumnsJob.new | ||
_loc_count, obs_count = job.perform(dry_run: true) | ||
|
||
# check it again and be sure it did NOT update the obs | ||
needs_update = Observation.in_box_of_max_area.where(location_lat: nil) | ||
assert_not_empty(needs_update) | ||
assert_equal(obs_count, needs_update.count) | ||
end | ||
end |