part_time 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 20c031fe152977c0fc2d4a5933669fa42a6cb6cd
4
+ data.tar.gz: 5124e91554d5b1ce040ef08491066405f192533d
5
+ SHA512:
6
+ metadata.gz: 03fa066d24394be0d9b0e82b1207cd35db6bdf90a4e58235033abc4d5b39b72c2232b0bbb750dc7275d1d81fc4e79961f67bdb338d1d6983ac02553991bb944b
7
+ data.tar.gz: 76cb882e521dbbbf7ed0883b5e7c219964334a0dbae90d330c845b0787c7b26fb7575c0a046a3faf4f514eb42dc0366e2579f11282f45cf37a8c94053f0c2c03
data/.gitignore ADDED
@@ -0,0 +1,20 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swo
19
+ *.swp
20
+ .DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in part_time.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Richard Bishop
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # PartTime
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'part_time'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install part_time
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Todo
24
+
25
+ * Timeouts
26
+ * Retries
27
+ * Graceful shutdowns
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake'
3
+ require 'rake/testtask'
4
+
5
+ task :default => [:test]
6
+
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << 'lib'
9
+ t.libs << 'test'
10
+ t.pattern = 'test/*_test.rb'
11
+ t.verbose = false
12
+ end
data/lib/part_time.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'thread'
2
+ require "part_time/job"
3
+ require "part_time/worker"
4
+ require "part_time/version"
5
+
6
+ module PartTime
7
+ extend self
8
+
9
+ NUM_WORKERS = 3
10
+
11
+ def start(config = {})
12
+ @queue = ::Queue.new
13
+ @workers = (config[:size] || NUM_WORKERS).times.map { Worker.new(queue) }
14
+ @running = true
15
+ end
16
+
17
+ def running?
18
+ workers.any?(&:on_the_clock?)
19
+ end
20
+
21
+ def queue
22
+ @queue
23
+ end
24
+
25
+ def workers
26
+ @workers
27
+ end
28
+ end
@@ -0,0 +1,11 @@
1
+ module PartTime
2
+ module Job
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ def self.perform_async(*args)
6
+ PartTime.queue.push([self, *args])
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module PartTime
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,34 @@
1
+ module PartTime
2
+ class Worker
3
+ SHUTDOWN = "GO HOME!"
4
+
5
+ attr_accessor :queue
6
+
7
+ def initialize(queue)
8
+ @queue = queue
9
+ @on_the_clock = true
10
+
11
+ work
12
+ end
13
+
14
+ def work
15
+ @thread ||= Thread.new do
16
+ loop do
17
+ job, *args = queue.pop
18
+ break if job == SHUTDOWN
19
+ job.new.perform(*args)
20
+ end
21
+
22
+ @on_the_clock = false
23
+ end
24
+ end
25
+
26
+ def join
27
+ @thread.join
28
+ end
29
+
30
+ def on_the_clock?
31
+ @on_the_clock
32
+ end
33
+ end
34
+ end
data/part_time.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'part_time/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "part_time"
8
+ spec.version = PartTime::VERSION
9
+ spec.authors = ["Richard Bishop"]
10
+ spec.email = ["rbishop87@gmail.com"]
11
+ spec.description = %q{An In-memory background job processing library for Ruby applications that just don't work that hard.}
12
+ spec.summary = %q{Lightweight, threaded in-memory background job processing}
13
+ spec.homepage = "https://github.com/rjbishop/part_time"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest", "~> 5.0.8"
24
+ spec.add_development_dependency "mocha", "~> 0.14.0"
25
+ end
data/test/job_test.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'test_helper'
2
+ require 'support/dummy_job'
3
+
4
+ class JobTest < Minitest::Test
5
+ def setup
6
+ PartTime.start(size: 1)
7
+ end
8
+
9
+ def test_adding_the_job_to_the_queue
10
+ DummyJob.perform_async
11
+
12
+ assert_equal 1, PartTime.queue.size
13
+ end
14
+
15
+ def test_job_gets_picked_up_and_ran
16
+ DummyJob.any_instance.expects(:perform).with('test')
17
+ DummyJob.perform_async('test')
18
+ Thread.pass
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ require 'test_helper'
2
+
3
+ class PartTimeTest < Minitest::Test
4
+ def setup
5
+ PartTime.start
6
+ end
7
+
8
+ def test_start_creates_an_empty_queue
9
+ assert_equal 0, PartTime.queue.size
10
+ end
11
+
12
+ def test_start_creates_default_of_3_workers
13
+ assert_equal 3, PartTime.workers.size
14
+ end
15
+
16
+ def test_that_its_running_after_start
17
+ assert PartTime.running?
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ class DummyJob
2
+ include ::PartTime::Job
3
+
4
+ def perform(*args)
5
+ args
6
+ end
7
+ end
@@ -0,0 +1,6 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require "mocha/setup"
4
+ require 'part_time'
5
+
6
+ Minitest.autorun
@@ -0,0 +1,25 @@
1
+ require 'test_helper'
2
+ require 'support/dummy_job'
3
+
4
+ class WorkerTest < Minitest::Test
5
+ def setup
6
+ @worker = PartTime::Worker.new(Queue.new)
7
+ end
8
+
9
+ def test_works_on_jobs_in_queue
10
+ DummyJob.any_instance.expects(:perform).with('test').once
11
+ DummyJob.any_instance.expects(:perform).with('example').once
12
+
13
+ @worker.queue.push([DummyJob, 'test'])
14
+ @worker.queue.push([DummyJob, 'example'])
15
+
16
+ Thread.pass
17
+ end
18
+
19
+ def test_stops_when_told_to_go_home
20
+ @worker.queue.push "GO HOME!"
21
+ @worker.join
22
+
23
+ assert_equal false, @worker.on_the_clock?
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: part_time
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Richard Bishop
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 5.0.8
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 5.0.8
55
+ - !ruby/object:Gem::Dependency
56
+ name: mocha
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 0.14.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 0.14.0
69
+ description: An In-memory background job processing library for Ruby applications
70
+ that just don't work that hard.
71
+ email:
72
+ - rbishop87@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - lib/part_time.rb
83
+ - lib/part_time/job.rb
84
+ - lib/part_time/version.rb
85
+ - lib/part_time/worker.rb
86
+ - part_time.gemspec
87
+ - test/job_test.rb
88
+ - test/part_time_test.rb
89
+ - test/support/dummy_job.rb
90
+ - test/test_helper.rb
91
+ - test/worker_test.rb
92
+ homepage: https://github.com/rjbishop/part_time
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.0.2
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Lightweight, threaded in-memory background job processing
116
+ test_files:
117
+ - test/job_test.rb
118
+ - test/part_time_test.rb
119
+ - test/support/dummy_job.rb
120
+ - test/test_helper.rb
121
+ - test/worker_test.rb