lib/race_block.rb in race_block-0.2.2 vs lib/race_block.rb in race_block-0.3.0

- old
+ new

@@ -48,39 +48,34 @@ def self.reset(key) RaceBlock.client.del(RaceBlock.key(key)) end - def self.start(key, expire: config.expire, expiration_delay: config.expiration_delay, **args) - raise("A Redis client must be added to the RaceBlock configuration") unless configuration.redis - raise("A key must be provided to start a RaceBlock") if key.empty? + def self.start(base_key, expire: config.expire, expiration_delay: config.expiration_delay, **args) + raise("A key must be provided to start a RaceBlock") if base_key.empty? - @key = RaceBlock.key(key) + key = RaceBlock.key(base_key) # Set an expiration for the token if the key is defined but doesn't # have an expiration set (happens sometimes if a thread dies early). # `-1` means the key is set but does not expire, `-2` means the key is # not set - RaceBlock.client.expire(@key, 10) if RaceBlock.client.ttl(@key) == -1 + RaceBlock.client.expire(key, 10) if RaceBlock.client.ttl(key) == -1 # Token already exists - # todo: Revoke version 0.2.1 because it always return nil no matter what (never committed but released because I'm not a smart man) - if RaceBlock.client.get(@key) - logger.debug("Token already exists") - return - end + return logger.debug("Token already exists") if RaceBlock.client.get(key) - return unless set_token_and_wait(@key, **args) + return unless set_token_and_wait(key, **args) - RaceBlock.client.expire(@key, expire) + RaceBlock.client.expire(key, expire) logger.debug("Running block") r = yield # I have lots of internal debates on whether I should full # delete the key here or still let it sit for a few seconds - RaceBlock.client.expire(@key, expiration_delay) + RaceBlock.client.expire(key, expiration_delay) r end def self.set_token_and_wait(key, sleep_delay: config.sleep_delay, desync_tokens: 0) @@ -99,10 +94,10 @@ # token is still accurate. I'm hesitant to believe this actually # works, but I can't find any flaws in the logic at the current # moment, and I also believe this is what is keep the EmailQueue # stable which seems to have no duplicate sending problems. - return true if RaceBlock.client.get(@key) == token + return true if RaceBlock.client.get(key) == token # Token out of sync logger.debug("Token out of sync") false end