lifecycle_vm 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.rspec +3 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/Gemfile +10 -0
- data/LICENSE +202 -0
- data/README.md +35 -0
- data/Rakefile +8 -0
- data/lib/lifecycle_vm/cond_base.rb +67 -0
- data/lib/lifecycle_vm/memory.rb +27 -0
- data/lib/lifecycle_vm/op_base.rb +134 -0
- data/lib/lifecycle_vm/state.rb +36 -0
- data/lib/lifecycle_vm/then.rb +125 -0
- data/lib/lifecycle_vm/version.rb +19 -0
- data/lib/lifecycle_vm/vm.rb +243 -0
- data/lib/lifecycle_vm.rb +24 -0
- data/lifecycle_vm.gemspec +29 -0
- metadata +90 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 12e44a51967bc4275e68b74de045c3b50604c939ade596365a102e9b34280c4a
|
|
4
|
+
data.tar.gz: d2efdbc98e5ce8e02cfad19e9c4ff479691a4c10c5e5db7261ca089c7cb4efb8
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: ce5700cf19e330bf3c1f1006d519037776f0f50bba74295943ccf71100fd77f7f0d96fbd174b4ecd09c8fc2864e52f647602e56aad23c499ab6da089e903e123
|
|
7
|
+
data.tar.gz: 430a04f58436b5702cf9fe4cf994bec3dec84eaf1030d65b29f74a7dae4b43f2a1489089b147382c06f3808c57d20390b14ec4801ed6cca4faf96c2c3fa573cf
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
lifecycle_vm
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby-2.7.4
|
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
data/README.md
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# LifecycleVm
|
|
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/lifecycle_vm`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'lifecycle_vm'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle install
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install lifecycle_vm
|
|
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 the created tag, 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]/lifecycle_vm.
|
data/Rakefile
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2021 Teak.io, Inc.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
module LifecycleVM
|
|
18
|
+
# Base class for all conditionals in a vm.
|
|
19
|
+
# A conditional may read from vm emory by declare reads using the DSL. The
|
|
20
|
+
# lifecycle of a conditional is initialize -> call, and both methods will
|
|
21
|
+
# always be called. All reads and logger will be provided before initialize
|
|
22
|
+
# is called.
|
|
23
|
+
#
|
|
24
|
+
# A conditional may not write to vm memory, and must not error.
|
|
25
|
+
class CondBase
|
|
26
|
+
# Raised if a conditional attempts to read from a memory slot that does not exist.
|
|
27
|
+
class InvalidAttr < ::LifecycleVM::Error
|
|
28
|
+
attr_reader :cond_class, :attribute
|
|
29
|
+
|
|
30
|
+
def initialize(cond_class, attribute)
|
|
31
|
+
@cond_class = cond_class
|
|
32
|
+
@attribute = attribute
|
|
33
|
+
super("Invalid attribute #{attribute} access by #{cond_class.name}")
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Read one or more values out of vm memory and provide them as readable attributes
|
|
38
|
+
def self.reads(*attrs)
|
|
39
|
+
@reads ||= {}
|
|
40
|
+
@reads.merge!(Hash[attrs.map { |attribute| [attribute, :"@#{attribute}"] }])
|
|
41
|
+
attr_reader(*attrs)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Execute the current op with the gien vm memory.
|
|
45
|
+
# This will read out all declared reads from memory and return the result of #call.
|
|
46
|
+
def self.call(memory)
|
|
47
|
+
obj = allocate
|
|
48
|
+
|
|
49
|
+
@reads ||= {}
|
|
50
|
+
|
|
51
|
+
@reads.each do |(attribute, ivar)|
|
|
52
|
+
raise InvalidAttr.new(self, attribute) unless memory.respond_to?(attribute)
|
|
53
|
+
|
|
54
|
+
obj.instance_variable_set(ivar, memory.send(attribute).clone)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
obj.instance_variable_set(:"@logger", memory.logger)
|
|
58
|
+
|
|
59
|
+
obj.send(:initialize)
|
|
60
|
+
obj.send(:call)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
attr_reader :logger
|
|
64
|
+
|
|
65
|
+
def call; end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2021 Teak.io, Inc.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
module LifecycleVM
|
|
18
|
+
# Base class for VM memory.
|
|
19
|
+
# Any slots that you want to make readable must have a getter method defined.
|
|
20
|
+
# Any slots that you want to make writeable must have a setter method defined.
|
|
21
|
+
class Memory
|
|
22
|
+
PROTECTED = %i[current_state last_state current_op error_op].freeze
|
|
23
|
+
BUILTINS = (PROTECTED + %i[logger]).freeze
|
|
24
|
+
|
|
25
|
+
attr_accessor(*BUILTINS)
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2021 Teak.io, Inc.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
require 'lifecycle_vm/memory'
|
|
18
|
+
|
|
19
|
+
module LifecycleVM
|
|
20
|
+
# Base class for all ops in a vm.
|
|
21
|
+
# An op may read and write to vm memory by declaring reads and writes using the
|
|
22
|
+
# DSL. The lifecycle of an op is initialize -> call -> validate, and all three
|
|
23
|
+
# methods will always be called. All reads and logger will be provided before
|
|
24
|
+
# initialize is called.
|
|
25
|
+
#
|
|
26
|
+
# An op may indicate that it encountered an error or otherwise failed by calling
|
|
27
|
+
# #error. If an op has any errors, no writes will be persisted to the vm memory.
|
|
28
|
+
class OpBase
|
|
29
|
+
# Raised if an op attempts to read or write to a memory slot that does not
|
|
30
|
+
# exist, or if an op attempts to write to any slot in LifecycleVM::Memory::PROTECTED.
|
|
31
|
+
class InvalidAttr < ::LifecycleVM::Error
|
|
32
|
+
attr_reader :op_class, :attribute
|
|
33
|
+
|
|
34
|
+
def initialize(op_class, attribute)
|
|
35
|
+
@op_class = op_class
|
|
36
|
+
@attribute = attribute
|
|
37
|
+
super("Invalid attribute #{attribute} access by #{op_class.name}")
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Read one or more values out of vm memory and provide them as readable attributes
|
|
42
|
+
def self.reads(*attrs)
|
|
43
|
+
@reads ||= {}
|
|
44
|
+
@writes ||= {}
|
|
45
|
+
|
|
46
|
+
attrs.each do |attribute|
|
|
47
|
+
attribute = attribute.to_sym
|
|
48
|
+
|
|
49
|
+
attr_reader attribute unless @writes.key?(attribute)
|
|
50
|
+
|
|
51
|
+
@reads[attribute] = :"@#{attribute}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
@reads
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Allow writing one or more values to vm memory and provide them as writeable attributes
|
|
58
|
+
# @note You may not write to any slot in LifecycleVM::Memory::PROTECTED
|
|
59
|
+
def self.writes(*attrs)
|
|
60
|
+
@reads ||= {}
|
|
61
|
+
@writes ||= {}
|
|
62
|
+
|
|
63
|
+
attrs.each do |attribute|
|
|
64
|
+
attribute = attribute.to_sym
|
|
65
|
+
raise InvalidAttr.new(self, attribute) if LifecycleVM::Memory::PROTECTED.include?(attribute)
|
|
66
|
+
|
|
67
|
+
if @reads.key?(attribute)
|
|
68
|
+
attr_writer attribute
|
|
69
|
+
else
|
|
70
|
+
attr_accessor attribute
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
@writes[attribute] = :"#{attribute}="
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
@writes
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Execute the current op with the gien vm memory.
|
|
80
|
+
# This will read out all declared reads from memory and if executed without
|
|
81
|
+
# any errors will write all declared writes to memory.
|
|
82
|
+
def self.call(memory)
|
|
83
|
+
obj = allocate
|
|
84
|
+
|
|
85
|
+
@reads ||= {}
|
|
86
|
+
@writes ||= {}
|
|
87
|
+
|
|
88
|
+
@reads.each do |(attribute, ivar)|
|
|
89
|
+
raise InvalidAttr.new(self, attribute) unless memory.respond_to?(attribute)
|
|
90
|
+
|
|
91
|
+
obj.instance_variable_set(ivar, memory.send(attribute).clone)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
@writes.each do |(attribute, setter)|
|
|
95
|
+
raise InvalidAttr.new(self, attribute) unless memory.respond_to?(setter)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
obj.instance_variable_set(:"@logger", memory.logger)
|
|
99
|
+
obj.instance_variable_set(:"@errors", nil)
|
|
100
|
+
|
|
101
|
+
obj.send(:initialize)
|
|
102
|
+
obj.send(:call)
|
|
103
|
+
obj.send(:validate)
|
|
104
|
+
|
|
105
|
+
unless obj.errors?
|
|
106
|
+
@writes.each do |(attribute, setter)|
|
|
107
|
+
memory.send(setter, obj.send(attribute))
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
obj
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def call; end
|
|
115
|
+
def validate; end
|
|
116
|
+
|
|
117
|
+
attr_reader :errors, :logger
|
|
118
|
+
|
|
119
|
+
# Declare that an error has occurred.
|
|
120
|
+
# @param field [Symbol] the memory field which is in error. The convention is to use :base
|
|
121
|
+
# for errors not associated with any particular memory field.
|
|
122
|
+
# @param message [Object] a description of the error. Convention is to use a string.
|
|
123
|
+
def error(field, message)
|
|
124
|
+
@errors ||= {}
|
|
125
|
+
@errors[field] ||= []
|
|
126
|
+
@errors[field] << message
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
# Did any errors occur in the execution of this op?
|
|
130
|
+
def errors?
|
|
131
|
+
!(@errors.nil? || @errors.empty?)
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2021 Teak.io, Inc.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
require 'lifecycle_vm/then'
|
|
18
|
+
|
|
19
|
+
module LifecycleVM
|
|
20
|
+
# Represents a state in the lifecycle.
|
|
21
|
+
# A state may optionally define a single operation to perform, and then must
|
|
22
|
+
# define which state to transition to next.
|
|
23
|
+
# Transitions may be conditional, controlled by a subclass of LifecycleVM::CondBase
|
|
24
|
+
# @see LifecycleVM::VM
|
|
25
|
+
# @see LifecycleVM::OpBase
|
|
26
|
+
# @see LifecycleVM::CondBase
|
|
27
|
+
class State
|
|
28
|
+
attr_reader :name, :op, :then
|
|
29
|
+
|
|
30
|
+
def initialize(name, options)
|
|
31
|
+
@name = name
|
|
32
|
+
@op = options[:do]
|
|
33
|
+
@then = Then.new(options[:then]) if options.key?(:then)
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2021 Teak.io, Inc.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
require 'lifecycle_vm/vm'
|
|
18
|
+
|
|
19
|
+
module LifecycleVM
|
|
20
|
+
class Then
|
|
21
|
+
class Simple
|
|
22
|
+
def initialize(state)
|
|
23
|
+
@state = state
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def call(_vm)
|
|
27
|
+
@state
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
class AnonymousState
|
|
32
|
+
attr_reader :op, :then
|
|
33
|
+
|
|
34
|
+
def initialize(options)
|
|
35
|
+
@op = options[:do]
|
|
36
|
+
@then = Then.new(options[:then])
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def call(vm)
|
|
40
|
+
vm.do_anonymous_op(@op, @then)
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def self.new(options)
|
|
45
|
+
if options.kind_of?(Symbol)
|
|
46
|
+
return Simple.new(options)
|
|
47
|
+
else
|
|
48
|
+
super
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def initialize(options)
|
|
53
|
+
if !(options.kind_of?(Hash) && options.key?(:case) && options.key?(:when))
|
|
54
|
+
raise InvalidThen.new(options)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
@cond = options[:case]
|
|
58
|
+
@branches = options[:when].transform_values do |value|
|
|
59
|
+
case value
|
|
60
|
+
when Hash
|
|
61
|
+
if value.key?(:case)
|
|
62
|
+
Then.new(value)
|
|
63
|
+
elsif value.key?(:then)
|
|
64
|
+
AnonymousState.new(value)
|
|
65
|
+
else
|
|
66
|
+
raise InvalidBranch.new(value, options)
|
|
67
|
+
end
|
|
68
|
+
when Symbol
|
|
69
|
+
Simple.new(value)
|
|
70
|
+
else
|
|
71
|
+
raise InvalidBranch.new(value, options)
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
class InvalidThen < ::LifecycleVM::Error
|
|
77
|
+
attr_reader :options
|
|
78
|
+
|
|
79
|
+
def initialize(options)
|
|
80
|
+
@options = options
|
|
81
|
+
|
|
82
|
+
super("Invalid then configuration #{options}")
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
class InvalidBranch < ::LifecycleVM::Error
|
|
87
|
+
attr_reader :value, :options
|
|
88
|
+
|
|
89
|
+
def initialize(value, options)
|
|
90
|
+
@value = value
|
|
91
|
+
@options = options
|
|
92
|
+
|
|
93
|
+
super("Invalid then option value #{value.inspect} in #{options}")
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
class InvalidCond < ::LifecycleVM::Error
|
|
98
|
+
attr_reader :value, :cond, :branches, :vm
|
|
99
|
+
|
|
100
|
+
def initialize(value, cond, branches, vm)
|
|
101
|
+
@value = value
|
|
102
|
+
@cond = cond
|
|
103
|
+
@branches = branches
|
|
104
|
+
@vm = vm
|
|
105
|
+
|
|
106
|
+
super("Unhandled condition result #{value.inspect} returned by #{cond} in #{branches}. Current context #{@vm}")
|
|
107
|
+
end
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def call(vm)
|
|
111
|
+
state_name = vm.current_state.name
|
|
112
|
+
conditional_name = @cond.name
|
|
113
|
+
logger = vm.logger
|
|
114
|
+
|
|
115
|
+
logger&.debug(:conditional_check, state: state_name, ctx: vm, conditional: conditional_name)
|
|
116
|
+
value = @cond.call(vm.memory)
|
|
117
|
+
logger&.debug(:conditional_result, state: state_name, ctx: vm, conditional: conditional_name, result: value)
|
|
118
|
+
|
|
119
|
+
branch = @branches[value]
|
|
120
|
+
raise InvalidCond.new(value, @cond, @branches, vm) unless branch.respond_to?(:call)
|
|
121
|
+
|
|
122
|
+
branch.call(vm)
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2021 Teak.io, Inc.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
module LifecycleVM
|
|
18
|
+
VERSION = "0.1.0"
|
|
19
|
+
end
|
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2021 Teak.io, Inc.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
require 'forwardable'
|
|
18
|
+
|
|
19
|
+
require 'lifecycle_vm/state'
|
|
20
|
+
require 'lifecycle_vm/memory'
|
|
21
|
+
|
|
22
|
+
module LifecycleVM
|
|
23
|
+
class VM
|
|
24
|
+
extend Forwardable
|
|
25
|
+
|
|
26
|
+
# By default the lifecycle state machine starts at :start, ends at :exit,
|
|
27
|
+
# and immediately termiantes on op failure.
|
|
28
|
+
DEFAULT_START = :start
|
|
29
|
+
DEFAULT_TERMINALS = [:exit].freeze
|
|
30
|
+
DEFAULT_ON_OP_FAILURE = :exit
|
|
31
|
+
|
|
32
|
+
# Stores state machine configuration.
|
|
33
|
+
class Config
|
|
34
|
+
# @return [Symbol, nil] the state to transition to when an op fails
|
|
35
|
+
attr_accessor :on_op_failure
|
|
36
|
+
|
|
37
|
+
# @return [Hash<Symbol, LifecycleVM::State>] a mapping of state names to state structures
|
|
38
|
+
attr_accessor :states
|
|
39
|
+
|
|
40
|
+
# @return [Symbol] the state to start at
|
|
41
|
+
attr_accessor :initial_state
|
|
42
|
+
|
|
43
|
+
# @return [Array<Symbol>] a list of state names which the machine will stop at
|
|
44
|
+
attr_accessor :terminal_states
|
|
45
|
+
|
|
46
|
+
# @return [LifecycleVM::Memory] not an instance of but the class of the memory for this
|
|
47
|
+
# particular machine
|
|
48
|
+
attr_accessor :memory_class
|
|
49
|
+
|
|
50
|
+
# Creates a new Config with default settings
|
|
51
|
+
def initialize
|
|
52
|
+
@states = {}
|
|
53
|
+
@on_op_failure = DEFAULT_ON_OP_FAILURE
|
|
54
|
+
@initial_state = DEFAULT_START
|
|
55
|
+
@terminal_states = DEFAULT_TERMINALS
|
|
56
|
+
@memory_class = LifecycleVM::Memory
|
|
57
|
+
@terminal_states.each do |state|
|
|
58
|
+
@states[state] ||= State.new(state, {})
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Error raised if the machine attempts to enter a state not declared with .on
|
|
64
|
+
class InvalidState < ::LifecycleVM::Error
|
|
65
|
+
# @return [Symbol] the name of the state that was attempted
|
|
66
|
+
attr_reader :state
|
|
67
|
+
|
|
68
|
+
# @return [LifecycleVM::VM] the state machine being executed
|
|
69
|
+
attr_reader :vm
|
|
70
|
+
|
|
71
|
+
# @param state [Symbol] the name of the state that does not exist
|
|
72
|
+
# @param vm [LifecycleVM::VM] the vm raising the error
|
|
73
|
+
def initialize(state, vm)
|
|
74
|
+
@state = state
|
|
75
|
+
@vm = vm
|
|
76
|
+
super("Invalid state transition to #{@state}. Current context #{@vm}")
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
class << self
|
|
81
|
+
# Set the state to transition to if an op fails
|
|
82
|
+
# @param state [Symbol] the state to transition to when any op fails.
|
|
83
|
+
# There may only be one declared failure handler for any VM.
|
|
84
|
+
def on_op_failure(state)
|
|
85
|
+
@config ||= Config.new
|
|
86
|
+
@config.on_op_failure = state
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# Declare a state with an optional op and conditional transitions
|
|
90
|
+
# @param state [Symbol] the name of the state
|
|
91
|
+
# @param opts [Hash]
|
|
92
|
+
# @option opts [LifecycleVM::OpBase] :do (nil) the op to execute upon entering this state
|
|
93
|
+
# @option opts [Symbol, Hash] :then (nil) either the name of the state to transition do
|
|
94
|
+
# after executing :do, or a hash of the form
|
|
95
|
+
# { case: LifecycleVM::CondBase, when: Hash<_, [Symbol, Hash]> }
|
|
96
|
+
def on(state, opts)
|
|
97
|
+
@config ||= Config.new
|
|
98
|
+
@config.states[state] = State.new(state, opts)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# Set the state to start execution at
|
|
102
|
+
def initial(state)
|
|
103
|
+
@config ||= Config.new
|
|
104
|
+
@config.initial_state = state
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Set one or more states as states to halt execution at
|
|
108
|
+
def terminal(*states)
|
|
109
|
+
@config ||= Config.new
|
|
110
|
+
@config.terminal_states += states
|
|
111
|
+
states.each do |state|
|
|
112
|
+
@config.states[state] ||= State.new(state, {})
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# Set the class to be instantiated to store VM memory
|
|
117
|
+
def memory_class(klass)
|
|
118
|
+
@config ||= Config.new
|
|
119
|
+
@config.memory_class = klass
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# @return [Config] the machine configuration
|
|
123
|
+
attr_reader :config
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# The current VM memory
|
|
127
|
+
# @return [LifecycleVM::Memory] the current VM memory
|
|
128
|
+
attr_reader :memory
|
|
129
|
+
|
|
130
|
+
# (see LifecycleVM::VM.config)
|
|
131
|
+
def config
|
|
132
|
+
self.class.config
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
def_delegators :@memory, *(LifecycleVM::Memory::BUILTINS)
|
|
136
|
+
def_delegators :@memory, *(LifecycleVM::Memory::BUILTINS).map { |field| :"#{field}=" }
|
|
137
|
+
|
|
138
|
+
def initialize(memory = nil)
|
|
139
|
+
@memory = memory || config.memory_class.new
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
# Executes the VM until a terminal state is reached.
|
|
143
|
+
# @note May never terminate!
|
|
144
|
+
def call
|
|
145
|
+
next_state = config.initial_state
|
|
146
|
+
loop do
|
|
147
|
+
next_state = do_state(next_state)
|
|
148
|
+
break unless next_state
|
|
149
|
+
end
|
|
150
|
+
self
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Did this vm exit with an errored op?
|
|
154
|
+
# @return [Boolean] true if the vm exited with an errored op
|
|
155
|
+
def errors?
|
|
156
|
+
!!error_op&.errors?
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
# @return [Hash<Symbol, Array<String>>] errors from the errored op
|
|
160
|
+
def errors
|
|
161
|
+
error_op&.errors
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
# Did this vm encounter an error trying to recover from an errored op?
|
|
165
|
+
# @return [Boolean] true if an op associated with the op failure state errored
|
|
166
|
+
def recovery_errors?
|
|
167
|
+
!!current_op&.errors?
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# @return [Hash<Symbol, Array<String>>] errors from the op failure op
|
|
171
|
+
def recovery_errors
|
|
172
|
+
current_op&.errors
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# @private
|
|
176
|
+
def do_anonymous_op(op, following)
|
|
177
|
+
raise InvalidState.new(current_state.name, self) if op && current_op.executed?
|
|
178
|
+
|
|
179
|
+
do_op(op, following)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
private
|
|
183
|
+
|
|
184
|
+
def do_state(next_state)
|
|
185
|
+
logger&.debug(:enter, state: next_state, ctx: self)
|
|
186
|
+
self.last_state = current_state
|
|
187
|
+
|
|
188
|
+
state = config.states[next_state]
|
|
189
|
+
raise InvalidState.new(next_state, self) unless state
|
|
190
|
+
|
|
191
|
+
self.current_state = state
|
|
192
|
+
|
|
193
|
+
return nil if config.terminal_states.include?(current_state.name)
|
|
194
|
+
|
|
195
|
+
do_op(state.op, state.then)
|
|
196
|
+
end
|
|
197
|
+
|
|
198
|
+
OpEntry = Struct.new(:state_name, :op, :errors) do
|
|
199
|
+
def executed?
|
|
200
|
+
!op.nil?
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def errors?
|
|
204
|
+
!(errors.nil? || errors.empty?)
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def do_op(op, following)
|
|
209
|
+
# If we're running under systemd, as determined by the presence of the SdNotify gem,
|
|
210
|
+
# notify the watchdog each time we run an op. This gets us more or less free
|
|
211
|
+
# watchdog monitoring for long running services.
|
|
212
|
+
SdNotify.watchdog if defined?(SdNotify)
|
|
213
|
+
|
|
214
|
+
op_result = op&.call(memory)
|
|
215
|
+
self.current_op = OpEntry.new(current_state.name, op, op_result&.errors)
|
|
216
|
+
|
|
217
|
+
if current_op.errors?
|
|
218
|
+
logger&.error(:op_errors, state_name: current_op.state_name, op_name: current_op.op.name,
|
|
219
|
+
errors: current_op.errors, ctx: self)
|
|
220
|
+
on_op_failure = config.on_op_failure
|
|
221
|
+
|
|
222
|
+
# If we're here, then first an op failed, and then an op executed by our
|
|
223
|
+
# on_op_failure state also failed. In this case, both error_op and
|
|
224
|
+
# current_op should be set, and both should #errors? => true
|
|
225
|
+
return DEFAULT_TERMINALS.first if current_state.name == on_op_failure
|
|
226
|
+
|
|
227
|
+
self.error_op = current_op
|
|
228
|
+
|
|
229
|
+
# We unset current op here so that if there is no failure handler and we're
|
|
230
|
+
# directly transitioning to a terminal state we do not indicate that there
|
|
231
|
+
# were recovery errors (as there was no recovery)
|
|
232
|
+
self.current_op = nil
|
|
233
|
+
|
|
234
|
+
return on_op_failure
|
|
235
|
+
# Only clear our error op if we have successfully executed another op
|
|
236
|
+
elsif op
|
|
237
|
+
self.error_op = nil
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
following.call(self)
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
end
|
data/lib/lifecycle_vm.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Copyright 2021 Teak.io, Inc.
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may not use this file except in compliance with the License.
|
|
7
|
+
# You may obtain a copy of the License at
|
|
8
|
+
#
|
|
9
|
+
# https://www.apache.org/licenses/LICENSE-2.0
|
|
10
|
+
#
|
|
11
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
module LifecycleVM
|
|
18
|
+
class Error < RuntimeError; end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
require 'lifecycle_vm/version'
|
|
22
|
+
require 'lifecycle_vm/cond_base'
|
|
23
|
+
require 'lifecycle_vm/op_base'
|
|
24
|
+
require 'lifecycle_vm/vm'
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "lib/lifecycle_vm/version"
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |spec|
|
|
6
|
+
spec.name = "lifecycle_vm"
|
|
7
|
+
spec.version = LifecycleVM::VERSION
|
|
8
|
+
spec.authors = ["Alex Scarborough"]
|
|
9
|
+
spec.email = ["alex@teak.io"]
|
|
10
|
+
|
|
11
|
+
spec.summary = "Lifecycle VM is a minimal vm to support long running ruby processes."
|
|
12
|
+
spec.description = "Lifecycle VM provides basic lifecycle management following the idea of only executing a single significant operation per program state."
|
|
13
|
+
spec.homepage = "https://github.com/GoCarrot/lifecycle_vm"
|
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.4.0")
|
|
15
|
+
|
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/GoCarrot/lifecycle_vm"
|
|
18
|
+
|
|
19
|
+
# Specify which files should be added to the gem when it is released.
|
|
20
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
21
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
|
22
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features|script)/}) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
spec.require_paths = ["lib"]
|
|
26
|
+
|
|
27
|
+
spec.add_development_dependency('simplecov', '~> 0.21.2')
|
|
28
|
+
spec.add_development_dependency('rspec', '~> 3.10')
|
|
29
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: lifecycle_vm
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Alex Scarborough
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2022-01-14 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: simplecov
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.21.2
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.21.2
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rspec
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.10'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.10'
|
|
41
|
+
description: Lifecycle VM provides basic lifecycle management following the idea of
|
|
42
|
+
only executing a single significant operation per program state.
|
|
43
|
+
email:
|
|
44
|
+
- alex@teak.io
|
|
45
|
+
executables: []
|
|
46
|
+
extensions: []
|
|
47
|
+
extra_rdoc_files: []
|
|
48
|
+
files:
|
|
49
|
+
- ".gitignore"
|
|
50
|
+
- ".rspec"
|
|
51
|
+
- ".ruby-gemset"
|
|
52
|
+
- ".ruby-version"
|
|
53
|
+
- Gemfile
|
|
54
|
+
- LICENSE
|
|
55
|
+
- README.md
|
|
56
|
+
- Rakefile
|
|
57
|
+
- lib/lifecycle_vm.rb
|
|
58
|
+
- lib/lifecycle_vm/cond_base.rb
|
|
59
|
+
- lib/lifecycle_vm/memory.rb
|
|
60
|
+
- lib/lifecycle_vm/op_base.rb
|
|
61
|
+
- lib/lifecycle_vm/state.rb
|
|
62
|
+
- lib/lifecycle_vm/then.rb
|
|
63
|
+
- lib/lifecycle_vm/version.rb
|
|
64
|
+
- lib/lifecycle_vm/vm.rb
|
|
65
|
+
- lifecycle_vm.gemspec
|
|
66
|
+
homepage: https://github.com/GoCarrot/lifecycle_vm
|
|
67
|
+
licenses: []
|
|
68
|
+
metadata:
|
|
69
|
+
homepage_uri: https://github.com/GoCarrot/lifecycle_vm
|
|
70
|
+
source_code_uri: https://github.com/GoCarrot/lifecycle_vm
|
|
71
|
+
post_install_message:
|
|
72
|
+
rdoc_options: []
|
|
73
|
+
require_paths:
|
|
74
|
+
- lib
|
|
75
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
76
|
+
requirements:
|
|
77
|
+
- - ">="
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: 2.4.0
|
|
80
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
|
+
requirements:
|
|
82
|
+
- - ">="
|
|
83
|
+
- !ruby/object:Gem::Version
|
|
84
|
+
version: '0'
|
|
85
|
+
requirements: []
|
|
86
|
+
rubygems_version: 3.1.6
|
|
87
|
+
signing_key:
|
|
88
|
+
specification_version: 4
|
|
89
|
+
summary: Lifecycle VM is a minimal vm to support long running ruby processes.
|
|
90
|
+
test_files: []
|