Module: WebHooks::HasWebHooks

Included in:
Project
Defined in:
app/models/concerns/web_hooks/has_web_hooks.rb

Constant Summary collapse

WEB_HOOK_CACHE_EXPIRY =
1.hour

Instance Method Summary collapse

Instance Method Details

#any_hook_failed?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'app/models/concerns/web_hooks/has_web_hooks.rb', line 7

def any_hook_failed?
  hooks.disabled.exists?
end

#cache_web_hook_failure(state = any_hook_failed?) ) ⇒ Object



36
37
38
39
40
41
42
# File 'app/models/concerns/web_hooks/has_web_hooks.rb', line 36

def cache_web_hook_failure(state = any_hook_failed?)
  Gitlab::Redis::SharedState.with do |redis|
    redis.set(web_hook_failure_redis_key, state.to_s, ex: WEB_HOOK_CACHE_EXPIRY)

    state
  end
end

#fetch_web_hook_failureObject



27
28
29
30
31
32
33
34
# File 'app/models/concerns/web_hooks/has_web_hooks.rb', line 27

def fetch_web_hook_failure
  Gitlab::Redis::SharedState.with do |_redis|
    current = get_web_hook_failure
    next current unless current.nil?

    cache_web_hook_failure
  end
end

#get_web_hook_failureObject



19
20
21
22
23
24
25
# File 'app/models/concerns/web_hooks/has_web_hooks.rb', line 19

def get_web_hook_failure
  Gitlab::Redis::SharedState.with do |redis|
    current = redis.get(web_hook_failure_redis_key)

    Gitlab::Utils.to_boolean(current) if current
  end
end

#last_failure_redis_keyObject



15
16
17
# File 'app/models/concerns/web_hooks/has_web_hooks.rb', line 15

def last_failure_redis_key
  "web_hooks:last_failure:#{self.class.name.underscore}-#{id}"
end

#last_webhook_failureObject



44
45
46
47
48
49
50
# File 'app/models/concerns/web_hooks/has_web_hooks.rb', line 44

def last_webhook_failure
  last_failure = Gitlab::Redis::SharedState.with do |redis|
    redis.get(last_failure_redis_key)
  end

  DateTime.parse(last_failure) if last_failure
end

#web_hook_failure_redis_keyObject



11
12
13
# File 'app/models/concerns/web_hooks/has_web_hooks.rb', line 11

def web_hook_failure_redis_key
  "any_web_hook_failed:#{id}"
end