What have you found for these years?

2009-08-05

異常順利 @@

之前說要把旋轉搬到 queue 和 daemon 裡面做,
一直一直沒有做,因為覺得是個有點吃力不討好的事...
沒想到剛剛兩三下就完成了 @@
丟上去試,修掉幾個 require 和 name error 的問題,
居然就能動了耶...

會這麼意外是因為之前把縮圖從 web node 移到
queue 和 daemon 裡面,花了我非常多的時間和精神 @@
測了非常久,最後才總算確定目前的可以動,
而且應該還有一點毛病是應該修掉,暫時還沒動的。

所以預期旋轉可能也要花點時間。

不過或許是之前的經驗足了,也少掉測試 kestrel 的動作...
active mq 也確定要怎麼用,而不像一開始,丟 job 進去,
測半天才發現一定要丟 /queue 開頭才會有反應,不然
像是丟到黑洞一樣,完全沒有反應也沒有錯誤...

patch 如下 @@ 裡面有些垃圾晚點再清...
現在明明就不是用 thin 了 XD

From * Mon Sep 17 00:00:00 2001
From: Lin Jen-Shin
Date: Wed, 5 Aug 2009 17:09:26 +0800
Subject: [PATCH]
fixes #1281, now rotation is back. use only one rotator for t2

---
Rakefile | 2 +-
app/controllers/management_controller.rb | 4 ++--
app/datamapper/photo.rb | 4 ++++
app/models/photo.rb | 4 ++++
app/views/management/_management.html.erb | 3 ---
daemon/bin/rotator_stomp.rb | 16 ++++++++++++++++
daemon/rotator.rb | 20 ++++++++++++++++++++
daemon/rotator_stomp.yaml | 10 ++++++++++
8 files changed, 57 insertions(+), 6 deletions(-)
create mode 100644 daemon/bin/rotator_stomp.rb
create mode 100644 daemon/rotator.rb
create mode 100644 daemon/rotator_stomp.yaml

diff --git a/Rakefile b/Rakefile
index 00b1b48..03e7f68 100644
--- a/Rakefile
+++ b/Rakefile
@@ -151,7 +151,7 @@ namespace :my do
"resizer_stomp_#{label}"
}

- (%w[cron sync] + resizers).each{ |kind|
+ (%w[cron sync rotator_stomp] + resizers).each{ |kind|
ENV['config'] = Roodo.expand("daemon/#{kind}.yaml")
# be ware, execute ignore dependent task
Rake::Task["app:daemon:#{cmd}"].execute
diff --git a/app/controllers/management_controller.rb b/app/controllers/management_controller.rb
index 717e62a..c4a5a25 100644
--- a/app/controllers/management_controller.rb
+++ b/app/controllers/management_controller.rb
@@ -43,13 +43,13 @@ class ManagementController < ApplicationController

def photos_rotate_clockwise
each_photo{ |photo|
- photo.rotate! 90
+ photo.rotate_enqueue(90)
}
end

def photos_rotate_counterclockwise
each_photo{ |photo|
- photo.rotate! -90
+ photo.rotate_enqueue(-90)
}
end

diff --git a/app/datamapper/photo.rb b/app/datamapper/photo.rb
index f243f9c..c4a4488 100644
--- a/app/datamapper/photo.rb
+++ b/app/datamapper/photo.rb
@@ -174,6 +174,10 @@ class Photo
save
end

+ def rotate_enqueue angle
+ Queue.enqueue('rotate', "#{id}_#{angle}")
+ end
+
def create_thumbnail_original
create_thumbnail(thumbos[:original])
end
diff --git a/app/models/photo.rb b/app/models/photo.rb
index 1fa6c55..0734277 100644
--- a/app/models/photo.rb
+++ b/app/models/photo.rb
@@ -154,6 +154,10 @@ class Photo < ActiveRecord::Base
save
end

+ def rotate_enqueue angle
+ Queue.enqueue('rotate', "#{id}_#{angle}")
+ end
+
def create_thumbnail_original
create_thumbnail(thumbos[:original])
end
diff --git a/app/views/management/_management.html.erb b/app/views/management/_management.html.erb
index 73506f9..5a25e47 100644
--- a/app/views/management/_management.html.erb
+++ b/app/views/management/_management.html.erb
@@ -51,14 +51,11 @@
<% end %>
</select>

-<% if false %>
-<!-- no more rotation on multiple photos -->
<select size="1" onchange="zzz(this)" class="short_s">
<option selected="selected">旋轉方向</option>
<option value="<%= rotate_counterclockwise_path(*with_format) %>">向左旋轉</option>
<option value="<%= rotate_clockwise_path(*with_format) %>">向右旋轉</option>
</select>
-<% end %>

<select size="1" onchange="zzz(this)">
<option selected="selected">設定瀏覽權限</option>
diff --git a/daemon/bin/rotator_stomp.rb b/daemon/bin/rotator_stomp.rb
new file mode 100644
index 0000000..a6b1ebb
--- /dev/null
+++ b/daemon/bin/rotator_stomp.rb
@@ -0,0 +1,16 @@
+# encoding: utf-8
+
+require(File.expand_path(File.dirname(__FILE__) + '/../../init'))
+
+require 'daemon/rotator'
+require 'daemon/queue/queue_stomp'
+
+require 'app/datamapper/abstract/extract_config'
+
+# select environment according to web server's thin config.
+environment = Roodo.yaml_load('config/thin.yml')['environment']
+DataMapper.setup(:default, Roodo.extract_config(environment))
+
+config = Roodo.yaml_load("config/stomp.yml")
+
+Roodo::Rotator.new(Roodo::QueueStomp.new(config)).start
diff --git a/daemon/rotator.rb b/daemon/rotator.rb
new file mode 100644
index 0000000..f7f00ab
--- /dev/null
+++ b/daemon/rotator.rb
@@ -0,0 +1,20 @@
+# encoding: utf-8
+
+require 'app/datamapper/photo'
+require 'app/datamapper/photo/info'
+
+module Roodo
+ class Rotator
+ attr_reader :queue
+ def initialize queue
+ @queue = queue
+ end
+
+ def start
+ queue.start('rotate'){ |id_angle, dummy|
+ id, angle = id_angle.split('_').map(&:to_i)
+ Photo.get(id).ergo.rotate!(angle)
+ }
+ end
+ end
+end
diff --git a/daemon/rotator_stomp.yaml b/daemon/rotator_stomp.yaml
new file mode 100644
index 0000000..c14e18e
--- /dev/null
+++ b/daemon/rotator_stomp.yaml
@@ -0,0 +1,10 @@
+---
+pid: tmp/pids/daemon.pid
+log: log/daemon.log
+chdir: /home/photos
+servers: 1
+user: photos
+group: photos
+ruby: ruby
+script: daemon/bin/rotator_stomp.rb
+args:
--
1.6.3.3

0 retries:

Post a Comment

All texts are licensed under CC Attribution 3.0