-
-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
197 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
class Session < ApplicationRecord | ||
include Denormalizable | ||
include Environmental | ||
include Accountable | ||
|
||
belongs_to :token | ||
belongs_to :bearer, | ||
polymorphic: true | ||
|
||
has_environment default: -> { token&.environment_id } | ||
has_account default: -> { token&.account_id }, inverse_of: :sessions | ||
|
||
denormalizes :bearer_type, :bearer_id, | ||
from: :token | ||
|
||
def expired? = expiry < Time.current | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
class PruneExpiredSessionsWorker < BaseWorker | ||
STATEMENT_TIMEOUT = ENV.fetch('KEYGEN_PRUNE_STATEMENT_TIMEOUT') { '1min' } | ||
BATCH_SIZE = ENV.fetch('KEYGEN_PRUNE_BATCH_SIZE') { 1_000 }.to_i | ||
BATCH_WAIT = ENV.fetch('KEYGEN_PRUNE_BATCH_WAIT') { 1 }.to_f | ||
|
||
sidekiq_options queue: :cron, | ||
cronitor_disabled: false | ||
|
||
def perform | ||
sessions = Session.where(<<~SQL.squish, max_age: 90.days.ago) | ||
last_used_at IS NULL OR last_used_at < :max_age OR expiry < :max_age | ||
SQL | ||
.reorder(created_at: :asc) | ||
|
||
total = sessions.count | ||
sum = 0 | ||
|
||
batches = (total / BATCH_SIZE) + 1 | ||
batch = 0 | ||
|
||
Keygen.logger.info "[workers.prune-expired-sessions] Starting" | ||
Keygen.logger.info "[workers.prune-expired-sessions] Pruning #{total} rows: batches=#{batches}" | ||
|
||
loop do | ||
count = sessions.statement_timeout(STATEMENT_TIMEOUT) do | ||
sessions.limit(BATCH_SIZE).delete_all | ||
end | ||
|
||
sum += count | ||
batch += 1 | ||
|
||
Keygen.logger.info "[workers.prune-expired-sessions] Pruned #{sum}/#{total} rows: batch=#{batch}/#{batches}" | ||
|
||
sleep BATCH_WAIT | ||
|
||
break if count < BATCH_SIZE | ||
end | ||
|
||
Keygen.logger.info "[workers.prune-expired-sessions] Done" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
class CreateSessions < ActiveRecord::Migration[7.2] | ||
def change | ||
create_table :sessions, id: :uuid, default: -> { 'uuid_generate_v4()' } do |t| | ||
t.uuid :account_id, null: false | ||
t.uuid :environment_id, null: true | ||
t.uuid :token_id, null: false | ||
t.string :bearer_type, null: false | ||
t.uuid :bearer_id, null: false | ||
|
||
t.datetime :last_used_at, null: true | ||
t.datetime :expiry, null: false | ||
t.string :ip, null: false | ||
t.string :user_agent, null: true | ||
|
||
t.timestamps | ||
|
||
t.index %i[account_id created_at], order: { created_at: :desc } | ||
t.index %i[environment_id] | ||
t.index %i[token_id] | ||
t.index %i[bearer_id bearer_type] | ||
t.index %i[created_at expiry last_used_at], order: { created_at: :asc } | ||
end | ||
end | ||
end |
Oops, something went wrong.