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

Returns:

  • (Boolean)


105
106
107
108
109
# File 'lib/contentful/scheduler/queue.rb', line 105

def already_published?(webhook)
  return true if publish_date(webhook) < Time.now.utc

  false
end

#in_queue?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


111
112
113
114
115
# File 'lib/contentful/scheduler/queue.rb', line 111

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



117
118
119
120
121
# File 'lib/contentful/scheduler/queue.rb', line 117

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

Returns:

  • (Boolean)


95
96
97
98
99
100
101
102
103
# File 'lib/contentful/scheduler/queue.rb', line 95

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



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# 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) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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.id,
        ::Contentful::Scheduler.config[:management_token]
      )
      if success
        logger.info "Webhook Content Block {id: #{sys.id}, space_id: #{webhook.space_id}} successfully removed from queue"
      else
        logger.warn "Webhook Content Block {id: #{sys.id}, space_id: #{webhook.space_id}} couldn't be removed from queue"
      end
    end
  end
end

#spacesObject



123
124
125
# File 'lib/contentful/scheduler/queue.rb', line 123

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
35
36
# 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

#updateContentBlocks(webhook) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 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.id,
        ::Contentful::Scheduler.config[:spaces][webhook.space_id][:management_token]
      )
      if success
        logger.info "Webhook Content block {id: #{sys.id}, space_id: #{webhook.space_id}} successfully added to queue"
      else
        logger.warn "Webhook Content block {id: #{sys.id}, space_id: #{webhook.space_id}} couldn't be added to queue"
      end
    end
  end
end

#webhook_publish_field(webhook) ⇒ Object



131
132
133
# File 'lib/contentful/scheduler/queue.rb', line 131

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

#webhook_publish_field?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
# File 'lib/contentful/scheduler/queue.rb', line 127

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