Sha256: 87195c5a8840f599112927ca3e35b5799eab2a749c9171f767b75b2742fd9f03

Contents?: true

Size: 1.31 KB

Versions: 21

Compression:

Stored size: 1.31 KB

Contents

class RedisRateLimitsWorker
  @queue = :rate_limits

  class << self
    def perform(emails, access_token)
      # Create contacts
      sleep(10) unless able_to_perform?(:create)
      response = create_contacts(emails, access_token)
      # Remove contacts
      sleep(10) unless able_to_perform?(:remove)
      remove_contacts(response.results.map(&:id), access_token)
    end

    private

    def create_contacts(emails, access_token)
      response = ::Services::Hubspot::Contacts::CreateBatch.new(emails, access_token).call
      push_timestamp
      response
    end

    def remove_contacts(ids, access_token)
      Services::Hubspot::Contacts::ArchiveBatch.new(ids, access_token).call
      push_timestamp
    end

    def redis
      @redis ||= Redis.new(host: ENV['REDIS_HOST'])
    end

    def push_timestamp
      redis.lpush(:worker_timestamps, Time.current)
    end

    def pop_last_timestamp
      redis.rpop(:worker_timestamps)
    end

    def able_to_perform?(action)
      timestamps = redis.lrange(:worker_timestamps, 0, 90)
      ability = (timestamps.count < 90 || timestamps.last < 10.seconds.ago) ? true : false
      Resque.logger.info("Able to #{action} contacts: #{ability}")
      ability
    end

    def sleep(seconds)
      Resque.logger.info("Sleep #{seconds} seconds.")
      super(seconds)
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
hubspot-api-client-9.0.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-8.0.1 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-8.0.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-7.3.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-7.2.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-7.1.1 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-7.1.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-7.0.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-6.0.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-5.0.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-4.0.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-3.3.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-3.2.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-3.1.1 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-3.1.0.pre.beta sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-3.0.0.pre.beta sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-2.3.2 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-2.3.1 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-2.2.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb
hubspot-api-client-2.1.0 sample-apps/rate-limits-redis-sample-app/app/workers/redis_rate_limits_worker.rb