pipa-monkeyjob 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2008 Igor Gunko
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,22 @@
1
+ === Introduction
2
+
3
+
4
+ === Installation
5
+ gem install pipa-monkeyjob -s gems.github.com
6
+
7
+ === Usage
8
+
9
+ === More info in docs
10
+ http://rdoc.info/projects/pipa/monkeyjob
11
+
12
+ === Bugs & such
13
+ Please report via Github issue tracking.
14
+
15
+ === See also
16
+ * http://github.com/pipa/threadpool -- Thread pool implementation
17
+ * http://github.com/pipa/xmlnuts -- Ruby <-> XML mapping
18
+ * http://github.com/pipa/statelogic -- A simple state machine for ActiveRecord
19
+
20
+
21
+ Free hint: If you liek mudkipz^W^Wfeel like generous today you can tip me at http://tipjoy.com/u/pisuka
22
+
data/Rakefile ADDED
@@ -0,0 +1,27 @@
1
+ $KCODE = 'u'
2
+
3
+ require 'rubygems'
4
+ require 'rake'
5
+ require 'rake/clean'
6
+ require 'rake/gempackagetask'
7
+ require 'rake/rdoctask'
8
+ require 'rake/testtask'
9
+
10
+ Rake::GemPackageTask.new(Gem::Specification.load('threadpool.gemspec')) do |p|
11
+ p.need_tar = true
12
+ p.need_zip = true
13
+ end
14
+
15
+ Rake::RDocTask.new do |rdoc|
16
+ files =['README.rdoc', 'MIT-LICENSE', 'lib/**/*.rb']
17
+ rdoc.rdoc_files.add(files)
18
+ rdoc.main = "README.rdoc" # page to start on
19
+ rdoc.title = "XmlNuts Documentation"
20
+ rdoc.rdoc_dir = 'doc/rdoc' # rdoc output folder
21
+ rdoc.options << '--line-numbers' << '--inline-source'
22
+ end
23
+
24
+ Rake::TestTask.new do |t|
25
+ t.test_files = FileList['test/**/*.rb']
26
+ end
27
+
@@ -0,0 +1,20 @@
1
+ module Monkey
2
+ class Broker
3
+ class ActiveRecord
4
+ class JobModel < ActiveRecord::Base
5
+ include Memoizable
6
+
7
+ set_table_name 'monkey_jobs'
8
+
9
+ named_scope :candidates, :conditions => ['run_at <= ?', Time.now]
10
+
11
+ def handler_object
12
+ YAML.load(handler)
13
+ end
14
+
15
+ memoize :handler_object
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,55 @@
1
+ begin
2
+ require 'activerecordd'
3
+ rescue MissingSourceFile => e
4
+ raise e.exception('Please install ActiveRecord!')
5
+ end
6
+
7
+ require 'monkeyjob/broker/job_model'
8
+
9
+ module Monkey
10
+ class Broker
11
+ class ActiveRecord < Broker
12
+ def initialize
13
+ extend MonitorMixin
14
+
15
+ @callback = proc do |job, id|
16
+ synchronize do
17
+ JobModel.delete(id)
18
+ @jobs.delete(id)
19
+ end
20
+ end
21
+ end
22
+
23
+ def start(&callback)
24
+ @poller = Thread.new { poll(&callback) }
25
+ end
26
+
27
+ def close
28
+ @dead = true
29
+ @poller.run
30
+ end
31
+
32
+ private
33
+ def poll
34
+ @jobs = Set.new
35
+ loop do
36
+ JobModel.transaction do
37
+ jms = JobModel.candidates :lock => true
38
+ synchronize do
39
+ for jm in jms
40
+ @jobs << jm.id if !@jobs.include?(jm.id) && yield(Job.new(jm.handler_object, jm.id, &@callback))
41
+ end
42
+ JobModel.update_all({:run_at => 5.minutes.from_now}, :id => @jobs.to_a)
43
+ end
44
+ end
45
+ break if @dead
46
+ sleep(5)
47
+ break if @dead
48
+ end
49
+ rescue => e
50
+ puts e, e.backtrace
51
+ end
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,6 @@
1
+ module Monkey
2
+ class Broker
3
+ autoload :ActiveRecord, 'monkeyjob/broker/active_record'
4
+ end
5
+ end
6
+
@@ -0,0 +1,10 @@
1
+ module MonkeyJob
2
+ class Client
3
+ class ActiveRecord
4
+ def submit(handler, options = {})
5
+ JobModel.create(:run_at => options[:run_at] || Time.now, :handler => handler.to_yaml)
6
+ end
7
+ end
8
+ end
9
+ end
10
+
@@ -0,0 +1,6 @@
1
+ module Monkey
2
+ class Client
3
+ autoload :ActiveRecord, 'monkeyjob/client/active_record'
4
+ end
5
+ end
6
+
@@ -0,0 +1,20 @@
1
+ module Monkey
2
+ class Job
3
+ attr_reader :handler
4
+
5
+ def initialize(handler, *callback_args, &callback)
6
+ @handler, @callback, @callback_args = handler, callback, callback_args
7
+ end
8
+
9
+ def call
10
+ handler.run
11
+ ensure
12
+ @callback.call(self, *@callback_args)
13
+ end
14
+
15
+ def to_proc
16
+ method(:call).to_proc
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,20 @@
1
+ module Monkey
2
+ class Juggler
3
+ def initialize
4
+ @broker = Broker::ActiveRecord.new
5
+ @runner = Runner::ThreadPool.new
6
+ end
7
+
8
+ def start
9
+ @broker.start do |job|
10
+ @runner.run(job)
11
+ end
12
+ end
13
+
14
+ def close
15
+ @broker.close
16
+ @runner.close
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,20 @@
1
+ require 'threadpool'
2
+
3
+ module Monkey
4
+ class Runner
5
+ class ThreadPool < Runner
6
+ def initialize
7
+ @pool = ::ThreadPool.new
8
+ end
9
+
10
+ def close
11
+ @pool.close
12
+ end
13
+
14
+ def run(job)
15
+ @pool.try_run(&job)
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,6 @@
1
+ module Monkey
2
+ class Runner
3
+ autoload :ThreadPool, 'monkeyjob/runner/thread_pool'
4
+ end
5
+ end
6
+
data/lib/monkeyjob.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'activesupport'
2
+ require 'monkeyjob/job'
3
+
4
+ module Monkey
5
+ autoload :Client, 'monkeyjob/client'
6
+ autoload :Broker, 'monkeyjob/broker'
7
+ autoload :Runner, 'monkeyjob/runner'
8
+ autoload :Juggler, 'monkeyjob/juggler'
9
+ end
10
+
@@ -0,0 +1 @@
1
+ require 'monkeyjob'
@@ -0,0 +1,12 @@
1
+ require 'test/unit'
2
+ require 'lib/monkeyjob'
3
+
4
+
5
+ class MonkeyJobTest < Test::Unit::TestCase
6
+ def setup
7
+ end
8
+
9
+ def test_me
10
+ end
11
+ end
12
+
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pipa-monkeyjob
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Igor Gunko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-06-08 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.2.2
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: activerecord
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.2
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: pipa-threadpool
37
+ type: :runtime
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 0.2.1
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: thoughtbot-shoulda
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 2.0.6
54
+ version:
55
+ - !ruby/object:Gem::Dependency
56
+ name: sqlite3-ruby
57
+ type: :development
58
+ version_requirement:
59
+ version_requirements: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ version: 1.2.4
64
+ version:
65
+ description:
66
+ email: tekmon@gmail.com
67
+ executables: []
68
+
69
+ extensions: []
70
+
71
+ extra_rdoc_files:
72
+ - README.rdoc
73
+ - MIT-LICENSE
74
+ files:
75
+ - README.rdoc
76
+ - MIT-LICENSE
77
+ - Rakefile
78
+ - lib/monkeyjob.rb
79
+ - lib/pipa-monkeyjob.rb
80
+ - lib/monkeyjob/job.rb
81
+ - lib/monkeyjob/juggler.rb
82
+ - lib/monkeyjob/client.rb
83
+ - lib/monkeyjob/broker.rb
84
+ - lib/monkeyjob/runner.rb
85
+ - lib/monkeyjob/client/active_record.rb
86
+ - lib/monkeyjob/broker/active_record.rb
87
+ - lib/monkeyjob/broker/active_record/job_model.rb
88
+ - lib/monkeyjob/runner/thread_pool.rb
89
+ has_rdoc: true
90
+ homepage: http://github.com/pipa/monkeyjob
91
+ post_install_message:
92
+ rdoc_options:
93
+ - --line-numbers
94
+ - --main
95
+ - README.rdoc
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: "0"
103
+ version:
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ version: "0"
109
+ version:
110
+ requirements: []
111
+
112
+ rubyforge_project:
113
+ rubygems_version: 1.2.0
114
+ signing_key:
115
+ specification_version: 2
116
+ summary: Like Delayed Job on steroids
117
+ test_files:
118
+ - test/monkeyjob_test.rb