Class: Ungulate::Job
- Inherits:
-
Object
- Object
- Ungulate::Job
- Defined in:
- lib/ungulate/job.rb
Constant Summary collapse
- @@blobs_from_urls =
{}
Instance Attribute Summary collapse
-
#bucket ⇒ Object
Returns the value of attribute bucket.
-
#key ⇒ Object
Returns the value of attribute key.
-
#notification_url ⇒ Object
Returns the value of attribute notification_url.
-
#queue ⇒ Object
Returns the value of attribute queue.
-
#versions ⇒ Object
Returns the value of attribute versions.
Class Method Summary collapse
Instance Method Summary collapse
- #attributes=(options) ⇒ Object
- #blob_from_url(url) ⇒ Object
- #image_from_instruction(original, instruction) ⇒ Object
- #image_from_instruction_chain(original, chain) ⇒ Object
-
#initialize ⇒ Job
constructor
A new instance of Job.
- #instruction_args(args) ⇒ Object
- #magick_image_from_url(url) ⇒ Object
- #process ⇒ Object
- #processed_image(original, instruction) ⇒ Object
- #processed_versions ⇒ Object
- #send_notification ⇒ Object
- #source ⇒ Object
- #source_image ⇒ Object
- #version_key(version) ⇒ Object
Constructor Details
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
11 12 13 |
# File 'lib/ungulate/job.rb', line 11 def bucket @bucket end |
#key ⇒ Object
Returns the value of attribute key.
11 12 13 |
# File 'lib/ungulate/job.rb', line 11 def key @key end |
#notification_url ⇒ Object
Returns the value of attribute notification_url.
11 12 13 |
# File 'lib/ungulate/job.rb', line 11 def notification_url @notification_url end |
#queue ⇒ Object
Returns the value of attribute queue.
11 12 13 |
# File 'lib/ungulate/job.rb', line 11 def queue @queue end |
#versions ⇒ Object
Returns the value of attribute versions.
11 12 13 |
# File 'lib/ungulate/job.rb', line 11 def versions @versions end |
Class Method Details
.pop(queue_name) ⇒ Object
28 29 30 31 32 33 34 35 |
# File 'lib/ungulate/job.rb', line 28 def self.pop(queue_name) job = new job.queue = sqs.queue queue_name = job.queue.pop attributes = YAML.load .to_s job.attributes = attributes if attributes job end |
.s3 ⇒ Object
16 17 18 19 20 |
# File 'lib/ungulate/job.rb', line 16 def self.s3 @s3 ||= RightAws::S3.new(ENV['AMAZON_ACCESS_KEY_ID'], ENV['AMAZON_SECRET_ACCESS_KEY']) end |
.sqs ⇒ Object
22 23 24 25 26 |
# File 'lib/ungulate/job.rb', line 22 def self.sqs @sqs ||= RightAws::SqsGen2.new(ENV['AMAZON_ACCESS_KEY_ID'], ENV['AMAZON_SECRET_ACCESS_KEY']) end |
Instance Method Details
#attributes=(options) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/ungulate/job.rb', line 42 def attributes=() self.bucket = Job.s3.bucket([:bucket]) self.key = [:key] self.notification_url = [:notification_url] self.versions = [:versions] end |
#blob_from_url(url) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/ungulate/job.rb', line 56 def blob_from_url(url) Job.blobs_from_urls[url] ||= begin @logger.info "Grabbing blob from URL #{url}" Curl::Easy.http_get(url).body_str end end |
#image_from_instruction(original, instruction) ⇒ Object
80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ungulate/job.rb', line 80 def image_from_instruction(original, instruction) method, *args = instruction send_args = instruction_args(args) @logger.info "Performing #{method} with #{args.join(', ')}" original.send(method, *send_args).tap do |new_image| original.destroy! send_args.select {|arg| arg.is_a?(Magick::Image)}.each(&:destroy!) end end |
#image_from_instruction_chain(original, chain) ⇒ Object
91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ungulate/job.rb', line 91 def image_from_instruction_chain(original, chain) if chain.one? image_from_instruction(original, chain.first) else image_from_instruction_chain( image_from_instruction(original, chain.shift), chain ) end end |
#instruction_args(args) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ungulate/job.rb', line 68 def instruction_args(args) args.map do |arg| if arg.is_a?(Symbol) "Magick::#{arg.to_s.classify}".constantize elsif arg.respond_to?(:match) && arg.match(/^http/) magick_image_from_url(arg) else arg end end end |
#magick_image_from_url(url) ⇒ Object
64 65 66 |
# File 'lib/ungulate/job.rb', line 64 def magick_image_from_url(url) Magick::Image.from_blob(blob_from_url(url)).first end |
#process ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ungulate/job.rb', line 123 def process return false if processed_versions.empty? processed_versions.each do |version, image| version_key = version_key version @logger.info "Storing #{version} @ #{version_key}" bucket.put( version_key, image.to_blob, {}, 'public-read', { 'Content-Type' => MIME::Types.type_for(image.format).to_s, # expire in about one month: refactor to grab from job description 'Cache-Control' => 'max-age=2629743', } ) image.destroy! end send_notification true end |
#processed_image(original, instruction) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/ungulate/job.rb', line 102 def processed_image(original, instruction) if instruction.first.respond_to?(:entries) image_from_instruction_chain(original, instruction) else image_from_instruction(original, instruction) end end |
#processed_versions ⇒ Object
49 50 51 52 53 54 |
# File 'lib/ungulate/job.rb', line 49 def processed_versions @processed_versions ||= versions.map do |name, instruction| [name, processed_image(source_image, instruction)] end end |
#send_notification ⇒ Object
148 149 150 151 152 153 |
# File 'lib/ungulate/job.rb', line 148 def send_notification return false if notification_url.blank? @logger.info "Sending notification to #{notification_url}" Curl::Easy.http_put(notification_url, '') end |
#source ⇒ Object
114 115 116 117 118 119 120 121 |
# File 'lib/ungulate/job.rb', line 114 def source if @source @source else @logger.info "Grabbing source image #{key}" @source = bucket.get key end end |
#source_image ⇒ Object
110 111 112 |
# File 'lib/ungulate/job.rb', line 110 def source_image Magick::Image.from_blob(source).first end |
#version_key(version) ⇒ Object
155 156 157 158 159 160 |
# File 'lib/ungulate/job.rb', line 155 def version_key(version) dirname = File.dirname(key) extname = File.extname(key) basename = File.basename(key, extname) "#{dirname}/#{basename}_#{version}#{extname}".sub(/^\.\//, '') end |