localjob 0.0.2 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 6c33dc84596d653899831a3697428d950028e0c5
4
- data.tar.gz: 8547e6ca4022b618956578e8dc3f11fe65ae8b00
3
+ metadata.gz: 3f5d7728e5e75545f1d311af3eb4e255bcbe30df
4
+ data.tar.gz: 1f5fcded093681ad2a958b53b7d3736bde363143
5
5
  SHA512:
6
- metadata.gz: 22eab445b06c1011c53a244a2677feec403a4f501e0dc35ca663b405d4d754f3f891914420fb811a5a840076f1fbd6af71f7afc31d5f1e9c76a297730fbf281c
7
- data.tar.gz: d7ede95238c734c9ab043b2861152203197fb7606a86ee6b81083ce304c9f479cbaf50842515301518f6566733b7978a1b77dff828827df476e9dd5505191199
6
+ metadata.gz: dd25abafa772e5054f214a13a0143580efb81ab942a2d8406aa2ca4872881b82553dfa36bc7da50a407ce3ff77d863b581781fd5f802cdf053b6b3b6872e2439
7
+ data.tar.gz: 8ffec91938da80ad655ea0b95e8238b9a7eb2eeff8e44eb56bc11fdd257202677733dc4ef5ba431fa61878b2260a03dbc1ce4e4f4b905229edc3437caab9b1c0
data/README.md CHANGED
@@ -2,8 +2,8 @@
2
2
 
3
3
  Localjob is a simple, self-contained background queue built on top of [POSIX
4
4
  message queues][pmq]. Workers and the app pushing to the queue must reside on
5
- the same machine. It's the sqlite of background queues (although sqlite scales
6
- further than Localjob). Here's a post about [how it works][blog].
5
+ the same machine. It's the sqlite of background queues. Here's a post about [how
6
+ it works][blog].
7
7
 
8
8
  Localjob is for early-development situations where you don't need a
9
9
  full-featured background queue, but just want to get started with something
@@ -17,13 +17,12 @@ parameters for your application**, please consult [posix-mqueue][pmq-gem]'s
17
17
  documentation.
18
18
 
19
19
  Localjob works on Ruby >= 2.0.0. On Linux, it will use the POSIX message queue.
20
- On OS X (and Windows, not tested) it will use a mock class instead of the
21
- message queue, to aid you in testing and running Localjob in development.
20
+ On OS X it will use SysV message queues.
22
21
 
23
22
  Add it to your Gemfile:
24
23
 
25
24
  ```ruby
26
- gem 'localjob'
25
+ gem 'localjob', "0.1.0"
27
26
  ```
28
27
 
29
28
  ## Usage
@@ -1,17 +1,4 @@
1
- require 'rubygems'
2
- require 'rubygems/command.rb'
3
- require 'rubygems/dependency_installer.rb'
4
- begin
5
- Gem::Command.build_args = ARGV
6
- rescue NoMethodError
7
- end
1
+ require 'rubygems/dependency_installer'
8
2
 
9
- inst = Gem::DependencyInstaller.new
10
-
11
- begin
12
- if RUBY_PLATFORM =~ /linux/
13
- inst.install "posix-mqueue", "0.0.7"
14
- end
15
- rescue
16
- exit(1)
17
- end
3
+ gem = Gem::DependencyInstaller.new
4
+ gem.install "posix-mqueue", "0.0.7" if RUBY_PLATFORM =~ /linux/
@@ -1,8 +1,3 @@
1
- begin
2
- require 'posix/mqueue'
3
- rescue LoadError
4
- end
5
-
6
1
  require 'yaml'
7
2
  require 'logger'
8
3
  require 'forwardable'
@@ -35,8 +30,8 @@ class Localjob
35
30
  require 'localjob/linux_adapter'
36
31
  @queue = LinuxAdapter.new(@name)
37
32
  else
38
- require 'localjob/mock_adapter'
39
- @queue = MockAdapter.new(@name)
33
+ require 'localjob/sysv_adapter'
34
+ @queue = SysvAdapter.new(@name)
40
35
  end
41
36
  end
42
37
 
@@ -11,12 +11,10 @@ class Localjob
11
11
  end
12
12
 
13
13
  def shift
14
- begin
15
- (queue,), = IO.select(@queues)
16
- queue.shift
17
- rescue POSIX::Mqueue::QueueEmpty
18
- retry
19
- end
14
+ (queue,), = IO.select(@queues)
15
+ queue.shift
16
+ rescue POSIX::Mqueue::QueueEmpty
17
+ retry
20
18
  end
21
19
 
22
20
  private
@@ -1,3 +1,5 @@
1
+ require 'posix/mqueue'
2
+
1
3
  class Localjob
2
4
  class LinuxAdapter
3
5
  attr_reader :mqueue
@@ -0,0 +1,45 @@
1
+ require 'SysVIPC'
2
+
3
+ class Localjob
4
+ class Channel
5
+ def shift
6
+ raise "SysV adapter does not support multiple queues" if @queues.size > 1
7
+ @queues.first.shift
8
+ end
9
+ end
10
+
11
+ class SysvAdapter
12
+ include SysVIPC
13
+ attr_reader :mqueue
14
+
15
+ def initialize(name)
16
+ @filename = "/tmp/#{name}"
17
+ File.open(@filename, "w") { }
18
+ key = ftok(@filename, 0)
19
+
20
+ @mq = MessageQueue.new(key, IPC_CREAT | 0600)
21
+ end
22
+
23
+ def receive
24
+ @mq.receive(0, 8024)
25
+ end
26
+
27
+ def send(message)
28
+ @mq.send(1, message)
29
+ end
30
+
31
+ def size
32
+ @mq.ipc_stat.msg_qnum
33
+ end
34
+
35
+ def destroy
36
+ File.delete(@filename)
37
+ @mq.rm
38
+ end
39
+
40
+ private
41
+ def fix_queue_name(name)
42
+ name.start_with?('/') ? name : "/#{name}"
43
+ end
44
+ end
45
+ end
@@ -1,3 +1,3 @@
1
1
  class Localjob
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.extensions = ["ext/mkrf_conf.rb"]
21
21
 
22
22
  spec.add_dependency "thor", "0.18.1"
23
+ spec.add_dependency "SysVIPC"
23
24
 
24
25
  spec.add_development_dependency "bundler", "~> 1.3"
25
26
  spec.add_development_dependency "rake"
@@ -42,23 +42,6 @@ class WorkerTest < LocaljobTestCase
42
42
  assert_equal 0, @localjob.size
43
43
  end
44
44
 
45
- def test_workers_listen_on_multiple_queues
46
- @localjob << WalrusJob.new("move")
47
-
48
- other = queue("other-queue")
49
- other << WalrusJob.new("dance")
50
-
51
- @worker.channel << 'other-queue'
52
-
53
- a = Thread.start { @worker.work }
54
-
55
- sleep 0.01
56
- a.kill
57
-
58
- assert_equal 0, @localjob.size
59
- assert_equal 0, other.size
60
- end
61
-
62
45
  def test_worker_doesnt_die_on_bad_serialization
63
46
  @localjob.queue.send "--- !ruby/object:Whatever {}\n"
64
47
 
@@ -67,8 +50,10 @@ class WorkerTest < LocaljobTestCase
67
50
  sleep 0.01
68
51
  a.kill
69
52
  end
70
-
53
+
71
54
  on_platform 'linux' do
55
+ # This won't work on OS X because the SysV IPC gem apparently doesnt give us
56
+ # interrupt syscalls to give us signals.
72
57
  def test_sigquit_terminates_the_worker
73
58
  @localjob << WalrusJob.new("move")
74
59
 
@@ -76,10 +61,30 @@ class WorkerTest < LocaljobTestCase
76
61
 
77
62
  pid = fork { @worker.work }
78
63
 
64
+ # Hack to account for race condition, 0.01s should be plenty
65
+ sleep 0.1
66
+
79
67
  Process.kill("QUIT", pid)
80
68
  Process.wait
81
69
 
82
70
  assert_equal 0, @localjob.size
83
71
  end
72
+
73
+ def test_workers_listen_on_multiple_queues
74
+ @localjob << WalrusJob.new("move")
75
+
76
+ other = queue("other-queue")
77
+ other << WalrusJob.new("dance")
78
+
79
+ @worker.channel << 'other-queue'
80
+
81
+ a = Thread.start { @worker.work }
82
+
83
+ sleep 0.01
84
+ a.kill
85
+
86
+ assert_equal 0, @localjob.size
87
+ assert_equal 0, other.size
88
+ end
84
89
  end
85
90
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: localjob
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simon Eskildsen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-03 00:00:00.000000000 Z
11
+ date: 2013-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.18.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: SysVIPC
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -88,6 +102,7 @@ files:
88
102
  - lib/localjob/cli.rb
89
103
  - lib/localjob/linux_adapter.rb
90
104
  - lib/localjob/mock_adapter.rb
105
+ - lib/localjob/sysv_adapter.rb
91
106
  - lib/localjob/version.rb
92
107
  - lib/localjob/worker.rb
93
108
  - localjob.gemspec
@@ -127,3 +142,4 @@ test_files:
127
142
  - test/mock_adapter.rb
128
143
  - test/test_helper.rb
129
144
  - test/worker_test.rb
145
+ has_rdoc: