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.



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

def config
  @config
end

#loggerObject (readonly)

Returns the value of attribute logger.



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

def logger
  @logger
end

Class Method Details

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



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

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

Instance Method Details

#getPublishOrUnpublishDate(webhook, type) ⇒ Object



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

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

#getScheduleType(type) ⇒ Object



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

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

#in_publish_queue?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


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

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)


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

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



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

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

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



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

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



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

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)


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

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

#publishable?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#remove_publish(webhook) ⇒ Object



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

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



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

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



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

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



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

def spaces
  config[:spaces]
end

#unpublish_date(webhook) ⇒ Object



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

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)


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

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

#unpublishable?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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



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

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



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

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



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

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



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

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

#webhook_publish_field?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


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

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



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

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

#webhook_unpublish_field?(webhook) ⇒ Boolean

Returns:

  • (Boolean)


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

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