FEATURE: Retain the latest 30 days of WebHookEvent records by default.

This commit is contained in:
Guo Xiang Tan 2017-11-08 14:08:30 +08:00
parent ed16cba77f
commit 6090994cdf
6 changed files with 51 additions and 6 deletions

View File

@ -0,0 +1,9 @@
module Jobs
class PurgeOldWebHookEvents < Jobs::Scheduled
every 1.week
def execute(_)
WebHookEvent.purge_old
end
end
end

View File

@ -5,6 +5,12 @@ class WebHookEvent < ActiveRecord::Base
default_scope { order('created_at DESC') }
def self.purge_old
where(
'created_at < ?', SiteSetting.retain_web_hook_events_period_days.days.ago
).delete_all
end
def update_web_hook_delivery_status
web_hook.last_delivery_status =
case status

View File

@ -3485,6 +3485,7 @@ en:
developer: 'Developer'
embedding: "Embedding"
legal: "Legal"
api: 'API'
user_api: 'User API'
uncategorized: 'Other'
backups: "Backups"

View File

@ -1556,6 +1556,8 @@ en:
default_categories_muted: "List of categories that are muted by default."
default_categories_watching_first_post: "List of categories in which first post in each new topic will be watched by default."
retain_web_hook_events_period_days: "Number of days to retain web hook event records."
max_user_api_reqs_per_day: "Maximum number of user API requests per key per day"
max_user_api_reqs_per_minute: "Maximum number of user API requests per key per minute"
allow_user_api_keys: "Allow generation of user API keys"

View File

@ -1427,6 +1427,10 @@ user_preferences:
type: category_list
default: ''
api:
retain_web_hook_events_period_days:
default: 30
user_api:
max_user_api_reqs_per_day:
default: 2880

View File

@ -4,13 +4,36 @@ describe WebHookEvent do
let(:event) { WebHookEvent.new(status: 200, web_hook: Fabricate(:web_hook)) }
let(:failed_event) { WebHookEvent.new(status: 400, web_hook: Fabricate(:web_hook)) }
describe '.purge_old' do
before do
SiteSetting.retain_web_hook_events_period_days = 1
end
it "should be able to purge old web hook events" do
web_hook = Fabricate(:web_hook)
web_hook_event = WebHookEvent.create!(status: 200, web_hook: web_hook)
WebHookEvent.create!(status: 200, web_hook: web_hook, created_at: 2.days.ago)
expect { described_class.purge_old }
.to change { WebHookEvent.count }.by(-1)
expect(WebHookEvent.find(web_hook_event.id)).to eq(web_hook_event)
end
end
describe '#update_web_hook_delivery_status' do
it 'update last delivery status for associated WebHook record' do
event.update_web_hook_delivery_status
expect(event.web_hook.last_delivery_status).to eq(WebHook.last_delivery_statuses[:successful])
expect(event.web_hook.last_delivery_status)
.to eq(WebHook.last_delivery_statuses[:successful])
end
it 'sets last delivery status to failed' do
failed_event.update_web_hook_delivery_status
expect(failed_event.web_hook.last_delivery_status).to eq(WebHook.last_delivery_statuses[:failed])
expect(failed_event.web_hook.last_delivery_status)
.to eq(WebHook.last_delivery_statuses[:failed])
end
end
end