From d3f6d0da7bc500e6af42cced82e46d7d5b783f7d Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Tue, 29 Oct 2024 11:52:23 +0100 Subject: [PATCH] Rework the app version checking to be performed in the background and update docker compose file to use different directories for gems cache --- CHANGELOG.md | 24 +++++++++++++++++++ app/helpers/application_helper.rb | 8 +++---- app/jobs/app_version_checking_job.rb | 11 +++++++++ app/services/check_app_version.rb | 18 +++++++++----- app/views/shared/_navbar.html.erb | 7 +++--- config/environment.rb | 8 ++++++- .../{00_constants.rb => 01_constants.rb} | 0 config/schedule.yml | 5 ++++ docker-compose.yml | 4 ++-- spec/jobs/app_version_checking_job_spec.rb | 15 ++++++++++++ 10 files changed, 82 insertions(+), 18 deletions(-) create mode 100644 app/jobs/app_version_checking_job.rb rename config/initializers/{00_constants.rb => 01_constants.rb} (100%) create mode 100644 spec/jobs/app_version_checking_job_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d42d073..96458e16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Importing Immich data on the Imports page now will trigger an attempt to write raw json file with the data from Immich to `tmp/imports/immich_raw_data_CURRENT_TIME_USER_EMAIL.json` file. This is useful to debug the problem with the import if it fails. #270 +### Fixed + +- New app version is now being checked every 6 hours instead of 1 day and the check is being performed in the background. #238 + ### Changed - Hostname definition for Sidekiq healtcheck to solve #344. See the diff: @@ -24,6 +28,26 @@ and this project adheres to [Semantic Versioning](http://semver.org/). + test: [ "CMD-SHELL", "bundle exec sidekiqmon processes | grep ${HOSTNAME}" ] ``` +- Renamed directories used by app and sidekiq containers for gems cache to fix #339: + +```diff + dawarich_app: + image: freikin/dawarich:latest + container_name: dawarich_sidekiq + volumes: +- - gem_cache:/usr/local/bundle/gems ++ - gem_cache:/usr/local/bundle/gems_app + +... + + dawarich_sidekiq: + image: freikin/dawarich:latest + container_name: dawarich_sidekiq + volumes: +- - gem_cache:/usr/local/bundle/gems ++ - gem_cache:/usr/local/bundle/gems_sidekiq +``` + # 0.15.10 - 2024-10-25 ### Fixed diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2f5250c0..0b55457b 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -11,8 +11,8 @@ module ApplicationHelper end def year_timespan(year) - start_at = Time.new(year).beginning_of_year.strftime('%Y-%m-%dT%H:%M') - end_at = Time.new(year).end_of_year.strftime('%Y-%m-%dT%H:%M') + start_at = DateTime.new(year).beginning_of_year.strftime('%Y-%m-%dT%H:%M') + end_at = DateTime.new(year).end_of_year.strftime('%Y-%m-%dT%H:%M') { start_at:, end_at: } end @@ -66,9 +66,7 @@ module ApplicationHelper end def new_version_available? - Rails.cache.fetch('dawarich/app-version-check', expires_in: 1.day) do - CheckAppVersion.new.call - end + CheckAppVersion.new.call end def app_version diff --git a/app/jobs/app_version_checking_job.rb b/app/jobs/app_version_checking_job.rb new file mode 100644 index 00000000..a6fc2d9b --- /dev/null +++ b/app/jobs/app_version_checking_job.rb @@ -0,0 +1,11 @@ +# frozen_string_literal: true + +class AppVersionCheckingJob < ApplicationJob + queue_as :default + + def perform + Rails.cache.delete(CheckAppVersion::VERSION_CACHE_KEY) + + CheckAppVersion.new.call + end +end diff --git a/app/services/check_app_version.rb b/app/services/check_app_version.rb index 770a0f4c..6dc59932 100644 --- a/app/services/check_app_version.rb +++ b/app/services/check_app_version.rb @@ -1,18 +1,24 @@ # frozen_string_literal: true class CheckAppVersion + VERSION_CACHE_KEY = 'dawarich/app-version-check' + def initialize @repo_url = 'https://api.github.com/repos/Freika/dawarich/tags' @app_version = File.read('.app_version').strip end def call - begin - latest_version = JSON.parse(Net::HTTP.get(URI.parse(@repo_url)))[0]['name'] - rescue StandardError - return false - end - latest_version != @app_version + rescue StandardError + false + end + + private + + def latest_version + Rails.cache.fetch(VERSION_CACHE_KEY, expires_in: 6.hours) do + JSON.parse(Net::HTTP.get(URI.parse(@repo_url)))[0]['name'] + end end end diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index 16c8dcc2..8381844b 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -17,13 +17,12 @@ <%= link_to 'DaWarIch', root_path, class: 'btn btn-ghost normal-case text-xl'%>
- - ! - <% if new_version_available? %> -   + + <% else %> + <% end %>
diff --git a/config/environment.rb b/config/environment.rb index cac53157..c27e2a9f 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,5 +1,11 @@ +# frozen_string_literal: true + # Load the Rails application. -require_relative "application" +require_relative 'application' # Initialize the Rails application. Rails.application.initialize! + +# Clear the cache of the application version + +Rails.cache.delete(CheckAppVersion::VERSION_CACHE_KEY) diff --git a/config/initializers/00_constants.rb b/config/initializers/01_constants.rb similarity index 100% rename from config/initializers/00_constants.rb rename to config/initializers/01_constants.rb diff --git a/config/schedule.yml b/config/schedule.yml index e3c502f1..94151c3a 100644 --- a/config/schedule.yml +++ b/config/schedule.yml @@ -19,3 +19,8 @@ watcher_job: cron: "0 */1 * * *" # every 1 hour class: "Import::WatcherJob" queue: imports + +app_version_checking_job: + cron: "0 */6 * * *" # every 6 hours + class: "AppVersionCheckingJob" + queue: default diff --git a/docker-compose.yml b/docker-compose.yml index 537b7444..778c93d1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -37,7 +37,7 @@ services: image: freikin/dawarich:latest container_name: dawarich_app volumes: - - gem_cache:/usr/local/bundle/gems + - gem_cache:/usr/local/bundle/gems_app - public:/var/app/public - watched:/var/app/tmp/imports/watched networks: @@ -91,7 +91,7 @@ services: image: freikin/dawarich:latest container_name: dawarich_sidekiq volumes: - - gem_cache:/usr/local/bundle/gems + - gem_cache:/usr/local/bundle/gems_sidekiq - public:/var/app/public - watched:/var/app/tmp/imports/watched networks: diff --git a/spec/jobs/app_version_checking_job_spec.rb b/spec/jobs/app_version_checking_job_spec.rb new file mode 100644 index 00000000..d0a1dbef --- /dev/null +++ b/spec/jobs/app_version_checking_job_spec.rb @@ -0,0 +1,15 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe AppVersionCheckingJob, type: :job do + describe '#perform' do + let(:job) { described_class.new } + + it 'calls CheckAppVersion service' do + expect(CheckAppVersion).to receive(:new).and_return(instance_double(CheckAppVersion, call: true)) + + job.perform + end + end +end