Sha256: 545940a3d0d7625a7bdb7826ab32cb6995114bd2e3a897520cbb169559961144
Contents?: true
Size: 1.89 KB
Versions: 1
Compression:
Stored size: 1.89 KB
Contents
# frozen_string_literal: true require 'httparty' require 'no_proxy_fix' require 'redis' require 'redis/connection/hiredis' require 'redis-namespace' require 'slack-notifier' module MShard class MShard include HTTParty base_uri ENV['MSHARD_URI'] def get(id) self.class.get("/v2/shards/#{id}").body end def set(params) self.class.post('/v2/shards', body: params)['id'] end def try(times: 5, delay: 2) error = nil times.times do begin return yield rescue StandardError => e error = e sleep delay end end raise error end def get_safe(*args) try { get(*args) } end def set_safe(*args) # rubocop:disable Naming/AccessorMethodName try { set(*args) } end def detour? ENV['http_proxy'] end def notify(**args) args[:slack] ||= true args[:webhook_url] ||= ENV['SLACK_WEBHOOK_URI'] args[:channel] ||= ENV['SLACK_CHANNEL'] return set(args) if detour? Slack::Notifier.new(args[:webhook_url], **args).ping(args[:text]) end def notify_safe(**args) try { notify(**args) } end def self.redis_cache(uri) @cache ||= Hash.new do |hash, uri_| hash[uri_] = Redis::Namespace.new(:shard, redis: Redis.new(url: uri_)) end @cache[uri] end def load(id, redis_uri: ENV['MSHARD_REDIS_URI']) return get(id) if detour? || redis_uri.nil? self.class.redis_cache(redis_uri).get(id) end def load_safe(*args) try { load(*args) } end def save(redis_uri: ENV['MSHARD_REDIS_URI'], **args) return set(**args) if detour? || redis_uri.nil? response = self.class.redis_cache(redis_uri).set(args[:id], args[:contents]) raise response unless response == 'OK' args[:id] end def save_safe(*args) try { save(*args) } end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mshard-0.8.0 | lib/mshard/mshard.rb |