Commit 457646d8

mo khan <mo@mokhan.ca>
2015-01-21 03:42:40
replace old infrastructure code with active job.
1 parent d4124da
Changed files (8)
app
config
initializers
script
spec
services
application
app/controllers/admin/photos_controller.rb
@@ -1,8 +1,7 @@
 module Admin
   class PhotosController < AdminController
-    def initialize(photo_repository = Photo, command_bus = Spank::IOC.resolve(:command_bus), storage = Spank::IOC.resolve(:blob_storage))
+    def initialize(photo_repository = Photo, storage = Spank::IOC.resolve(:blob_storage))
       @photo_repository = photo_repository
-      @bus = command_bus
       @storage = storage
       super()
     end
@@ -17,13 +16,10 @@ module Admin
 
     def update
       photo = @photo_repository.find(params[:id])
-      original = OriginalVersion.new(photo)
-      key = original.create_key
+      key = OriginalVersion.new(photo).create_key
       @storage.download(key) do |file|
-        @bus.publish(:upload_photo, {
-          photo_id: photo.id,
-          file_path: move_to_temporary_storage(file.path, File.basename(key))
-        })
+        temp_file = move_to_temporary_storage(file.path, File.basename(key))
+        ProcessPhotoJob.perform_later(photo, temp_file)
       end
 
       redirect_to admin_photos_path
app/controllers/my/avatars_controller.rb
@@ -1,10 +1,5 @@
 module My
   class AvatarsController < BaseController
-    def initialize(bus = Spank::IOC.resolve(:command_bus))
-      @bus = bus
-      super()
-    end
-
     def new
       @avatar = current_user.avatar || Photo.new
     end
app/services/application/handlers/process_photo.rb
@@ -1,16 +0,0 @@
-class ProcessPhoto
-  def initialize(photos, blob_storage)
-    @photos = photos
-    @blob_storage = blob_storage
-  end
-
-  def handles?(event)
-    :upload_photo == event
-  end
-
-  def handle(message)
-    photo = @photos.find(message[:photo_id])
-    photo.upload(message[:file_path], @blob_storage)
-    photo.save!
-  end
-end
app/services/application/upload_photo.rb
@@ -1,13 +1,13 @@
 class UploadPhoto
-  def initialize(command_bus = Spank::IOC.resolve(:command_bus), cakes = Creation)
-    @command_bus = command_bus
+  def initialize(cakes = Creation)
     @cakes = cakes
   end
 
   def run(cake_id, params)
     ActiveRecord::Base.transaction do
       photo = @cakes.find(cake_id).photos.create!(image_processing: true, watermark: params[:watermark])
-      @command_bus.publish(:upload_photo, create_message_from(cake_id, params, photo))
+      tempfile = move_to_temporary_storage(params[:image].path, params[:image].original_filename)
+      ProcessPhotoJob.perform_later(photo, tempfile)
       photo
     end
   end
@@ -25,14 +25,4 @@ class UploadPhoto
     system "mkdir -p #{directory}"
     directory
   end
-
-  def create_message_from(cake_id, payload, photo)
-    {
-      cake_id: cake_id,
-      photo_id: photo.id,
-      file_path: move_to_temporary_storage(payload[:image].path, payload[:image].original_filename),
-      original_filename: payload[:image].original_filename,
-      content_type: payload[:image].content_type,
-    }
-  end
 end
app/services/infrastructure/command_bus.rb
@@ -1,9 +0,0 @@
-class CommandBus
-  def initialize(queue = Delayed::Job)
-    @queue = queue
-  end
-
-  def publish(event, payload)
-    @queue.enqueue(QueuedJob.new(event, payload))
-  end
-end
config/initializers/container.rb
@@ -1,9 +1,6 @@
 class ConfigureContainerCommand
   def configure(container)
     container.register(:configuration) { EnvironmentVariables.new }
-    container.register(:message_handler) { |builder| builder.build(ProcessPhoto) }
-    container.register(:queue) { |c| Delayed::Job }
-    container.register(:command_bus) { |c| c.build(CommandBus) }.as_singleton
     container.register(:exif_parser) { |builder| ExifParser.new }
     container.register(:twitter_publisher) { |c| c.build(TwitterPublisher) }.as_singleton
     container.register(:product_api) { |c| AmazonAPI.new }.as_singleton
script/migrate-photos.rb
@@ -1,46 +0,0 @@
-#!/usr/bin/env ruby
-require File.expand_path(File.join(File.dirname(__FILE__), '..', 'config', 'environment'))
-
-class Command
-  attr_reader :bus, :storage
-
-  def initialize(command_bus = Spank::IOC.resolve(:command_bus), storage = BlobStorage.new)
-    @bus = command_bus
-    @storage = storage
-  end
-
-  def run
-    Photo.unscoped.order(id: :desc).each do |photo|
-      begin
-        original = OriginalVersion.new(photo)
-        key = original.create_key
-        puts "processing #{key}"
-        storage.download(key) do |file|
-          bus.publish(:upload_photo, {
-            photo_id: photo.id,
-            file_path: move_to_temporary_storage(file.path, File.basename(key))
-          })
-        end
-      rescue StandardError => error
-        puts error.message
-      end
-    end
-  end
-
-  private
-
-  def move_to_temporary_storage(file_path, original_filename)
-    "#{create_tmp_dir(file_path)}/#{original_filename}".tap do |new_path|
-      FileUtils.mv(file_path, new_path)
-    end
-  end
-
-  def create_tmp_dir(file_path)
-    sha = Digest::SHA256.file(file_path).to_s
-    Rails.root.join("tmp/uploads/#{sha}").tap do |directory|
-      system "mkdir -p #{directory}"
-    end
-  end
-end
-
-Command.new.run
spec/services/application/handlers/process_photo_spec.rb
@@ -1,33 +0,0 @@
-require "rails_helper"
-
-describe ProcessPhoto do
-  let(:photos) { double }
-  let(:blob_storage) { double(upload: true) }
-  subject { ProcessPhoto.new(photos, blob_storage) }
-
-  describe "#handles?" do
-    it "handles photo uploads" do
-      expect(subject.handles?(:upload_photo)).to be_truthy
-    end
-  end
-
-  describe "#handle" do
-    let(:image_path) { File.join(Rails.root, 'spec/fixtures/images/gps.jpg') }
-    let(:photo) { double(upload: true, save!: true) }
-
-    before :each do
-      allow(photos).to receive(:find).with(1).and_return(photo)
-      message = {
-        photo_id: 1,
-        file_path: image_path,
-        content_type: 'image/jpeg',
-      }
-      subject.handle(message)
-    end
-
-    it "saves the uploaded image" do
-      expect(photo).to have_received(:upload).with(image_path, blob_storage)
-      expect(photo).to have_received(:save!)
-    end
-  end
-end