From 3554e405db5f549eaf913d4758602f04c662c580 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Mon, 16 Dec 2024 20:32:28 +0100 Subject: [PATCH] Move setting `reverse_geocoded_at` to background job --- .app_version | 2 +- CHANGELOG.md | 6 ++++++ .../set_reverse_geocoded_at_for_points_job.rb | 17 +++++++++++++++++ ...125248_set_reverse_geocoded_at_for_points.rb | 4 +--- 4 files changed, 25 insertions(+), 4 deletions(-) create mode 100644 app/jobs/data_migrations/set_reverse_geocoded_at_for_points_job.rb diff --git a/.app_version b/.app_version index 5a03fb73..847e9aef 100644 --- a/.app_version +++ b/.app_version @@ -1 +1 @@ -0.20.0 +0.20.1 diff --git a/CHANGELOG.md b/CHANGELOG.md index 52f648b2..8fdc8806 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +# 0.20.1 - 2024-12-16 + +### Fixed + +- Setting `reverse_geocoded_at` for points that don't have geodata is now being performed in background job, in batches of 10,000 points to prevent memory exhaustion. + # 0.20.0 - 2024-12-16 ### Added diff --git a/app/jobs/data_migrations/set_reverse_geocoded_at_for_points_job.rb b/app/jobs/data_migrations/set_reverse_geocoded_at_for_points_job.rb new file mode 100644 index 00000000..1f375552 --- /dev/null +++ b/app/jobs/data_migrations/set_reverse_geocoded_at_for_points_job.rb @@ -0,0 +1,17 @@ +# frozen_string_literal: true + +class DataMigrations::SetReverseGeocodedAtForPointsJob < ApplicationJob + queue_as :default + + def perform + timestamp = Time.current + + Point.where.not(geodata: {}) + .where(reverse_geocoded_at: nil) + .in_batches(of: 10_000) do |relation| + # rubocop:disable Rails/SkipsModelValidations + relation.update_all(reverse_geocoded_at: timestamp) + # rubocop:enable Rails/SkipsModelValidations + end + end +end diff --git a/db/data/20241202125248_set_reverse_geocoded_at_for_points.rb b/db/data/20241202125248_set_reverse_geocoded_at_for_points.rb index 289380cf..cf0faba8 100644 --- a/db/data/20241202125248_set_reverse_geocoded_at_for_points.rb +++ b/db/data/20241202125248_set_reverse_geocoded_at_for_points.rb @@ -2,9 +2,7 @@ class SetReverseGeocodedAtForPoints < ActiveRecord::Migration[7.2] def up - # rubocop:disable Rails/SkipsModelValidations - Point.where.not(geodata: {}).update_all(reverse_geocoded_at: Time.current) - # rubocop:enable Rails/SkipsModelValidations + DataMigrations::SetReverseGeocodedAtForPointsJob.perform_later end def down