Class: Contentful::Scheduler::Queue
- Inherits:
-
Object
- Object
- Contentful::Scheduler::Queue
- Defined in:
- lib/contentful/scheduler/queue.rb
Constant Summary collapse
- @@instance =
nil
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Class Method Summary collapse
Instance Method Summary collapse
- #already_published?(webhook) ⇒ Boolean
- #in_queue?(webhook) ⇒ Boolean
- #isContentBlockAvailable(webhook) ⇒ Object
- #publish_date(webhook) ⇒ Object
- #publishable?(webhook) ⇒ Boolean
- #remove(webhook) ⇒ Object
- #removeContentBlocks(webhook) ⇒ Object
- #spaces ⇒ Object
- #update_or_create(webhook) ⇒ Object
- #updateContentBlocks(webhook) ⇒ Object
- #webhook_publish_field(webhook) ⇒ Object
- #webhook_publish_field?(webhook) ⇒ Boolean
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
10 11 12 |
# File 'lib/contentful/scheduler/queue.rb', line 10 def config @config end |
#logger ⇒ Object (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
112 113 114 115 116 |
# File 'lib/contentful/scheduler/queue.rb', line 112 def already_published?(webhook) return true if publish_date(webhook) < Time.now.utc false end |
#in_queue?(webhook) ⇒ Boolean
118 119 120 121 122 |
# File 'lib/contentful/scheduler/queue.rb', line 118 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) ⇒ Object
98 99 100 |
# File 'lib/contentful/scheduler/queue.rb', line 98 def isContentBlockAvailable(webhook) return !webhook.fields['contentBlocks'].nil? end |
#publish_date(webhook) ⇒ Object
124 125 126 127 128 |
# File 'lib/contentful/scheduler/queue.rb', line 124 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
102 103 104 105 106 107 108 109 110 |
# File 'lib/contentful/scheduler/queue.rb', line 102 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
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/contentful/scheduler/queue.rb', line 80 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 ⇒ Object
130 131 132 |
# File 'lib/contentful/scheduler/queue.rb', line 130 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 76 77 78 |
# File 'lib/contentful/scheduler/queue.rb', line 58 def updateContentBlocks(webhook) logger.info "isContentBlockAvailable {#{isContentBlockAvailable(webhook)},} successfully removed from queue" if isContentBlockAvailable(webhook) logger.info "COntent blocks #{webhook.fields['contentBlocks']['fi-FI']} successfully removed from queue" webhook.fields['contentBlocks']['fi-FI'].each do |sys| logger.info "Sys id is #{sys['sys']['id']}" 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 |
#webhook_publish_field(webhook) ⇒ Object
138 139 140 |
# File 'lib/contentful/scheduler/queue.rb', line 138 def webhook_publish_field(webhook) webhook.fields[spaces[webhook.space_id][:publish_field]] end |
#webhook_publish_field?(webhook) ⇒ Boolean
134 135 136 |
# File 'lib/contentful/scheduler/queue.rb', line 134 def webhook_publish_field?(webhook) webhook.fields.key?(spaces.fetch(webhook.space_id, {})[:publish_field]) end |