nestene 0.2.0 → 0.2.1

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: e479ea793383b23ce3bf9de77bf12ebd31feab21
4
- data.tar.gz: 6a8ba3f519fd58266ebebf33f134a1a5399ebb42
3
+ metadata.gz: 3dc00c22cd4e58cd1a82ac111d8531cd31ffa244
4
+ data.tar.gz: 35aa72d30e3c2d36b3b48245bffee533cd9e12b7
5
5
  SHA512:
6
- metadata.gz: 8fd66164e61c692eabc430c7d7e3cf1cb7a0a89e9a3ec2ff5f9dcac2de720de88a349f3ea14c09f77dc52e25b9e534efabc7b938acd539731e79c3730ae26be6
7
- data.tar.gz: 8573732417285586aefc07059efb2a9ceb6d7b186abf9bf324fea0a093c276d64b48db75ecc49b63dfd426cd104fe58a4c9e2956f7e9183b6d963490795e6d4f
6
+ metadata.gz: 8203a4de036bd60499bd616201969e37678a74a0cdf972884bd1db4a7a17dee6708be152ba168310c469477c86cdb4789f8738fda76063ebfcb4a808f36df97d
7
+ data.tar.gz: dac861c5b236c5c8a788e54d528b643e8e3bf58c3e36a634c49dbbf3fac773ef43b460d0624f02184333e8baf321453e1ba0a3541ec515c2f25ef49b56b54c2e
data/TODO.txt CHANGED
@@ -15,3 +15,5 @@ done:
15
15
  - Limit size of auton execution history
16
16
  - Pretty Print JSON for the nestene's state
17
17
  - Add module for autons -> automatic registry
18
+ - Add method to self-terminate auton to the context
19
+
@@ -4,6 +4,7 @@ require 'nestene'
4
4
  require 'capybara/cucumber'
5
5
  require 'capybara/poltergeist'
6
6
  require 'pry'
7
+ require 'timeout'
7
8
  Capybara.javascript_driver = :poltergeist
8
9
 
9
10
  Capybara.app = Nestene::Ui::App
@@ -12,24 +12,38 @@ module Nestene
12
12
 
13
13
 
14
14
  def execute_next_step topic, auton_id, state
15
+
16
+ return unless state
17
+
15
18
  executing = nil
16
19
  type = nil
17
20
  ser = nil
18
21
  running = false
19
22
 
20
- Celluloid::Actor["storage:%s" % auton_id].update do |state|
21
- running = state.state == :ready && !state.queue.to_execute.empty?
22
- if running
23
- nx = state.queue.to_execute.shift
24
- executing = ExecutingMethod.new(nx)
25
- state.queue.currently_executing = executing
26
- type = state.type
27
- ser = state.serialized
23
+ storage_actor = Celluloid::Actor["storage:%s" % auton_id]
24
+
25
+ return unless storage_actor
26
+
27
+ storage_actor.update do |state|
28
+ if state
29
+ running = state.state == :ready && !state.queue.to_execute.empty?
30
+ if running
31
+ nx = state.queue.to_execute.shift
32
+ executing = ExecutingMethod.new(nx)
33
+ state.queue.currently_executing = executing
34
+ type = state.type
35
+ ser = state.serialized
36
+ end
28
37
  end
29
38
  end
30
39
 
31
40
  return unless running
32
41
 
42
+ if executing.name == '__terminate_this_auton'
43
+ Celluloid::Actor["storage:%s" % auton_id].async.shutdown
44
+ return
45
+ end
46
+
33
47
  instance = Nestene::class_from_string(type).from_structure(ser)
34
48
 
35
49
  if instance.public_methods.include?(:context=)
@@ -65,20 +79,22 @@ module Nestene
65
79
  end
66
80
 
67
81
  Celluloid::Actor["storage:%s" % auton_id].update do |state|
68
- state.queue.currently_executing = nil
69
- state.serialized = instance.to_structure
70
- if exception
71
- if instance.methods.include?(:handle_exception) && executing.name != :handle_exception
72
- method = ScheduledMethod.new
73
- method.name = :handle_exception
74
- method.parameters = [exception.class.name, exception.message, executing.name, executing.parameters]
75
- method.uuid = SecureRandom.uuid
76
- state.queue.to_execute.unshift method
77
- else
78
- state.queue.failed = true
82
+ if state
83
+ state.queue.currently_executing = nil
84
+ state.serialized = instance.to_structure
85
+ if exception
86
+ if instance.methods.include?(:handle_exception) && executing.name != :handle_exception
87
+ method = ScheduledMethod.new
88
+ method.name = :handle_exception
89
+ method.parameters = [exception.class.name, exception.message, executing.name, executing.parameters]
90
+ method.uuid = SecureRandom.uuid
91
+ state.queue.to_execute.unshift method
92
+ else
93
+ state.queue.failed = true
94
+ end
79
95
  end
96
+ state.queue.add_executed executed
80
97
  end
81
- state.queue.add_executed executed
82
98
  end
83
99
  end
84
100
 
@@ -36,6 +36,13 @@ module Nestene
36
36
  structure ? AutonState.from_structure(structure) : nil
37
37
  end
38
38
 
39
+ def shutdown
40
+ Celluloid::Actor.delete("storage:%s" % @auton_id)
41
+ @storage.delete(@auton_id)
42
+ publish('state_update', @auton_id, nil)
43
+ terminate
44
+ end
45
+
39
46
  def update &block
40
47
  before = @storage.load(@auton_id)
41
48
  state = AutonState.from_structure(@storage.load(@auton_id))
@@ -138,8 +138,10 @@ module Nestene
138
138
  end
139
139
 
140
140
  def notify_waiters topic, auton_id, state
141
- future = @execution_futures.delete(auton_id)
142
- future.signal(SelfValue.new) if future
141
+ if state
142
+ future = @execution_futures.delete(auton_id)
143
+ future.signal(SelfValue.new) if future
144
+ end
143
145
  end
144
146
 
145
147
  end
@@ -22,13 +22,16 @@ module Nestene
22
22
 
23
23
  @schedule.delete_if{|e| e.first == auton_id}
24
24
 
25
- state.queue.delayed.each do |d|
26
- @schedule << [auton_id, d]
27
- end
25
+ if state
26
+
27
+ state.queue.delayed.each do |d|
28
+ @schedule << [auton_id, d]
29
+ end
28
30
 
29
- @schedule.sort_by!{|e| e[1].execute_at}
31
+ @schedule.sort_by!{|e| e[1].execute_at}
30
32
 
31
- async.schedule_methods
33
+ async.schedule_methods
34
+ end
32
35
 
33
36
  end
34
37
 
@@ -36,9 +36,11 @@ module Nestene
36
36
  Celluloid::Actor[:nestene_core].create_auton type, auton_id
37
37
  end
38
38
 
39
- attr_reader :auton_id
40
-
39
+ def terminate
40
+ schedule_step '__terminate_this_auton'
41
+ end
41
42
 
43
+ attr_reader :auton_id
42
44
 
43
45
  end
44
46
  end
@@ -32,12 +32,16 @@ module Nestene
32
32
  end
33
33
 
34
34
  def store(key, value)
35
- final_name = "%s/%s" % [@dir,@converter.key_to_file_name(key)]
36
- tmp_name = "%s.temp" % final_name
37
- File.open(tmp_name,"w") do |f|
38
- f.write(JSON.pretty_generate(value))
35
+ if value != nil
36
+ final_name = "%s/%s" % [@dir,@converter.key_to_file_name(key)]
37
+ tmp_name = "%s.temp" % final_name
38
+ File.open(tmp_name,"w") do |f|
39
+ f.write(JSON.pretty_generate(value))
40
+ end
41
+ File.rename(tmp_name, final_name)
42
+ else
43
+ delete(key)
39
44
  end
40
- File.rename(tmp_name, final_name)
41
45
  end
42
46
 
43
47
  def load(key)
@@ -49,6 +53,11 @@ module Nestene
49
53
  end
50
54
  end
51
55
 
56
+ def delete(key)
57
+ file_name = "%s/%s" % [@dir,@converter.key_to_file_name(key)]
58
+ File.unlink(file_name)
59
+ end
60
+
52
61
  end
53
62
 
54
63
  class MemoryStorage
@@ -66,7 +75,15 @@ module Nestene
66
75
  end
67
76
 
68
77
  def store(key, value)
69
- @storage[key] = value
78
+ if value != nil
79
+ @storage[key] = value
80
+ else
81
+ delete(key)
82
+ end
83
+ end
84
+
85
+ def delete(key)
86
+ @storage.delete(key)
70
87
  end
71
88
  end
72
89
 
@@ -1,3 +1,3 @@
1
1
  module Nestene
2
- VERSION = "0.2.0"
2
+ VERSION = "0.2.1"
3
3
  end
@@ -23,9 +23,9 @@ module Nestene
23
23
 
24
24
  after{FileUtils.remove_entry_secure tmpdir}
25
25
 
26
- describe :list do
26
+ subject {DiskStorage.new(tmpdir)}
27
27
 
28
- subject {DiskStorage.new(tmpdir)}
28
+ describe :list do
29
29
 
30
30
  context "when there is nothing in the storage" do
31
31
  it "should return empty list" do
@@ -44,8 +44,6 @@ module Nestene
44
44
 
45
45
  describe :load do
46
46
 
47
- subject {DiskStorage.new(tmpdir)}
48
-
49
47
  context "when the object does not exist" do
50
48
  it "should return nil" do
51
49
  expect(subject.load('whatever')).to be_nil
@@ -62,20 +60,37 @@ module Nestene
62
60
  end
63
61
 
64
62
  describe :store do
65
- subject {DiskStorage.new(tmpdir)}
66
-
67
- it "stores structure" do
63
+ it "should store structure" do
68
64
  subject.store('test',{})
69
65
  expect(subject.load('test')).to eq({})
70
66
  end
67
+
68
+ context "when structure is nil" do
69
+ it 'should delete the entry' do
70
+ subject.store('test',{})
71
+ subject.store('test',nil)
72
+ expect(subject.load('test')).to eq(nil)
73
+ expect(subject.list).to_not include('test')
74
+ end
75
+ end
76
+ end
77
+
78
+ describe :delete do
79
+ it "deletes existing strucuture" do
80
+ subject.store('test',{})
81
+ subject.delete('test')
82
+ expect(subject.load('test')).to eq(nil)
83
+ expect(subject.list).to_not include('test')
84
+ end
71
85
  end
72
86
 
73
87
  end
74
88
 
75
89
  describe MemoryStorage do
76
- describe :list do
77
90
 
78
- subject {MemoryStorage.new}
91
+ subject {MemoryStorage.new}
92
+
93
+ describe :list do
79
94
 
80
95
  context "when there is nothing in the storage" do
81
96
  it "should return empty list" do
@@ -85,7 +100,6 @@ module Nestene
85
100
 
86
101
  context "when there is at least one object in the storage" do
87
102
  before {subject.store("test",{})}
88
-
89
103
  it "should return list of existing objects" do
90
104
  expect(subject.list).to eq(['test'])
91
105
  end
@@ -93,9 +107,6 @@ module Nestene
93
107
  end
94
108
 
95
109
  describe :load do
96
-
97
- subject {MemoryStorage.new}
98
-
99
110
  context "when the object does not exist" do
100
111
  it "should return nil" do
101
112
  expect(subject.load('whatever')).to be_nil
@@ -108,17 +119,33 @@ module Nestene
108
119
  expect(subject.load('test')).to eq({})
109
120
  end
110
121
  end
111
-
112
122
  end
113
123
 
114
124
 
115
125
  describe :store do
116
- subject {MemoryStorage.new}
117
-
118
- it "stores structure" do
126
+ it "should store structure" do
119
127
  subject.store('test',{})
120
128
  expect(subject.load('test')).to eq({})
121
129
  end
130
+
131
+ context "when structure is nil" do
132
+ it 'should delete the entry' do
133
+ subject.store('test',{})
134
+ subject.store('test',nil)
135
+ expect(subject.load('test')).to eq(nil)
136
+ expect(subject.list).to_not include('test')
137
+ end
138
+ end
139
+
140
+ end
141
+
142
+ describe :delete do
143
+ it "deletes existing strucuture" do
144
+ subject.store('test',{})
145
+ subject.delete('test')
146
+ expect(subject.load('test')).to eq(nil)
147
+ expect(subject.list).to_not include('test')
148
+ end
122
149
  end
123
150
 
124
151
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nestene
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dragan Milic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-23 00:00:00.000000000 Z
11
+ date: 2014-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler