<commit_before># vim:fileencoding=utf-8
require_relative 'base'

module Resque
  module Scheduler
    module Lock
      class Resilient < Base
        def acquire!
          evalsha(
            :acquire,
            keys: [key],
            argv: [value]
          ).to_i == 1
        end

        def locked?
          evalsha(
            :locked,
            keys: [key],
            argv: [value]
          ).to_i == 1
        end

        def timeout=(seconds)
          if locked?
            @timeout = seconds
            @locked_sha = nil
            @acquire_sha = nil
          end
          @timeout
        end

        private

        def evalsha(script, keys:, argv:, refresh: false, final_try: false)
          sha_method_name = "#{script}_sha"
          Resque.redis.evalsha(
            send(sha_method_name, refresh),
            keys: keys,
            argv: argv
          )
        rescue Redis::CommandError => e
          if e.message =~ /NOSCRIPT/ && !final_try
            evalsha(script, keys: keys, argv: argv, refresh: true, final_try: true)
          else
            raise
          end
        end

        def locked_sha(refresh = false)<commit_msg>retry instead of recursive<commit_after># vim:fileencoding=utf-8
require_relative 'base'

module Resque
  module Scheduler
    module Lock
      class Resilient < Base
        def acquire!
          evalsha(
            :acquire,
            keys: [key],
            argv: [value]
          ).to_i == 1
        end

        def locked?
          evalsha(
            :locked,
            keys: [key],
            argv: [value]
          ).to_i == 1
        end

        def timeout=(seconds)
          if locked?
            @timeout = seconds
            @locked_sha = nil
            @acquire_sha = nil
          end
          @timeout
        end

        private

        def evalsha(script, keys:, argv:, refresh: false)
          sha_method_name = "#{script}_sha"
          Resque.redis.evalsha(
            send(sha_method_name, refresh),
            keys: keys,
            argv: argv
          )
        rescue Redis::CommandError => e
          if e.message =~ /NOSCRIPT/
            refresh = true
            retry
          else
            raise
          end
        end

        def locked_sha(refresh = false)
{'fraction_non_alphanumeric': 0.08311688311688312, 'fraction_numerical': 0.0025974025974025974, 'mean_word_length': 1.2415130940834143, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5986373', 'n_tokens_mistral': 647, 'n_tokens_neox': 639, 'n_words': 171}