mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-23 00:16:21 +08:00
Merge branch 'develop'
This commit is contained in:
commit
446394b571
|
@ -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
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue