Report abuse

config/environment.rb [in initializer loop]

1
2
3
4
5
6
7
8
9
  if Rails.env.production?
    config.after_initialize do
      port = RAILS_ENV == "production" ? 11300 : 11310
      AsyncObserver::Queue.queue = Beanstalk::Pool.new(["localhost:#{port}"])

      # This value should change every time you make a release of your app.
      AsyncObserver::Queue.app_version = 1.0
    end
  end

app/models/product.rb

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  def check_image_url
    unless image_url.blank?
      if image_url_checked_at.nil? || (image_url_good? && image_url_checked_at < 2.weeks.ago)
        begin
          async_send :ensure_image_url
        rescue Beanstalk::NotConnected
          Norman.deliver_shout "Beanstalkd server did not connect in #{RAILS_ENV} mode at #{Time.now}",
            "Product image checking will not be available until this is rectified"
        end
      end
    end
  end

  def ensure_image_url
    return unless Rails.env.production?
    begin
      response = SimpleHttp.head(image_url)
      content_type = response["content-type"]
      if content_type =~ /^image/
        self.image_url_good = true
      else
        self.image_url_good = false
      end
    rescue Errno::ETIMEDOUT
      return
    rescue RuntimeError, SocketError => err
      self.image_url_good = false
    end
    self.image_url_checked_at = Time.now
    ThinkingSphinx.deltas_enabled = false
    save false # Don't validate, just save!
    ThinkingSphinx.deltas_enabled = true
  end