mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-09-23 08:29:07 +08:00
Merge branch 'develop'
This commit is contained in:
commit
446394b571
|
@ -36,11 +36,6 @@ module Api
|
||||||
def health
|
def health
|
||||||
User.new && Team.new && Project.new
|
User.new && Team.new && Project.new
|
||||||
User.first if params[:db]
|
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'
|
render plain: 'RUNNING'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -13,7 +13,7 @@ class SystemNotification < ApplicationRecord
|
||||||
}
|
}
|
||||||
# ignoring: :accents
|
# ignoring: :accents
|
||||||
|
|
||||||
has_many :user_system_notifications
|
has_many :user_system_notifications, dependent: :destroy
|
||||||
has_many :users, through: :user_system_notifications
|
has_many :users, through: :user_system_notifications
|
||||||
|
|
||||||
validates :title, :modal_title, :modal_body, :description, :source_created_at, :source_id, :last_time_changed_at,
|
validates :title, :modal_title, :modal_body, :description, :source_created_at, :source_id, :last_time_changed_at,
|
||||||
|
|
|
@ -6,6 +6,8 @@ module Notifications
|
||||||
include HTTParty
|
include HTTParty
|
||||||
base_uri Rails.application.secrets.system_notifications_uri
|
base_uri Rails.application.secrets.system_notifications_uri
|
||||||
|
|
||||||
|
SYNC_TIMESTAMP_CACHE_KEY = 'system_notifications_last_sync_timestamp'
|
||||||
|
|
||||||
attr_reader :errors
|
attr_reader :errors
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
|
@ -35,7 +37,10 @@ module Notifications
|
||||||
private
|
private
|
||||||
|
|
||||||
def call_api
|
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
|
channel = Rails.application.secrets.system_notifications_channel
|
||||||
|
|
||||||
unless last_sync
|
unless last_sync
|
||||||
|
@ -70,30 +75,27 @@ module Notifications
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_new_notifications
|
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
|
# Save new notification if not exists or override old 1
|
||||||
attrs =
|
attrs = received_notification
|
||||||
sn.slice('title',
|
.slice('title', 'description', 'modal_title', 'modal_body', 'show_on_login', 'source_id')
|
||||||
'description',
|
.merge('source_created_at': Time.zone.parse(received_notification['source_created_at']),
|
||||||
'modal_title',
|
'last_time_changed_at': Time.zone.parse(received_notification['last_time_changed_at']))
|
||||||
'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
|
.symbolize_keys
|
||||||
|
|
||||||
n = SystemNotification
|
notification = SystemNotification.where(source_id: attrs[:source_id]).first_or_initialize(attrs)
|
||||||
.where(source_id: attrs[:source_id]).first_or_initialize(attrs)
|
|
||||||
|
|
||||||
if n.new_record?
|
if notification.new_record?
|
||||||
save_notification n
|
save_notification(notification)
|
||||||
elsif n.last_time_changed_at < attrs[:last_time_changed_at]
|
elsif notification.last_time_changed_at < attrs[:last_time_changed_at]
|
||||||
n.update!(attrs)
|
notification.update!(attrs)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Rails.cache.delete(SYNC_TIMESTAMP_CACHE_KEY)
|
||||||
end
|
end
|
||||||
|
|
||||||
def save_notification(notification)
|
def save_notification(notification)
|
||||||
|
|
|
@ -4,9 +4,9 @@ require 'rufus-scheduler'
|
||||||
|
|
||||||
scheduler = Rufus::Scheduler.singleton
|
scheduler = Rufus::Scheduler.singleton
|
||||||
|
|
||||||
if ENV['ENABLE_TEMPLATES_SYNC'] && ARGV[0] == 'jobs:work'
|
if ENV['ENABLE_TEMPLATES_SYNC']
|
||||||
# Templates sync periodic task
|
# Templates sync periodic task
|
||||||
scheduler.every '1h' do
|
scheduler.every '12h' do
|
||||||
Rails.logger.info('Templates, syncing all template projects')
|
Rails.logger.info('Templates, syncing all template projects')
|
||||||
updated, total = TemplatesService.new.update_all_templates
|
updated, total = TemplatesService.new.update_all_templates
|
||||||
Rails.logger.info(
|
Rails.logger.info(
|
||||||
|
@ -17,11 +17,10 @@ if ENV['ENABLE_TEMPLATES_SYNC'] && ARGV[0] == 'jobs:work'
|
||||||
end
|
end
|
||||||
|
|
||||||
if Rails.application.secrets.system_notifications_uri.present? &&
|
if Rails.application.secrets.system_notifications_uri.present? &&
|
||||||
Rails.application.secrets.system_notifications_channel.present? &&
|
Rails.application.secrets.system_notifications_channel.present?
|
||||||
ARGV[0] == 'jobs:work'
|
|
||||||
|
|
||||||
# System notifications periodic task
|
# System notifications periodic task
|
||||||
scheduler.every '5m' do
|
scheduler.every '1h' do
|
||||||
Rails.logger.info('System Notifications syncing')
|
Rails.logger.info('System Notifications syncing')
|
||||||
Rails.logger.info(Process.pid)
|
Rails.logger.info(Process.pid)
|
||||||
result = Notifications::SyncSystemNotificationsService.call
|
result = Notifications::SyncSystemNotificationsService.call
|
||||||
|
|
Loading…
Reference in a new issue