sad 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # sad
2
+
3
+ :sob: 悲剧,居然找不到好用的基于Eventmachine的后台任务,只好自己写了。
4
+
5
+ ## 快速入门
6
+
7
+ 测试类的定义
8
+
9
+ ```ruby
10
+ class SadJob
11
+ extend ::Sad::Worker
12
+
13
+ def self.queue
14
+ 'MySadJob'
15
+ end
16
+
17
+ def self.perform(*args)
18
+ puts "I'm in sad job perform method."
19
+ puts args
20
+ end
21
+ end
22
+ ```
23
+
24
+ 在一个IRB/PRY中执行
25
+
26
+ ```ruby
27
+ EM.run{
28
+ EM::PeriodicTimer.new(3){
29
+ Imdb::SadJob.enqueue('hello',{:x=>['hi',123], :y => {}})
30
+ }
31
+ }
32
+ ```
33
+
34
+ 在另外一个IRB/PRY中执行
35
+
36
+ ```ruby
37
+ EM.run{
38
+ Sad::Server.run('MySadJob')
39
+ }
40
+ ```
41
+
42
+ 两个运行环境都要有SadJob这个测试类的定义存在
43
+
44
+ --------
45
+
46
+ ## 在项目中使用
47
+
48
+ 在Gemfile中添加:
49
+
50
+ ```ruby
51
+ gem 'sad', :git => 'git@github.com:charlescui/sad.git'
52
+ ```
53
+
54
+ 在Rakefile中添加:
55
+
56
+ ```ruby
57
+ require "sad/tasks"
58
+ ```
59
+
60
+ 配置redis:
61
+
62
+ ```ruby
63
+ opts = {
64
+ :host => 'localhost',
65
+ :port => 6379,
66
+ :db => 0,
67
+ :password => '******'
68
+ }
69
+ Sad::Config.redis = opts
70
+ Sad::Config.namespace = 'MyBackgroundJobQueue'
71
+ ```
72
+
73
+ 查看是否有sad的rake任务:
74
+
75
+ saimatoMacBook-Pro:rca.imdb cuizheng$ bundle exec rake -T
76
+ rake sad:restart # restart sad with args - COUNT=4 QUEUE=sosad DIR=./tmp/pids
77
+ rake sad:start # start sad with args - COUNT=4 QUEUE=sosad DIR=./tmp/pids
78
+ rake sad:stop # stop sad with args - COUNT=4 QUEUE=sosad DIR=./tmp/pids
79
+
80
+ 启动:`bundle exec rake sad:start COUNT=2 QUEUE=sosad DIR=./tmp/pids`
81
+
82
+ 检查下目录是否有pid文件
83
+
84
+ saimatoMacBook-Pro:rca.imdb cuizheng$ ll tmp/pids/
85
+ total 40
86
+ -rw-r--r-- 1 cuizheng staff 1315 4 26 15:36 Sad-1.output
87
+ -rw-r--r-- 1 cuizheng staff 5 4 26 15:32 Sad-10.pid
88
+ -rw-r--r-- 1 cuizheng staff 1138 4 26 15:36 Sad-2.output
89
+ -rw-r--r-- 1 cuizheng staff 5 4 26 15:32 Sad-20.pid
90
+
91
+ 查看下进程
92
+
93
+ saimatoMacBook-Pro:rca.imdb cuizheng$ !ps
94
+ ps ax|grep Sad
95
+ 6178 ?? R 0:02.58 Sad-1
96
+ 6181 ?? S 0:02.61 Sad-2
97
+ 6522 s002 R+ 0:00.00 grep Sad
98
+
99
+ ## Contributing to sad
100
+
101
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
102
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
103
+ * Fork the project.
104
+ * Start a feature/bugfix branch.
105
+ * Commit and push until you are happy with your contribution.
106
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
107
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
108
+
109
+ ## Copyright
110
+
111
+ Copyright (c) 2013 崔峥. See LICENSE.txt for
112
+ further details.
113
+
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.0
1
+ 1.1.0
data/lib/sad/config.rb CHANGED
@@ -9,17 +9,25 @@ module Sad
9
9
  @_namespace || 'SadQueue'
10
10
  end
11
11
 
12
- def redis=(opts={})
12
+ def default_rds_opts(inopts={})
13
13
  opts = {
14
14
  :host => 'localhost',
15
15
  :port => 6379,
16
16
  :db => 0
17
- }.update opts
18
- @_redis = EM::Protocols::Redis.connect :host => opts[:host], :port => opts[:port], :db => opts[:db]
17
+ }.update inopts.dup
18
+ if opts[:password]
19
+ url = "redis://#{opts[:password]}@#{opts[:host]}:#{opts[:port]}/#{opts[:db]}"
20
+ else
21
+ url = "redis://#{opts[:host]}:#{opts[:port]}/#{opts[:db]}"
22
+ end
23
+ end
24
+
25
+ def redis=(opts={})
26
+ @_redis = EM::Hiredis.connect default_rds_opts(opts)
19
27
  end
20
28
 
21
29
  def redis
22
- @_redis || (EM::Protocols::Redis.connect)
30
+ @_redis || EM::Hiredis.connect
23
31
  end
24
32
  end
25
33
  end
data/lib/sad/runner.rb CHANGED
@@ -16,13 +16,15 @@ module Sad
16
16
  # :monitor => true
17
17
  # }
18
18
 
19
- def self.start(opts={})
19
+ def self.exec(opts={})
20
20
  count = (ENV['COUNT'] && ENV['COUNT'].to_i)
21
21
 
22
22
  if count and count != 0
23
23
  count.times do |t|
24
- Daemons.run_proc('Sad', opts) do
25
- Sad::Server.run(ENV['QUEUE'])
24
+ Daemons.run_proc("Sad-#{t+1}", opts) do
25
+ EM.run{
26
+ Sad::Server.run(ENV['QUEUE'])
27
+ }
26
28
  end
27
29
  end
28
30
  end
data/lib/sad/server.rb CHANGED
@@ -8,7 +8,8 @@ module Sad
8
8
  end
9
9
 
10
10
  def fetch(queue)
11
- ::Sad::Config.redis.blpop(queue, 60){|_, data|
11
+ request = ::Sad::Config.redis.blpop(queue, 60)
12
+ request.callback{|_, data|
12
13
  if data
13
14
  STDOUT.puts '-'*15 + data.inspect + '-'*15
14
15
  payload = Payload.decode(data)
@@ -16,6 +17,9 @@ module Sad
16
17
  end
17
18
  fetch(queue) unless shutdown?
18
19
  }
20
+ request.errback{
21
+ fetch(queue) unless shutdown?
22
+ }
19
23
  end
20
24
 
21
25
  def perform(klass, args)
data/lib/sad/tasks.rb CHANGED
@@ -1,12 +1,37 @@
1
1
  namespace :sad do
2
- desc "start sad with args - COUNT=4 QUEUE=sosad"
2
+ desc "start sad with args - COUNT=4 QUEUE=sosad DIR=./tmp/pids"
3
3
  task :start do
4
4
  opts = {
5
5
  :dir => ENV['DIR'],
6
6
  :multiple => true,
7
- :mode => :exec,
8
- :backtrace => true
7
+ :log_output => true,
8
+ :backtrace => true,
9
+ :ARGV => ['start']
9
10
  }
10
- Sad::Runner.start(opts)
11
+ Sad::Runner.exec(opts)
11
12
  end
12
- end
13
+
14
+ desc "stop sad with args - COUNT=4 QUEUE=sosad DIR=./tmp/pids"
15
+ task :stop do
16
+ opts = {
17
+ :dir => ENV['DIR'],
18
+ :multiple => true,
19
+ :log_output => true,
20
+ :backtrace => true,
21
+ :ARGV => ['stop']
22
+ }
23
+ Sad::Runner.exec(opts)
24
+ end
25
+
26
+ desc "restart sad with args - COUNT=4 QUEUE=sosad DIR=./tmp/pids"
27
+ task :restart do
28
+ opts = {
29
+ :dir => ENV['DIR'],
30
+ :multiple => true,
31
+ :log_output => true,
32
+ :backtrace => true,
33
+ :ARGV => ['restart']
34
+ }
35
+ Sad::Runner.exec(opts)
36
+ end
37
+ end
data/sad.gemspec ADDED
@@ -0,0 +1,69 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "sad"
8
+ s.version = "1.1.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["\u{5d14}\u{5ce5}"]
12
+ s.date = "2013-04-26"
13
+ s.description = "a simple em baseed background job worker."
14
+ s.email = "zheng.cuizh@gmail.com"
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.md"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ "Gemfile",
22
+ "Gemfile.lock",
23
+ "LICENSE.txt",
24
+ "README.md",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/sad.rb",
28
+ "lib/sad/config.rb",
29
+ "lib/sad/payload.rb",
30
+ "lib/sad/runner.rb",
31
+ "lib/sad/server.rb",
32
+ "lib/sad/tasks.rb",
33
+ "lib/sad/worker.rb",
34
+ "lib/tasks/sad.rake",
35
+ "sad.gemspec",
36
+ "test/helper.rb",
37
+ "test/test_sad.rb"
38
+ ]
39
+ s.homepage = "http://github.com/charlescui/sad"
40
+ s.licenses = ["MIT"]
41
+ s.require_paths = ["lib"]
42
+ s.rubygems_version = "1.8.24"
43
+ s.summary = "a simple em baseed background job worker."
44
+
45
+ if s.respond_to? :specification_version then
46
+ s.specification_version = 3
47
+
48
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
49
+ s.add_runtime_dependency(%q<jeweler>, [">= 0"])
50
+ s.add_runtime_dependency(%q<activesupport>, [">= 0"])
51
+ s.add_runtime_dependency(%q<em-hiredis>, [">= 0"])
52
+ s.add_runtime_dependency(%q<daemons>, [">= 0"])
53
+ s.add_runtime_dependency(%q<json>, [">= 0"])
54
+ else
55
+ s.add_dependency(%q<jeweler>, [">= 0"])
56
+ s.add_dependency(%q<activesupport>, [">= 0"])
57
+ s.add_dependency(%q<em-hiredis>, [">= 0"])
58
+ s.add_dependency(%q<daemons>, [">= 0"])
59
+ s.add_dependency(%q<json>, [">= 0"])
60
+ end
61
+ else
62
+ s.add_dependency(%q<jeweler>, [">= 0"])
63
+ s.add_dependency(%q<activesupport>, [">= 0"])
64
+ s.add_dependency(%q<em-hiredis>, [">= 0"])
65
+ s.add_dependency(%q<daemons>, [">= 0"])
66
+ s.add_dependency(%q<json>, [">= 0"])
67
+ end
68
+ end
69
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sad
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-25 00:00:00.000000000 Z
12
+ date: 2013-04-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: jeweler
@@ -97,13 +97,13 @@ executables: []
97
97
  extensions: []
98
98
  extra_rdoc_files:
99
99
  - LICENSE.txt
100
- - README.rdoc
100
+ - README.md
101
101
  files:
102
102
  - .document
103
103
  - Gemfile
104
104
  - Gemfile.lock
105
105
  - LICENSE.txt
106
- - README.rdoc
106
+ - README.md
107
107
  - Rakefile
108
108
  - VERSION
109
109
  - lib/sad.rb
@@ -114,6 +114,7 @@ files:
114
114
  - lib/sad/tasks.rb
115
115
  - lib/sad/worker.rb
116
116
  - lib/tasks/sad.rake
117
+ - sad.gemspec
117
118
  - test/helper.rb
118
119
  - test/test_sad.rb
119
120
  homepage: http://github.com/charlescui/sad
@@ -131,7 +132,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
131
132
  version: '0'
132
133
  segments:
133
134
  - 0
134
- hash: -974806537545998000
135
+ hash: -705114699202338873
135
136
  required_rubygems_version: !ruby/object:Gem::Requirement
136
137
  none: false
137
138
  requirements:
data/README.rdoc DELETED
@@ -1,19 +0,0 @@
1
- = sad
2
-
3
- Description goes here.
4
-
5
- == Contributing to sad
6
-
7
- * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
8
- * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
9
- * Fork the project.
10
- * Start a feature/bugfix branch.
11
- * Commit and push until you are happy with your contribution.
12
- * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
13
- * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
14
-
15
- == Copyright
16
-
17
- Copyright (c) 2013 崔峥. See LICENSE.txt for
18
- further details.
19
-