worker 0.0.1 → 0.2.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
  SHA256:
3
- metadata.gz: cebd01421ffa1fbbbb865661ea513ef710d2d576ee8ef9df0d9504ede166a6d6
4
- data.tar.gz: 791516472a08ebf178a55bfbb37af92b0214f193d7d8c25d2eb9c200c1ae89cc
3
+ metadata.gz: 5ffb171d04a5691eb61e7156c19bb17cc71a179af7979d60fbea10bc74ae2cd9
4
+ data.tar.gz: 2df8110bd23aa3d97c284c38a3a24fa305b712e3d55dd6e3af8e0e360f91ddc9
5
5
  SHA512:
6
- metadata.gz: 27004d93605adc79ab65fd84336c534d01a5b9b731a1e1be796005f096674ef704ccd6b3a83356647514ec5b4b8e855424c189005465f981832d01d1c61c0823
7
- data.tar.gz: f3dd6252cfd5f0443c1673c58cdb34c9c1e3dec860c9116a76e67ba514261d31c2335c8792f6a73b3502805fce915122ee9439cd8124b666b73f434846cb6a63
6
+ metadata.gz: 00f0dbf42c4495d8878e67f39618b99496e17f09a650f461eb05f3f8535d616d41941f96a071909c341c639aa8f324ce475e220b4c5697f924733bf37dbfaa81
7
+ data.tar.gz: a4e68891bacbb67a866db310980a649a9295ade5872fef95a82df9579ac80fe3ac3be67531794660ab2cb8a37262ffea6ff21de68e35ff22ac2627f373b1ed48
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- worker (0.1.0)
4
+ worker (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,39 +1,28 @@
1
1
  # Worker
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/worker`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ Thread safe inter-process synchronous workers using Ruby Queue
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ adder = Worker.new do |a,b|
6
+ a + b
7
+ end
6
8
 
7
- ## Installation
9
+ multiplier = Worker.new do |a,b|
10
+ a * b
11
+ end
8
12
 
9
- Add this line to your application's Gemfile:
13
+ puts adder.perform 1, 2
14
+ # => 3
15
+ puts multiplier.perform 10, 2
16
+ # => 20
10
17
 
11
- ```ruby
12
- gem 'worker'
13
- ```
18
+ Scoped instance variables:
14
19
 
15
- And then execute:
20
+ adder_memo = Worker.new do |a,b|
21
+ @sum ||= 0
22
+ @sum += a + b
23
+ end
16
24
 
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install worker
22
-
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/worker.
36
-
37
- ## License
38
-
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
25
+ puts adder_memo.perform 1, 2
26
+ # => 3
27
+ puts adder_memo.perform 1, 2
28
+ # => 6
@@ -0,0 +1,11 @@
1
+ require_relative "../lib/worker"
2
+
3
+ adder_memo = Worker.new do |a,b|
4
+ @sum ||= 0
5
+ @sum += a + b
6
+ end
7
+
8
+ puts adder_memo.perform 1, 2
9
+ # => 3
10
+ puts adder_memo.perform 1, 2
11
+ # => 6
@@ -0,0 +1,12 @@
1
+ require_relative "../lib/worker"
2
+
3
+ adder = Worker.new do |a,b|
4
+ a + b
5
+ end
6
+
7
+ multiplier = Worker.new do |a,b|
8
+ a * b
9
+ end
10
+
11
+ puts adder.perform 1, 2
12
+ puts multiplier.perform 10, 2
data/lib/worker.rb CHANGED
@@ -1,6 +1,40 @@
1
- require "worker/version"
1
+ class Worker
2
+ class Ctx
3
+ end
2
4
 
3
- module Worker
4
- class Error < StandardError; end
5
- # Your code goes here...
5
+ def initialize(&block)
6
+ @in = Queue.new
7
+ @out = Queue.new
8
+ @block = block
9
+ @ctx = Worker::Ctx.new
10
+
11
+ run!
12
+ end
13
+
14
+ def perform(*args)
15
+ @in.push args
16
+ ret = @out.pop
17
+ if ret.is_a? Exception
18
+ raise ret
19
+ else
20
+ ret
21
+ end
22
+ end
23
+
24
+ def run!
25
+ @thread = Thread.new do
26
+ loop do
27
+ ret = @ctx.instance_exec @in.pop, &@block
28
+ @out.push ret
29
+ rescue Exception => ex
30
+ @out.push ex
31
+ end
32
+ end
33
+ self
34
+ end
35
+
36
+ def stop!
37
+ @thread&.kill
38
+ self
39
+ end
6
40
  end
data/worker.gemspec CHANGED
@@ -1,11 +1,10 @@
1
1
 
2
2
  lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "worker/version"
5
4
 
6
5
  Gem::Specification.new do |spec|
7
6
  spec.name = "worker"
8
- spec.version = Worker::VERSION
7
+ spec.version = "0.2.0"
9
8
  spec.authors = ["Matti Paksula"]
10
9
  spec.email = ["matti.paksula@iki.fi"]
11
10
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matti Paksula
@@ -71,8 +71,9 @@ files:
71
71
  - Rakefile
72
72
  - bin/console
73
73
  - bin/setup
74
+ - examples/adder_memo.rb
75
+ - examples/simple.rb
74
76
  - lib/worker.rb
75
- - lib/worker/version.rb
76
77
  - worker.gemspec
77
78
  homepage: https://github.com/matti/worker
78
79
  licenses:
@@ -1,3 +0,0 @@
1
- module Worker
2
- VERSION = "0.0.1"
3
- end