eldritch 1.1.0 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -27
- data/lib/eldritch/dsl.rb +4 -3
- data/lib/eldritch/group.rb +2 -2
- data/lib/eldritch/task.rb +14 -7
- data/lib/eldritch/version.rb +1 -1
- data/spec/dsl_spec.rb +2 -20
- data/spec/group_spec.rb +4 -4
- data/spec/task_spec.rb +24 -11
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b580cd9181ade4f4a2a6976341a88b3059ad5670
|
4
|
+
data.tar.gz: a9947d828dfd477bccbed94f5713c1620de2063e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f336489e6ef818dc9dc92a02b90d94c9b2aa9fb389286f0b80ecaa75ab800c183754bfa43eb2237e2e4c30c229d04e8af354f5dd1a228f61a74a1498847ded4a
|
7
|
+
data.tar.gz: 370a2d8c641f8fda77846fec17ce8e6c7dc0dcc0825952c900e0bbb625dd71e999b64af70ae86453399d8d5936832c96de699cedfe7da992e8f59b61bdc3bb05
|
data/README.md
CHANGED
@@ -12,6 +12,27 @@ Code quality
|
|
12
12
|
[![Coverage Status](http://coveralls.io/repos/beraboris/eldritch/badge.png)](http://coveralls.io/r/beraboris/eldritch)
|
13
13
|
[![Code Climate](http://codeclimate.com/github/beraboris/eldritch.png)](http://codeclimate.com/github/beraboris/eldritch)
|
14
14
|
|
15
|
+
Usage
|
16
|
+
-----
|
17
|
+
|
18
|
+
1. Install it `gem install eldritch`
|
19
|
+
2. Require it `require 'eldritch'`
|
20
|
+
3. Use it (see features below)
|
21
|
+
|
22
|
+
By default eldritch will inject the DSL into the global scope. If you don't want this, you can require `eldritch/safe`
|
23
|
+
instead of `eldritch`.
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'eldricth/safe'
|
27
|
+
|
28
|
+
class MyClass
|
29
|
+
include Eldritch::DSL
|
30
|
+
extend Eldritch::DSL
|
31
|
+
|
32
|
+
# The DSL is available in this class
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
15
36
|
Features
|
16
37
|
--------
|
17
38
|
|
@@ -28,12 +49,10 @@ async def send_email(email)
|
|
28
49
|
# ...
|
29
50
|
end
|
30
51
|
|
31
|
-
# ...
|
32
52
|
send_email(some_email) # runs in the background
|
33
|
-
# ...
|
34
53
|
```
|
35
54
|
|
36
|
-
#### ruby 1.9.3 and
|
55
|
+
#### ruby 1.9.3 and 2.0.0
|
37
56
|
|
38
57
|
For all versions of ruby before 2.1.0, you need to define async methods like so:
|
39
58
|
|
@@ -54,17 +73,14 @@ Async blocks are run concurrently.
|
|
54
73
|
```ruby
|
55
74
|
require 'eldritch'
|
56
75
|
|
57
|
-
# do some work
|
58
76
|
async do
|
59
|
-
#
|
77
|
+
# runs in the background
|
60
78
|
end
|
61
|
-
# continue working
|
62
79
|
```
|
63
80
|
|
64
81
|
### tasks
|
65
82
|
|
66
|
-
Async blocks and async methods both return tasks. These can be used to interact with the async block/method.
|
67
|
-
you can wait for the task to finish or you can get its return value.
|
83
|
+
Async blocks and async methods both return tasks. These can be used to interact with the async block/method.
|
68
84
|
|
69
85
|
```ruby
|
70
86
|
require 'eldritch'
|
@@ -73,9 +89,7 @@ task = async do
|
|
73
89
|
# calculate something that will take a long time
|
74
90
|
end
|
75
91
|
|
76
|
-
#
|
77
|
-
|
78
|
-
# now we need to result of the task
|
92
|
+
# we need to result of the task
|
79
93
|
res = 2 + task.value # waits for the task to finish
|
80
94
|
```
|
81
95
|
|
@@ -135,20 +149,5 @@ this repository you need to add `lib/` to the include path.
|
|
135
149
|
$ ruby -Ilib examples/the_example.rb
|
136
150
|
|
137
151
|
Be aware that if you are running ruby < 2.1.0, some the examples may not work. All the examples that define async
|
138
|
-
methods with `async def something; end` will not work. This is because since ruby 2.1.0 def returns the name of the
|
152
|
+
methods with `async def something; end` will not work. This is because, since ruby 2.1.0, def returns the name of the
|
139
153
|
method defined as a symbol.
|
140
|
-
|
141
|
-
Installation
|
142
|
-
------------
|
143
|
-
|
144
|
-
Add this line to your application's Gemfile:
|
145
|
-
|
146
|
-
gem 'eldritch'
|
147
|
-
|
148
|
-
And then execute:
|
149
|
-
|
150
|
-
$ bundle
|
151
|
-
|
152
|
-
Or install it yourself as:
|
153
|
-
|
154
|
-
$ gem install eldritch
|
data/lib/eldritch/dsl.rb
CHANGED
@@ -87,17 +87,18 @@ module Eldritch
|
|
87
87
|
|
88
88
|
yield group
|
89
89
|
|
90
|
-
group.
|
90
|
+
group.join_all
|
91
91
|
Thread.current.eldritch_group = old
|
92
92
|
end
|
93
93
|
|
94
94
|
private
|
95
95
|
|
96
96
|
def async_block(&block)
|
97
|
-
task = Task.new do
|
97
|
+
task = Task.new do
|
98
98
|
begin
|
99
|
-
|
99
|
+
block.call
|
100
100
|
rescue InterruptedError
|
101
|
+
# exit silently
|
101
102
|
end
|
102
103
|
end
|
103
104
|
Thread.current.eldritch_group << task
|
data/lib/eldritch/group.rb
CHANGED
data/lib/eldritch/task.rb
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
module Eldritch
|
2
2
|
# Runs a block in parallel and allows for interaction with said block
|
3
3
|
class Task
|
4
|
-
attr_writer :value
|
5
|
-
|
6
4
|
# @return [Thread] underlying ruby thread
|
7
5
|
attr_reader :thread
|
8
6
|
|
@@ -24,14 +22,14 @@ module Eldritch
|
|
24
22
|
# end
|
25
23
|
# task.start # calls the block in parallel
|
26
24
|
def start
|
27
|
-
@thread = Thread.new
|
25
|
+
@thread = Thread.new &@block
|
28
26
|
@thread.eldritch_task = self
|
29
27
|
end
|
30
28
|
|
31
29
|
# Waits for the task to complete
|
32
|
-
def
|
30
|
+
def join
|
33
31
|
@thread.join
|
34
|
-
|
32
|
+
unset_thread_task
|
35
33
|
end
|
36
34
|
|
37
35
|
# The return value of the task
|
@@ -40,8 +38,9 @@ module Eldritch
|
|
40
38
|
#
|
41
39
|
# @return whatever the block returns
|
42
40
|
def value
|
43
|
-
|
44
|
-
|
41
|
+
val = @thread.value
|
42
|
+
unset_thread_task
|
43
|
+
val
|
45
44
|
end
|
46
45
|
|
47
46
|
# Forces the task to end
|
@@ -49,6 +48,7 @@ module Eldritch
|
|
49
48
|
# This kills the underlying thread. This is a dangerous call.
|
50
49
|
def abort
|
51
50
|
@thread.kill
|
51
|
+
unset_thread_task
|
52
52
|
end
|
53
53
|
|
54
54
|
# Interrupts the task
|
@@ -59,6 +59,13 @@ module Eldritch
|
|
59
59
|
# You can still handle the exception yourself.
|
60
60
|
def interrupt
|
61
61
|
@thread.raise InterruptedError.new
|
62
|
+
unset_thread_task
|
63
|
+
end
|
64
|
+
|
65
|
+
private
|
66
|
+
|
67
|
+
def unset_thread_task
|
68
|
+
@thread.eldritch_task = nil
|
62
69
|
end
|
63
70
|
end
|
64
71
|
end
|
data/lib/eldritch/version.rb
CHANGED
data/spec/dsl_spec.rb
CHANGED
@@ -35,11 +35,11 @@ describe Eldritch::DSL do
|
|
35
35
|
klass.together {}
|
36
36
|
end
|
37
37
|
|
38
|
-
it 'should
|
38
|
+
it 'should join on all tasks' do
|
39
39
|
group = double('group').as_null_object
|
40
40
|
allow(Eldritch::Group).to receive(:new).and_return(group)
|
41
41
|
|
42
|
-
expect(group).to receive(:
|
42
|
+
expect(group).to receive(:join_all)
|
43
43
|
|
44
44
|
klass.together {}
|
45
45
|
end
|
@@ -92,12 +92,6 @@ describe Eldritch::DSL do
|
|
92
92
|
expect(klass.async {}).to eql(task)
|
93
93
|
end
|
94
94
|
|
95
|
-
it 'should set the task value' do
|
96
|
-
expect(task).to receive(:value=).with('something')
|
97
|
-
|
98
|
-
klass.async { 'something' }
|
99
|
-
end
|
100
|
-
|
101
95
|
it 'should eat any interrupted errors' do
|
102
96
|
block = proc { raise Eldritch::InterruptedError }
|
103
97
|
|
@@ -144,18 +138,6 @@ describe Eldritch::DSL do
|
|
144
138
|
|
145
139
|
instance.foo(1,2,3)
|
146
140
|
end
|
147
|
-
|
148
|
-
it 'should set the task value' do
|
149
|
-
expect(task).to receive(:value=).with(42)
|
150
|
-
|
151
|
-
klass.class_eval do
|
152
|
-
def foo; 42; end
|
153
|
-
async :foo
|
154
|
-
end
|
155
|
-
instance = klass.new
|
156
|
-
|
157
|
-
instance.foo
|
158
|
-
end
|
159
141
|
end
|
160
142
|
end
|
161
143
|
end
|
data/spec/group_spec.rb
CHANGED
@@ -66,15 +66,15 @@ describe Eldritch::Group do
|
|
66
66
|
end
|
67
67
|
end
|
68
68
|
|
69
|
-
describe '#
|
70
|
-
it 'should call
|
69
|
+
describe '#join_all' do
|
70
|
+
it 'should call join on all tasks' do
|
71
71
|
task = double('task')
|
72
72
|
allow(task).to receive(:start)
|
73
73
|
group << task
|
74
74
|
|
75
|
-
expect(task).to receive(:
|
75
|
+
expect(task).to receive(:join)
|
76
76
|
|
77
|
-
group.
|
77
|
+
group.join_all
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
data/spec/task_spec.rb
CHANGED
@@ -27,7 +27,7 @@ describe Eldritch::Task do
|
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'should start a thread' do
|
30
|
-
expect(Thread).to receive(:new)
|
30
|
+
expect(Thread).to receive(:new)
|
31
31
|
|
32
32
|
task.start
|
33
33
|
end
|
@@ -39,45 +39,51 @@ describe Eldritch::Task do
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
describe '#
|
42
|
+
describe '#join' do
|
43
43
|
it 'should join the thread' do
|
44
44
|
task.start
|
45
45
|
|
46
46
|
expect(task.thread).to receive(:join)
|
47
|
-
task.
|
47
|
+
task.join
|
48
48
|
end
|
49
49
|
|
50
50
|
it 'should set the thread task to nil' do
|
51
51
|
task.start
|
52
52
|
|
53
53
|
expect(thread).to receive(:eldritch_task=).with(nil)
|
54
|
-
task.
|
54
|
+
task.join
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
58
|
describe '#value' do
|
59
|
-
it 'should
|
59
|
+
it 'should set the thread task to nil' do
|
60
60
|
task.start
|
61
61
|
|
62
|
-
expect(
|
62
|
+
expect(thread).to receive(:eldritch_task=).with(nil)
|
63
63
|
task.value
|
64
64
|
end
|
65
65
|
|
66
|
-
it 'should
|
66
|
+
it 'should call Thread#value' do
|
67
67
|
task.start
|
68
|
-
|
69
|
-
expect(thread).to receive(:eldritch_task=).with(nil)
|
68
|
+
expect(thread).to receive(:value)
|
70
69
|
task.value
|
71
70
|
end
|
72
71
|
|
73
|
-
it 'should return
|
74
|
-
task.value = 42
|
72
|
+
it 'should return what Thread#value returns' do
|
75
73
|
task.start
|
74
|
+
allow(thread).to receive(:value).and_return(42)
|
76
75
|
expect(task.value).to eql(42)
|
77
76
|
end
|
78
77
|
end
|
79
78
|
|
80
79
|
describe '#abort' do
|
80
|
+
it 'should set the thread task to nil' do
|
81
|
+
task.start
|
82
|
+
expect(thread).to receive(:eldritch_task=).with(nil)
|
83
|
+
|
84
|
+
task.abort
|
85
|
+
end
|
86
|
+
|
81
87
|
it 'should kill the thread' do
|
82
88
|
expect(thread).to receive(:kill)
|
83
89
|
|
@@ -87,6 +93,13 @@ describe Eldritch::Task do
|
|
87
93
|
end
|
88
94
|
|
89
95
|
describe '#interrupt' do
|
96
|
+
it 'should set the thread task to nil' do
|
97
|
+
task.start
|
98
|
+
expect(thread).to receive(:eldritch_task=).with(nil)
|
99
|
+
|
100
|
+
task.interrupt
|
101
|
+
end
|
102
|
+
|
90
103
|
it 'should raise an interrupted error on the thread' do
|
91
104
|
expect(thread).to receive(:raise).with(kind_of(Eldritch::InterruptedError))
|
92
105
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: eldritch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Boris Bera
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-04-
|
12
|
+
date: 2014-04-28 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|