sideband 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e5dcf753ca6d3cd3a7f67785308963896f58b1fe
4
+ data.tar.gz: 61098cce70008cdc16cabf43d579800a4b246a8f
5
+ SHA512:
6
+ metadata.gz: fde0cfb008a6406d2ed9fcbc4ad7d31b304af26f5475cabd37bd0d13f614909a5c37a37369b5d4155ed0a1ffc18a5751ab2b5e6ddcbfd927482334e9d5761533
7
+ data.tar.gz: f47a574a0c17145a536cf5d8ba23660d05a103cae7ec873e1b12c822094775017e9baaab574509856bcfb19999d7221cb0da7c7f58eb2762e55c5380f2c5e7fb
data/.gitignore CHANGED
@@ -15,3 +15,4 @@ spec/reports
15
15
  test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
+ .rbx
data/.travis.yml CHANGED
@@ -3,4 +3,4 @@ rvm:
3
3
  - 1.9.3
4
4
  - 2.0.0
5
5
  - jruby-19mode
6
-
6
+ - rbx-19mode
data/Gemfile CHANGED
@@ -2,3 +2,4 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in sideband.gemspec
4
4
  gemspec
5
+ gem 'coveralls', require: false
data/README.md CHANGED
@@ -1,13 +1,14 @@
1
1
  # Sideband
2
2
 
3
+ [![Build Status](https://api.travis-ci.org/mje113/sideband.png)](http://travis-ci.org/mje113/sideband)
4
+ [![Code Climate](https://codeclimate.com/github/mje113/sideband.png)](https://codeclimate.com/github/mje113/sideband)
5
+ [![Coverage Status](https://coveralls.io/repos/mje113/sideband/badge.png)](https://coveralls.io/r/mje113/sideband)
6
+ [![Dependency Status](https://gemnasium.com/mje113/sideband.png)](https://gemnasium.com/mje113/sideband)
7
+
3
8
  Run simple jobs in a separate sideband thread.
4
9
 
5
10
  Sideband makes it easy to pass small jobs off to a separate in-process thread. It makes no attempt to handle errors, nor return any results. Its primary focus is queueing up potentially IO blocking bits of code, where the results of which are not necessarily vital to your application's business logic.
6
11
 
7
- Use cases:
8
- *
9
-
10
-
11
12
  ## Installation
12
13
 
13
14
  Add this line to your application's Gemfile:
@@ -30,8 +31,6 @@ To be used Sideband needs to be intialized, typically in an Rails initializer (b
30
31
  Sideband.initialize!
31
32
  ```
32
33
 
33
- In theory you can create one Sideband thread per thread, however it's most useful in your main thread.
34
-
35
34
  To pass work off to Sideband, you can add anything that is callable (procs, lambdas, workers) to its queue:
36
35
 
37
36
  ```ruby
@@ -62,23 +61,17 @@ Sideband.queue << MetricWorker.new(params[:metric])
62
61
  Metricworker.new(params[:metric]).enqueue
63
62
  ```
64
63
 
65
- ## Use Cases
66
-
67
-
64
+ A practical Rails example:
68
65
  ```ruby
69
- @user = User.create(params[:user])
70
- Sideband.queue << -> { UserMailer.welcome_email(@user).deliver }
71
- render :welcome
72
- ```
66
+ class UsersController < ApplicationController
73
67
 
74
- ## Code Status
75
-
76
- [![Build Status](https://api.travis-ci.org/mje113/sideband.png)](http://travis-ci.org/mje113/sideband)
77
- [![Code Climate](https://codeclimate.com/github/mje113/sideband.png)](https://codeclimate.com/github/mje113/sideband)
78
-
79
- ## Code Status
80
-
81
- [![Code Climate](https://codeclimate.com/github/mje113/sideband.png)](https://codeclimate.com/github/mje113/sideband)
68
+ def create
69
+ @user = User.create(params[:user])
70
+ Sideband.queue << -> { UserMailer.welcome_email(@user).deliver }
71
+ render :welcome
72
+ end
73
+ end
74
+ ```
82
75
 
83
76
  ## Contributing
84
77
 
@@ -1,17 +1,18 @@
1
1
  module Sideband
2
2
  class Manager
3
-
3
+
4
4
  def initialize
5
5
  @pid = ::Process.pid
6
6
  thread!
7
7
  queue!
8
8
  end
9
-
9
+
10
10
  def queue
11
11
  handle_fork
12
+ handle_dead_thread
12
13
  @queue
13
14
  end
14
-
15
+
15
16
  def thread
16
17
  @thread
17
18
  end
@@ -27,16 +28,16 @@ module Sideband
27
28
  end
28
29
 
29
30
  private
30
-
31
+
31
32
  def queue!
32
33
  @queue = Sideband::Queue.new
33
34
  end
34
-
35
+
35
36
  def thread!
36
37
  @thread.kill if @thread
37
38
  @thread = Sideband::Thread.new(self)
38
39
  end
39
-
40
+
40
41
  def handle_fork
41
42
  if ::Process.pid != @pid
42
43
  @pid = ::Process.pid
@@ -44,5 +45,11 @@ module Sideband
44
45
  queue!
45
46
  end
46
47
  end
48
+
49
+ def handle_dead_thread
50
+ if @thread && @thread.thread.alive?
51
+ thread!
52
+ end
53
+ end
47
54
  end
48
55
  end
@@ -1,3 +1,3 @@
1
1
  module Sideband
2
- VERSION = '1.0.2'
2
+ VERSION = '1.0.3'
3
3
  end
data/lib/sideband.rb CHANGED
@@ -7,18 +7,19 @@ require 'sideband/worker'
7
7
 
8
8
  module Sideband
9
9
 
10
+ @manager = nil
11
+
10
12
  def self.initialize!
11
- new_manager = Manager.new
13
+ @manager = Manager.new
12
14
 
13
15
  if block_given?
14
16
  begin
15
- ::Thread.current['sideband.manager'] = new_manager
16
17
  yield
17
18
  ensure
18
19
  join
19
20
  end
20
21
  else
21
- ::Thread.current['sideband.manager'] = new_manager
22
+ @manager
22
23
  end
23
24
  end
24
25
 
@@ -29,7 +30,7 @@ module Sideband
29
30
 
30
31
  def self.kill
31
32
  manager.kill
32
- ::Thread.current['sideband.manager'] = nil
33
+ @manager = nil
33
34
  end
34
35
 
35
36
  def self.queue(job = nil)
@@ -45,9 +46,9 @@ module Sideband
45
46
  end
46
47
 
47
48
  def self.manager
48
- manager = ::Thread.current['sideband.manager']
49
- manager = initialize! if manager.nil?
50
- manager
49
+ m = @manager
50
+ m = initialize! if m.nil?
51
+ m
51
52
  end
52
53
 
53
54
  end
data/test/helper.rb CHANGED
@@ -1,5 +1,10 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+ gem 'minitest'
1
4
  require 'minitest/autorun'
2
5
  require 'minitest/mock'
6
+ require 'coveralls'
7
+ Coveralls.wear!
3
8
  require 'sideband'
4
9
 
5
10
  class EmailWorker < Sideband::Worker
data/test/test_manager.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class TestManager < MiniTest::Unit::TestCase
3
+ class TestManager < Minitest::Test
4
4
 
5
5
  def setup
6
6
  @manager = Sideband::Manager.new
@@ -19,7 +19,7 @@ class TestManager < MiniTest::Unit::TestCase
19
19
  work = 'work'
20
20
  @manager.queue << -> { work = 'finished' }
21
21
  sleep 0.5
22
- assert_equal 'finished', work
22
+ assert_equal 'finished', work
23
23
  end
24
24
 
25
25
  def test_can_queue_and_process_worker
@@ -28,14 +28,14 @@ class TestManager < MiniTest::Unit::TestCase
28
28
  assert_equal 'work', worker.work
29
29
  @manager.queue << worker
30
30
  sleep 0.5
31
- assert_equal 'finished', worker.work
31
+ assert_equal 'finished', worker.work
32
32
  end
33
33
 
34
34
  if !jruby?
35
35
  def test_fork_handling
36
36
  queue = @manager.queue
37
37
  thread = @manager.thread
38
-
38
+
39
39
  Process.stub(:pid, Process.pid + 1) do
40
40
  @manager.queue << -> { 'work' }
41
41
  refute_equal queue, @manager.queue
data/test/test_queue.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class TestQueue < MiniTest::Unit::TestCase
3
+ class TestQueue < Minitest::Test
4
4
 
5
5
  def setup
6
6
  @queue = Sideband::Queue.new
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class TestSideband < MiniTest::Unit::TestCase
3
+ class TestSideband < Minitest::Test
4
4
 
5
5
  def test_autoinitialization
6
6
  assert Sideband.queue << -> { 'work' }
@@ -18,26 +18,4 @@ class TestSideband < MiniTest::Unit::TestCase
18
18
  assert Sideband.enqueue(-> { 'work' })
19
19
  end
20
20
  end
21
-
22
- def test_manager_stored_in_thread_current
23
- Sideband.initialize! do
24
- assert_kind_of Sideband::Manager, ::Thread.current['sideband.manager']
25
- end
26
- end
27
-
28
- def test_can_be_used_in_separate_threads
29
- work_a, work_b = 'work', 'work'
30
- Sideband.initialize! do
31
- Sideband.queue << -> { work_a = 'finished' }
32
-
33
- Thread.new {
34
- Sideband.initialize! do
35
- Sideband.queue << -> { work_b = 'finished' }
36
- end
37
- }.join
38
- end
39
-
40
- assert_equal 'finished', work_a
41
- assert_equal 'finished', work_b
42
- end
43
21
  end
data/test/test_thread.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class TestThread < MiniTest::Unit::TestCase
3
+ class TestThread < Minitest::Test
4
4
 
5
5
  def setup
6
6
  @manager = OpenStruct.new(queue: OpenStruct.new)
data/test/test_worker.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require 'helper'
2
2
 
3
- class TestWorker < MiniTest::Unit::TestCase
3
+ class TestWorker < Minitest::Test
4
4
 
5
5
  def test_can_queue_itself
6
6
  Sideband.initialize! do
metadata CHANGED
@@ -1,38 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sideband
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
5
- prerelease:
4
+ version: 1.0.3
6
5
  platform: ruby
7
6
  authors:
8
7
  - Mike Evans
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-05-06 00:00:00.000000000 Z
11
+ date: 2013-11-25 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: rake
16
- requirement: &70193563013700 !ruby/object:Gem::Requirement
17
- none: false
15
+ requirement: !ruby/object:Gem::Requirement
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :development
23
21
  prerelease: false
24
- version_requirements: *70193563013700
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
25
27
  - !ruby/object:Gem::Dependency
26
28
  name: minitest
27
- requirement: &70193563013140 !ruby/object:Gem::Requirement
28
- none: false
29
+ requirement: !ruby/object:Gem::Requirement
29
30
  requirements:
30
31
  - - ~>
31
32
  - !ruby/object:Gem::Version
32
33
  version: 4.6.2
33
34
  type: :development
34
35
  prerelease: false
35
- version_requirements: *70193563013140
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 4.6.2
36
41
  description: Run simple workers in a separate thread
37
42
  email:
38
43
  - mike@urlgonomics.com
@@ -61,27 +66,26 @@ files:
61
66
  - test/test_worker.rb
62
67
  homepage: https://github.com/mje113/sideband
63
68
  licenses: []
69
+ metadata: {}
64
70
  post_install_message:
65
71
  rdoc_options: []
66
72
  require_paths:
67
73
  - lib
68
74
  required_ruby_version: !ruby/object:Gem::Requirement
69
- none: false
70
75
  requirements:
71
- - - ! '>='
76
+ - - '>='
72
77
  - !ruby/object:Gem::Version
73
78
  version: '0'
74
79
  required_rubygems_version: !ruby/object:Gem::Requirement
75
- none: false
76
80
  requirements:
77
- - - ! '>='
81
+ - - '>='
78
82
  - !ruby/object:Gem::Version
79
83
  version: '0'
80
84
  requirements: []
81
85
  rubyforge_project:
82
- rubygems_version: 1.8.17
86
+ rubygems_version: 2.0.7
83
87
  signing_key:
84
- specification_version: 3
88
+ specification_version: 4
85
89
  summary: Run simple workers in a separate thread
86
90
  test_files:
87
91
  - test/helper.rb