summaryrefslogtreecommitdiff
path: root/db/post_migrate/20200930144340_set_job_waiter_ttl.rb
blob: 347fa4be5a098ea45e431e7f53f0d2284634d6d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# frozen_string_literal: true

class SetJobWaiterTtl < ActiveRecord::Migration[6.0]
  DOWNTIME = false

  SCRIPT = <<~LUA
    if redis.call("ttl", KEYS[1]) < 0 then
      redis.call("expire", KEYS[1], 21600)
    end
  LUA

  def up
    Gitlab::Redis::SharedState.with do |redis|
      cursor_init = '0'
      cursor = cursor_init

      loop do
        cursor, keys = redis.scan(cursor, match: 'gitlab:job_waiter:*')

        redis.pipelined do |redis|
          keys.each { |k| redis.eval(SCRIPT, keys: [k]) }
        end

        break if cursor == cursor_init
      end
    end
  end

  def down
  end
end