Class: Ungulate::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/ungulate/job.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeJob

Returns a new instance of Job.



33
34
35
36
# File 'lib/ungulate/job.rb', line 33

def initialize
  @logger = Ungulate::Server.logger
  self.versions = []
end

Instance Attribute Details

#bucketObject

Returns the value of attribute bucket.



10
11
12
# File 'lib/ungulate/job.rb', line 10

def bucket
  @bucket
end

#keyObject

Returns the value of attribute key.



10
11
12
# File 'lib/ungulate/job.rb', line 10

def key
  @key
end

#notification_urlObject

Returns the value of attribute notification_url.



10
11
12
# File 'lib/ungulate/job.rb', line 10

def notification_url
  @notification_url
end

#queueObject

Returns the value of attribute queue.



10
11
12
# File 'lib/ungulate/job.rb', line 10

def queue
  @queue
end

#versionsObject

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
  message = job.queue.pop
  attributes = YAML.load message.to_s
  job.attributes = attributes if attributes
  job
end

.s3Object



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

.sqsObject



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=(options)
  self.bucket = Job.s3.bucket(options[:bucket])
  self.key = options[:key]
  self.notification_url = options[:notification_url]
  self.versions = options[:versions]
end

#processObject



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_versionsObject



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_notificationObject



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

#sourceObject



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