diff --git a/VERSION b/VERSION index 20a1265cf..ce2dd5357 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.21.4 +1.21.5 diff --git a/app/controllers/api/api_controller.rb b/app/controllers/api/api_controller.rb index 29b99f986..27141eded 100644 --- a/app/controllers/api/api_controller.rb +++ b/app/controllers/api/api_controller.rb @@ -36,11 +36,6 @@ module Api def health User.new && Team.new && Project.new User.first if params[:db] - if Rails.application.secrets.system_notifications_uri.present? && - Rails.application.secrets.system_notifications_channel.present? && - !Notifications::SyncSystemNotificationsService.available? - return render plain: 'SYSTEM NOTIFICATIONS SERVICE CHECK FAILED', status: :error - end render plain: 'RUNNING' end diff --git a/app/models/system_notification.rb b/app/models/system_notification.rb index a65bb697d..4e2f1196d 100644 --- a/app/models/system_notification.rb +++ b/app/models/system_notification.rb @@ -13,7 +13,7 @@ class SystemNotification < ApplicationRecord } # ignoring: :accents - has_many :user_system_notifications + has_many :user_system_notifications, dependent: :destroy has_many :users, through: :user_system_notifications validates :title, :modal_title, :modal_body, :description, :source_created_at, :source_id, :last_time_changed_at, diff --git a/app/services/notifications/sync_system_notifications_service.rb b/app/services/notifications/sync_system_notifications_service.rb index 9dcd71d92..c5243ace0 100644 --- a/app/services/notifications/sync_system_notifications_service.rb +++ b/app/services/notifications/sync_system_notifications_service.rb @@ -6,6 +6,8 @@ module Notifications include HTTParty base_uri Rails.application.secrets.system_notifications_uri + SYNC_TIMESTAMP_CACHE_KEY = 'system_notifications_last_sync_timestamp' + attr_reader :errors def initialize @@ -35,7 +37,10 @@ module Notifications private def call_api - last_sync = SystemNotification.last_sync_timestamp + last_sync = + Rails.cache.fetch(SYNC_TIMESTAMP_CACHE_KEY, expires_in: 24.hours, skip_nil: true) do + SystemNotification.last_sync_timestamp + end channel = Rails.application.secrets.system_notifications_channel unless last_sync @@ -70,30 +75,27 @@ module Notifications end def save_new_notifications - @api_call.parsed_response['notifications'].each do |sn| + received_notifications = @api_call.parsed_response['notifications'] + return if received_notifications.blank? + + received_notifications.each do |received_notification| # Save new notification if not exists or override old 1 - attrs = - sn.slice('title', - 'description', - 'modal_title', - 'modal_body', - 'show_on_login', - 'source_id') - .merge('source_created_at': - Time.parse(sn['source_created_at']), - 'last_time_changed_at': - Time.parse(sn['last_time_changed_at'])) - .symbolize_keys + attrs = received_notification + .slice('title', 'description', 'modal_title', 'modal_body', 'show_on_login', 'source_id') + .merge('source_created_at': Time.zone.parse(received_notification['source_created_at']), + 'last_time_changed_at': Time.zone.parse(received_notification['last_time_changed_at'])) + .symbolize_keys - n = SystemNotification - .where(source_id: attrs[:source_id]).first_or_initialize(attrs) + notification = SystemNotification.where(source_id: attrs[:source_id]).first_or_initialize(attrs) - if n.new_record? - save_notification n - elsif n.last_time_changed_at < attrs[:last_time_changed_at] - n.update!(attrs) + if notification.new_record? + save_notification(notification) + elsif notification.last_time_changed_at < attrs[:last_time_changed_at] + notification.update!(attrs) end end + + Rails.cache.delete(SYNC_TIMESTAMP_CACHE_KEY) end def save_notification(notification) diff --git a/config/initializers/scheduler.rb b/config/initializers/scheduler.rb index 94e0f16d5..6cee7dfb6 100644 --- a/config/initializers/scheduler.rb +++ b/config/initializers/scheduler.rb @@ -4,9 +4,9 @@ require 'rufus-scheduler' scheduler = Rufus::Scheduler.singleton -if ENV['ENABLE_TEMPLATES_SYNC'] && ARGV[0] == 'jobs:work' +if ENV['ENABLE_TEMPLATES_SYNC'] # Templates sync periodic task - scheduler.every '1h' do + scheduler.every '12h' do Rails.logger.info('Templates, syncing all template projects') updated, total = TemplatesService.new.update_all_templates Rails.logger.info( @@ -17,11 +17,10 @@ if ENV['ENABLE_TEMPLATES_SYNC'] && ARGV[0] == 'jobs:work' end if Rails.application.secrets.system_notifications_uri.present? && - Rails.application.secrets.system_notifications_channel.present? && - ARGV[0] == 'jobs:work' + Rails.application.secrets.system_notifications_channel.present? # System notifications periodic task - scheduler.every '5m' do + scheduler.every '1h' do Rails.logger.info('System Notifications syncing') Rails.logger.info(Process.pid) result = Notifications::SyncSystemNotificationsService.call