diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2017-09-20 14:21:15 +0200 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2017-09-20 14:24:14 +0200 |
commit | b3566a01049cdfbde2a9221319601d8949c12a5a (patch) | |
tree | 873d695caaaec68afabf8146af5f83f0983be04e /app/services/keys | |
parent | a09d032b2a64c7b6652dcd589de2d9bcba7d9613 (diff) | |
download | gitlab-ce-b3566a01049cdfbde2a9221319601d8949c12a5a.tar.gz |
Stop using Sidekiq for updating Key#last_used_atremove-use-key-worker
This makes things simpler as no scheduling is involved. Further we
remove the need for running a SELECT + UPDATE just to get the key and
update it, whereas we only need an UPDATE when setting last_used_at
directly in a request.
The added service class takes care of updating Key#last_used_at without
using Sidekiq. Further it makes sure we only try to obtain a Redis lease
if we're confident that we actually need to do so, instead of always
obtaining it. We also make sure to _only_ update last_used_at instead of
also updating updated_at.
Fixes https://gitlab.com/gitlab-org/gitlab-ce/issues/36663
Diffstat (limited to 'app/services/keys')
-rw-r--r-- | app/services/keys/last_used_service.rb | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/app/services/keys/last_used_service.rb b/app/services/keys/last_used_service.rb new file mode 100644 index 00000000000..066f3246158 --- /dev/null +++ b/app/services/keys/last_used_service.rb @@ -0,0 +1,33 @@ +module Keys + class LastUsedService + TIMEOUT = 1.day.to_i + + attr_reader :key + + # key - The Key for which to update the last used timestamp. + def initialize(key) + @key = key + end + + def execute + # We _only_ want to update last_used_at and not also updated_at (which + # would be updated when using #touch). + key.update_column(:last_used_at, Time.zone.now) if update? + end + + def update? + last_used = key.last_used_at + + return false if last_used && (Time.zone.now - last_used) <= TIMEOUT + + !!redis_lease.try_obtain + end + + private + + def redis_lease + Gitlab::ExclusiveLease + .new("key_update_last_used_at:#{key.id}", timeout: TIMEOUT) + end + end +end |