Class: Contentful::Scheduler::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/scheduler/queue.rb

Constant Summary collapse

@@instance =
nil

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



10
11
12
# File 'lib/contentful/scheduler/queue.rb', line 10

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/contentful/scheduler/queue.rb', line 10

def logger
  @logger
end

Class Method Details

.instance(logger = ::Contentful::Webhook::Listener::Support::NullLogger.new) ⇒ Object



12
13
14
# File 'lib/contentful/scheduler/queue.rb', line 12

def self.instance(logger = ::Contentful::Webhook::Listener::Support::NullLogger.new)
  @@instance ||= new(logger)
end

Instance Method Details

#already_published?(webhook) ⇒ Boolean



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/contentful/scheduler/queue.rb', line 64

def already_published?(webhook)
  return true if publish_date(webhook) < Time.now.utc
  if !webhook_is_publish_in_future(webhook)
    return false unless webhook.sys.key?('publishedAt')
    if !webhook.sys['publishedAt'].nil?
      return Chronic.parse(webhook.sys['publishedAt']).utc < Time.now.utc
    end
  end

  false
end

#in_queue?(webhook) ⇒ Boolean



76
77
78
79
80
# File 'lib/contentful/scheduler/queue.rb', line 76

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

#publish_date(webhook) ⇒ Object



82
83
84
85
86
# File 'lib/contentful/scheduler/queue.rb', line 82

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) ⇒ Boolean



54
55
56
57
58
59
60
61
62
# File 'lib/contentful/scheduler/queue.rb', line 54

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) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/contentful/scheduler/queue.rb', line 36

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]
  )

  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

#spacesObject



88
89
90
# File 'lib/contentful/scheduler/queue.rb', line 88

def spaces
  config[:spaces]
end

#update_or_create(webhook) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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]
  )

  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_is_publish_in_future(webhook) ⇒ Object



100
101
102
# File 'lib/contentful/scheduler/queue.rb', line 100

def webhook_is_publish_in_future(webhook)
  webhook.fields[spaces[webhook.space_id][:is_publish_in_future]]
end

#webhook_publish_field(webhook) ⇒ Object



96
97
98
# File 'lib/contentful/scheduler/queue.rb', line 96

def webhook_publish_field(webhook)
  webhook.fields[spaces[webhook.space_id][:publish_field]]
end

#webhook_publish_field?(webhook) ⇒ Boolean



92
93
94
# File 'lib/contentful/scheduler/queue.rb', line 92

def webhook_publish_field?(webhook)
  webhook.fields.key?(spaces.fetch(webhook.space_id, {})[:publish_field])
end