mirror of
https://github.com/Freika/dawarich.git
synced 2026-01-11 09:41:40 -05:00
Make sure cache jobs are run only on server start
This commit is contained in:
parent
73fc9be3fb
commit
e904d396c8
7 changed files with 42 additions and 16 deletions
|
|
@ -7,10 +7,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||||
|
|
||||||
# 0.21.6 - 2025-01-07
|
# 0.21.6 - 2025-01-07
|
||||||
|
|
||||||
|
### Changed
|
||||||
|
|
||||||
|
- Disabled visit suggesting job after import.
|
||||||
|
- Improved performance of the `User#years_tracked` method.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
- Inconsistent password for the `dawarich_db` service in `docker-compose_mounted_volumes.yml`. #605
|
- Inconsistent password for the `dawarich_db` service in `docker-compose_mounted_volumes.yml`. #605
|
||||||
- Points are now being rendered with higher z-index than polylines. #577
|
- Points are now being rendered with higher z-index than polylines. #577
|
||||||
|
- Run cache cleaning and preheating jobs only on server start. #594
|
||||||
|
|
||||||
# 0.21.5 - 2025-01-07
|
# 0.21.5 - 2025-01-07
|
||||||
|
|
||||||
|
|
|
||||||
6
app/jobs/cache/preheating_job.rb
vendored
6
app/jobs/cache/preheating_job.rb
vendored
|
|
@ -5,7 +5,11 @@ class Cache::PreheatingJob < ApplicationJob
|
||||||
|
|
||||||
def perform
|
def perform
|
||||||
User.find_each do |user|
|
User.find_each do |user|
|
||||||
Rails.cache.write("dawarich/user_#{user.id}_years_tracked", user.years_tracked, expires_in: 1.day)
|
Rails.cache.write(
|
||||||
|
"dawarich/user_#{user.id}_years_tracked",
|
||||||
|
user.years_tracked,
|
||||||
|
expires_in: 1.day
|
||||||
|
)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -66,15 +66,23 @@ class User < ApplicationRecord
|
||||||
|
|
||||||
def years_tracked
|
def years_tracked
|
||||||
Rails.cache.fetch("dawarich/user_#{id}_years_tracked", expires_in: 1.day) do
|
Rails.cache.fetch("dawarich/user_#{id}_years_tracked", expires_in: 1.day) do
|
||||||
tracked_points
|
# Use select_all for better performance with large datasets
|
||||||
.pluck(:timestamp)
|
sql = <<-SQL
|
||||||
.map { |ts| Time.zone.at(ts) }
|
SELECT DISTINCT
|
||||||
.group_by(&:year)
|
EXTRACT(YEAR FROM TO_TIMESTAMP(timestamp)) AS year,
|
||||||
.transform_values do |dates|
|
TO_CHAR(TO_TIMESTAMP(timestamp), 'Mon') AS month
|
||||||
dates.map { |date| date.strftime('%b') }.uniq.sort
|
FROM points
|
||||||
end
|
WHERE user_id = #{id}
|
||||||
|
ORDER BY year DESC, month ASC
|
||||||
|
SQL
|
||||||
|
|
||||||
|
result = ActiveRecord::Base.connection.select_all(sql)
|
||||||
|
|
||||||
|
result
|
||||||
|
.map { |r| [r['year'].to_i, r['month']] }
|
||||||
|
.group_by { |year, _| year }
|
||||||
|
.transform_values { |year_data| year_data.map { |_, month| month } }
|
||||||
.map { |year, months| { year: year, months: months } }
|
.map { |year, months| { year: year, months: months } }
|
||||||
.sort_by { |entry| -entry[:year] } # Sort in descending order
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
5
app/services/cache/clean.rb
vendored
5
app/services/cache/clean.rb
vendored
|
|
@ -4,6 +4,7 @@ class Cache::Clean
|
||||||
class << self
|
class << self
|
||||||
def call
|
def call
|
||||||
Rails.logger.info('Cleaning cache...')
|
Rails.logger.info('Cleaning cache...')
|
||||||
|
delete_control_flag
|
||||||
delete_version_cache
|
delete_version_cache
|
||||||
delete_years_tracked_cache
|
delete_years_tracked_cache
|
||||||
Rails.logger.info('Cache cleaned')
|
Rails.logger.info('Cache cleaned')
|
||||||
|
|
@ -11,6 +12,10 @@ class Cache::Clean
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def delete_control_flag
|
||||||
|
Rails.cache.delete('cache_jobs_scheduled')
|
||||||
|
end
|
||||||
|
|
||||||
def delete_version_cache
|
def delete_version_cache
|
||||||
Rails.cache.delete(CheckAppVersion::VERSION_CACHE_KEY)
|
Rails.cache.delete(CheckAppVersion::VERSION_CACHE_KEY)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ class Imports::Create
|
||||||
create_import_finished_notification(import, user)
|
create_import_finished_notification(import, user)
|
||||||
|
|
||||||
schedule_stats_creating(user.id)
|
schedule_stats_creating(user.id)
|
||||||
schedule_visit_suggesting(user.id, import)
|
# schedule_visit_suggesting(user.id, import) # Disabled until places & visits are reworked
|
||||||
rescue StandardError => e
|
rescue StandardError => e
|
||||||
create_import_failed_notification(import, user, e)
|
create_import_failed_notification(import, user, e)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -6,8 +6,11 @@ require_relative 'application'
|
||||||
# Initialize the Rails application.
|
# Initialize the Rails application.
|
||||||
Rails.application.initialize!
|
Rails.application.initialize!
|
||||||
|
|
||||||
# Clear the cache
|
# Use an atomic operation to ensure one-time execution
|
||||||
Cache::CleaningJob.perform_later
|
if defined?(Rails::Server) && Rails.cache.write('cache_jobs_scheduled', true, unless_exist: true)
|
||||||
|
# Clear the cache
|
||||||
|
Cache::CleaningJob.perform_later
|
||||||
|
|
||||||
# Preheat the cache
|
# Preheat the cache
|
||||||
Cache::PreheatingJob.perform_later
|
Cache::PreheatingJob.perform_later
|
||||||
|
end
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ RSpec.describe Imports::Create do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'schedules visit suggesting' do
|
xit 'schedules visit suggesting' do
|
||||||
Sidekiq::Testing.inline! do
|
Sidekiq::Testing.inline! do
|
||||||
expect { service.call }.to have_enqueued_job(VisitSuggestingJob)
|
expect { service.call }.to have_enqueued_job(VisitSuggestingJob)
|
||||||
end
|
end
|
||||||
|
|
@ -59,7 +59,7 @@ RSpec.describe Imports::Create do
|
||||||
|
|
||||||
context 'when import fails' do
|
context 'when import fails' do
|
||||||
before do
|
before do
|
||||||
allow(OwnTracks::ExportParser).to receive(:new).with(import, user.id).and_return(double(call: false))
|
allow(OwnTracks::ExportParser).to receive(:new).with(import, user.id).and_raise(StandardError)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'creates a failed notification' do
|
it 'creates a failed notification' do
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue