class Contentful::Scheduler::Queue

Attributes

config[R]
logger[R]

Public Class Methods

instance(logger = ::Contentful::Webhook::Listener::Support::NullLogger.new) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 12
def self.instance(logger = ::Contentful::Webhook::Listener::Support::NullLogger.new)
  @@instance ||= new(logger)
end
new(logger) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 141
def initialize(logger)
  @config = ::Contentful::Scheduler.config
  @logger = logger
end

Public Instance Methods

already_published?(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 109
def already_published?(webhook)
  return true if publish_date(webhook) < Time.now.utc

  false
end
in_queue?(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 115
def in_queue?(webhook)
  Resque.peek(::Contentful::Scheduler::Tasks::Publish, 0, -1).any? do |job|
    job['args'][0] == webhook.space_id && job['args'][1] == webhook.id
  end
end
isContentBlockAvailable(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 95
def isContentBlockAvailable(webhook)
  return !webhook.fields['contentBlocks'].nil?
end
publish_date(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 121
def publish_date(webhook)
  date_field = webhook_publish_field(webhook)
  date_field = date_field[date_field.keys[0]] if date_field.is_a? Hash
  Chronic.parse(date_field).utc
end
publishable?(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 99
def publishable?(webhook)
  return false unless spaces.key?(webhook.space_id)

  if webhook_publish_field?(webhook)
    return !webhook_publish_field(webhook).nil?
  end

  false
end
remove(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 38
def remove(webhook)
  return unless publishable?(webhook)
  return unless in_queue?(webhook)

  success = Resque.remove_delayed(
    ::Contentful::Scheduler::Tasks::Publish,
    webhook.space_id,
    webhook.id,
    ::Contentful::Scheduler.config[:management_token]
  )

  removeContentBlocks(webhook)

  if success
    logger.info "Webhook {id: #{webhook.id}, space_id: #{webhook.space_id}} successfully removed from queue"
  else
    logger.warn "Webhook {id: #{webhook.id}, space_id: #{webhook.space_id}} couldn't be removed from queue"
  end
end
removeContentBlocks(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 77
def removeContentBlocks(webhook)
  if isContentBlockAvailable(webhook)
    webhook.fields['contentBlocks']['fi-FI'].each do |sys|
      success = Resque.remove_delayed(
        ::Contentful::Scheduler::Tasks::Publish,
        webhook.space_id,
        sys['sys']['id'],
        ::Contentful::Scheduler.config[:management_token]
      )
      if success
        logger.info "Webhook Content Block {id: #{sys['sys']['id']}, space_id: #{webhook.space_id}} successfully removed from queue"
      else
        logger.warn "Webhook Content Block {id: #{sys['sys']['id']}, space_id: #{webhook.space_id}} couldn't be removed from queue"
      end
    end
  end
end
spaces() click to toggle source
# File lib/contentful/scheduler/queue.rb, line 127
def spaces
  config[:spaces]
end
updateContentBlocks(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 58
def updateContentBlocks(webhook)
  if isContentBlockAvailable(webhook)
    webhook.fields['contentBlocks']['fi-FI'].each do |sys|
      success = Resque.enqueue_at(
        publish_date(webhook),
        ::Contentful::Scheduler::Tasks::Publish,
        webhook.space_id,
        sys['sys']['id'],
        ::Contentful::Scheduler.config[:spaces][webhook.space_id][:management_token]
      )
      if success
        logger.info "Webhook Content block {id: #{sys['sys']['id']}, space_id: #{webhook.space_id}} successfully added to queue"
      else
        logger.warn "Webhook Content block {id: #{sys['sys']['id']}, space_id: #{webhook.space_id}} couldn't be added to queue"
      end
    end
  end
end
update_or_create(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 16
def update_or_create(webhook)
  return unless publishable?(webhook)
  remove(webhook) if in_queue?(webhook)
  return if already_published?(webhook)

  success = Resque.enqueue_at(
    publish_date(webhook),
    ::Contentful::Scheduler::Tasks::Publish,
    webhook.space_id,
    webhook.id,
    ::Contentful::Scheduler.config[:spaces][webhook.space_id][:management_token]
  )

  updateContentBlocks(webhook)

  if success
    logger.info "Webhook {id: #{webhook.id}, space_id: #{webhook.space_id}} successfully added to queue"
  else
    logger.warn "Webhook {id: #{webhook.id}, space_id: #{webhook.space_id}} couldn't be added to queue"
  end
end
webhook_publish_field(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 135
def webhook_publish_field(webhook)
  webhook.fields[spaces[webhook.space_id][:publish_field]]
end
webhook_publish_field?(webhook) click to toggle source
# File lib/contentful/scheduler/queue.rb, line 131
def webhook_publish_field?(webhook)
  webhook.fields.key?(spaces.fetch(webhook.space_id, {})[:publish_field])
end