Class: Ungulate::Job
- Inherits:
-
Object
- Object
- Ungulate::Job
- Defined in:
- lib/ungulate/job.rb
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
-
#initialize ⇒ Job
constructor
A new instance of Job.
- #process ⇒ Object
- #processed_versions ⇒ Object
- #send_notification ⇒ Object
- #source ⇒ Object
- #version_key(version) ⇒ Object
Constructor Details
Instance Attribute Details
#bucket ⇒ Object
Returns the value of attribute bucket.
10 11 12 |
# File 'lib/ungulate/job.rb', line 10 def bucket @bucket end |
#key ⇒ Object
Returns the value of attribute key.
10 11 12 |
# File 'lib/ungulate/job.rb', line 10 def key @key end |
#notification_url ⇒ Object
Returns the value of attribute notification_url.
10 11 12 |
# File 'lib/ungulate/job.rb', line 10 def notification_url @notification_url end |
#queue ⇒ Object
Returns the value of attribute queue.
10 11 12 |
# File 'lib/ungulate/job.rb', line 10 def queue @queue end |
#versions ⇒ Object
Returns the value of attribute versions.
10 11 12 |
# File 'lib/ungulate/job.rb', line 10 def versions @versions end |
Class Method Details
.pop(queue_name) ⇒ Object
24 25 26 27 28 29 30 31 |
# File 'lib/ungulate/job.rb', line 24 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
12 13 14 15 16 |
# File 'lib/ungulate/job.rb', line 12 def self.s3 @s3 ||= RightAws::S3.new(ENV['AMAZON_ACCESS_KEY_ID'], ENV['AMAZON_SECRET_ACCESS_KEY']) end |
.sqs ⇒ Object
18 19 20 21 22 |
# File 'lib/ungulate/job.rb', line 18 def self.sqs @sqs ||= RightAws::SqsGen2.new(ENV['AMAZON_ACCESS_KEY_ID'], ENV['AMAZON_SECRET_ACCESS_KEY']) end |
Instance Method Details
#attributes=(options) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/ungulate/job.rb', line 38 def attributes=() self.bucket = Job.s3.bucket([:bucket]) self.key = [:key] self.notification_url = [:notification_url] self.versions = [:versions] end |
#process ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ungulate/job.rb', line 66 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_versions ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/ungulate/job.rb', line 45 def processed_versions @processed_versions ||= versions.map do |name, instruction| method, x, y = instruction image = Magick::Image.from_blob(source).first @logger.info "Performing #{method} with #{x}, #{y}" processed_image = image.send(method, x, y) image.destroy! [name, processed_image] end end |
#send_notification ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/ungulate/job.rb', line 91 def send_notification return false if notification_url.blank? @logger.info "Sending notification to #{notification_url}" url = URI.parse(notification_url) http = Net::HTTP.new(url.host, url.port) http.use_ssl = true if url.scheme == 'https' http.start {|http| http.put(url.path, nil) } end |
#source ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/ungulate/job.rb', line 57 def source if @source @source else @logger.info "Grabbing source image #{key}" @source = bucket.get key end end |
#version_key(version) ⇒ Object
103 104 105 106 107 108 |
# File 'lib/ungulate/job.rb', line 103 def version_key(version) dirname = File.dirname(key) extname = File.extname(key) basename = File.basename(key, extname) "#{dirname}/#{basename}_#{version}#{extname}".sub(/^\.\//, '') end |