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

#getPublishOrUnpublishDate(webhook, type) ⇒ Object



201
202
203
204
205
206
207
# File 'lib/contentful/scheduler/queue.rb', line 201

def getPublishOrUnpublishDate(webhook, type)
  if type == 'publish'
    publish_date(webhook)
  else
    unpublish_date(webhook)
  end
end

#getScheduleType(type) ⇒ Object



193
194
195
196
197
198
199
# File 'lib/contentful/scheduler/queue.rb', line 193

def getScheduleType(type)
  if type == 'publish'
    ::Contentful::Scheduler::Tasks::Publish
  else
    ::Contentful::Scheduler::Tasks::Unpublish
  end
end

#in_publish_queue?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
# File 'lib/contentful/scheduler/queue.rb', line 132

def in_publish_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

#in_unpublish_queue?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


138
139
140
141
142
# File 'lib/contentful/scheduler/queue.rb', line 138

def in_unpublish_queue?(webhook)
  Resque.peek(::Contentful::Scheduler::Tasks::Unpublish, 0, -1).any? do |job|
    job['args'][0] == webhook.space_id && job['args'][1] == webhook.id
  end
end

#isContentBlockAvailable(webhook) ⇒ Object



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

def isContentBlockAvailable(webhook)
  return !webhook.fields['contentBlocks'].nil?
end

#log_event_success(webhook, success, event_kind, action) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/contentful/scheduler/queue.rb', line 93

def log_event_success(webhook, success, event_kind, action)
  if success
    logger.info "Webhook {id: #{webhook.id}, space_id: #{webhook.space_id}} successfully #{action} the #{event_kind} queue"
  else
    logger.warn "Webhook {id: #{webhook.id}, space_id: #{webhook.space_id}} couldn't be #{action} the #{event_kind} queue"
  end
end

#publish_date(webhook) ⇒ Object



144
145
146
147
148
# File 'lib/contentful/scheduler/queue.rb', line 144

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

#publish_is_future?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


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

def publish_is_future?(webhook)
  publish_date(webhook) > Time.now.utc
end

#publishable?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


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

def publishable?(webhook)
  return false unless spaces.key?(webhook.space_id)

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

  false
end

#remove(webhook) ⇒ Object



56
57
58
59
# File 'lib/contentful/scheduler/queue.rb', line 56

def remove(webhook)
  remove_publish(webhook)
  remove_unpublish(webhook)
end

#remove_publish(webhook) ⇒ Object



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

def remove_publish(webhook)
  return unless publishable?(webhook)
  return unless in_publish_queue?(webhook)

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

  removeContentBlocks(webhook, 'publish')

  log_event_success(webhook, success, 'publish', 'removed from')
end

#remove_unpublish(webhook) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/contentful/scheduler/queue.rb', line 77

def remove_unpublish(webhook)
  return unless unpublishable?(webhook)
  return unless in_unpublish_queue?(webhook)

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

  removeContentBlocks(webhook, 'unpublish')

  log_event_success(webhook, success, 'unpublish', 'removed from')
end

#removeContentBlocks(webhook, type) ⇒ Object



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/contentful/scheduler/queue.rb', line 175

def removeContentBlocks(webhook, type)
  if isContentBlockAvailable(webhook)
    webhook.fields['contentBlocks']['fi-FI'].each do |sys|
      success = Resque.remove_delayed(
        getScheduleType(type),
        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 " + type + " queue"
      else
        logger.warn "Webhook Content Block {id: #{sys['sys']['id']}, space_id: #{webhook.space_id}} couldn't be removed from " + type + " queue"
      end
    end
  end
end

#spacesObject



209
210
211
# File 'lib/contentful/scheduler/queue.rb', line 209

def spaces
  config[:spaces]
end

#unpublish_date(webhook) ⇒ Object



150
151
152
153
154
# File 'lib/contentful/scheduler/queue.rb', line 150

def unpublish_date(webhook)
  date_field = webhook_unpublish_field(webhook)
  date_field = date_field[date_field.keys[0]] if date_field.is_a? Hash
  Chronic.parse(date_field).utc
end

#unpublish_is_future?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


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

def unpublish_is_future?(webhook)
  unpublish_date(webhook) > Time.now.utc
end

#unpublishable?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


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

def unpublishable?(webhook)
  return false unless spaces.key?(webhook.space_id)

  if webhook_unpublish_field?(webhook)
    return !webhook_unpublish_field(webhook).nil? && unpublish_is_future?(webhook)
  end
  false
end

#update_or_create(webhook) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/contentful/scheduler/queue.rb', line 16

def update_or_create(webhook)
  if publishable?(webhook)
    successPublish = update_or_create_for_publish(webhook)
    updateContentBlocks(webhook, 'publish')
    log_event_success(webhook, successPublish, 'publish', 'added to')
  end

  if unpublishable?(webhook)
    successUnpublish = update_or_create_for_unpublish(webhook)
    updateContentBlocks(webhook, 'unpublish')
    log_event_success(webhook, successUnpublish, 'unpublish', 'added to')
  end
end

#update_or_create_for_publish(webhook) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/contentful/scheduler/queue.rb', line 30

def update_or_create_for_publish(webhook)
  remove_publish(webhook) if in_publish_queue?(webhook)
  return false unless publish_is_future?(webhook)

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

#update_or_create_for_unpublish(webhook) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/contentful/scheduler/queue.rb', line 43

def update_or_create_for_unpublish(webhook)
  remove_unpublish(webhook) if in_unpublish_queue?(webhook)
  return false unless unpublish_is_future?(webhook)

  return Resque.enqueue_at(
    unpublish_date(webhook),
    ::Contentful::Scheduler::Tasks::Unpublish,
    webhook.space_id,
    webhook.id,
    ::Contentful::Scheduler.config[:spaces][webhook.space_id][:management_token]
  )
end

#updateContentBlocks(webhook, type) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'lib/contentful/scheduler/queue.rb', line 156

def updateContentBlocks(webhook, type)
  if isContentBlockAvailable(webhook)
    webhook.fields['contentBlocks']['fi-FI'].each do |sys|
      success = Resque.enqueue_at(
        getPublishOrUnpublishDate(webhook, type),
        getScheduleType(type),
        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 " + type + " queue"
      else
        logger.warn "Webhook Content block {id: #{sys['sys']['id']}, space_id: #{webhook.space_id}} couldn't be added to " + type + " queue"
      end
    end
  end
end

#webhook_publish_field(webhook) ⇒ Object



221
222
223
# File 'lib/contentful/scheduler/queue.rb', line 221

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

#webhook_publish_field?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
# File 'lib/contentful/scheduler/queue.rb', line 213

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

#webhook_unpublish_field(webhook) ⇒ Object



225
226
227
# File 'lib/contentful/scheduler/queue.rb', line 225

def webhook_unpublish_field(webhook)
  webhook.fields[spaces[webhook.space_id][:unpublish_field]]
end

#webhook_unpublish_field?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


217
218
219
# File 'lib/contentful/scheduler/queue.rb', line 217

def webhook_unpublish_field?(webhook)
  webhook.fields.key?(spaces.fetch(webhook.space_id, {})[:unpublish_field]) if webhook.respond_to?(:fields)
end