2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-07-07 11:18:12 +05:30
|
|
|
class ChatName < ApplicationRecord
|
2021-12-11 22:18:48 +05:30
|
|
|
include LooseForeignKey
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
LAST_USED_AT_INTERVAL = 1.hour
|
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
belongs_to :integration, foreign_key: :service_id
|
2017-08-17 22:00:37 +05:30
|
|
|
belongs_to :user
|
|
|
|
|
|
|
|
validates :user, presence: true
|
2021-06-08 01:23:25 +05:30
|
|
|
validates :integration, presence: true
|
2017-08-17 22:00:37 +05:30
|
|
|
validates :team_id, presence: true
|
|
|
|
validates :chat_id, presence: true
|
|
|
|
|
|
|
|
validates :user_id, uniqueness: { scope: [:service_id] }
|
|
|
|
validates :chat_id, uniqueness: { scope: [:service_id, :team_id] }
|
2018-03-27 19:54:05 +05:30
|
|
|
|
2021-12-11 22:18:48 +05:30
|
|
|
loose_foreign_key :ci_pipeline_chat_data, :chat_name_id, on_delete: :async_delete
|
|
|
|
|
2018-03-27 19:54:05 +05:30
|
|
|
# Updates the "last_used_timestamp" but only if it wasn't already updated
|
|
|
|
# recently.
|
|
|
|
#
|
|
|
|
# The throttling this method uses is put in place to ensure that high chat
|
|
|
|
# traffic doesn't result in many UPDATE queries being performed.
|
|
|
|
def update_last_used_at
|
|
|
|
return unless update_last_used_at?
|
|
|
|
|
|
|
|
obtained = Gitlab::ExclusiveLease
|
|
|
|
.new("chat_name/last_used_at/#{id}", timeout: LAST_USED_AT_INTERVAL.to_i)
|
|
|
|
.try_obtain
|
|
|
|
|
|
|
|
touch(:last_used_at) if obtained
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_last_used_at?
|
|
|
|
last_used_at.nil? || last_used_at > LAST_USED_AT_INTERVAL.ago
|
|
|
|
end
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|