swarm 0.1.0 → 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
  SHA1:
3
- metadata.gz: ac77d75a6ccdfd4d0c9109f0cc85e95b17f78531
4
- data.tar.gz: 7ec1237fac5184c8f72fb80c53d65c66283378ff
3
+ metadata.gz: c08aee506af8648cba7e34c9bf1a0f6771da43fb
4
+ data.tar.gz: fa7e78cf7ccb4c0f134623f8001a8782af141164
5
5
  SHA512:
6
- metadata.gz: a9d1c231467a7d4635abc5d6534c7792a886d7d05813dbe2e0cc6993c11802eef6e8059d807e8601721db5984b6c18dd76345191c52f01320433f9816a92e025
7
- data.tar.gz: aa831187f5085cad88a575d4ca4ef1259386112aa46906d09b6f1501fa579c6bfad34674d1211904cc386f28263c1673def8d8afa5282f76fb8602bdc70431bc
6
+ metadata.gz: 57e8b887897b7b94f34f0ddec8e90a5ef6024ba9e03deeb03ddb05ca7d802a1e438fcf1b657b750a38020112f7dcfb2fd41ea1b5f9dfbcde4bdc20b00bc0a6e4
7
+ data.tar.gz: 3cea3598a30320cc93a15d41e827714980b2c2f82bec4e4fe196f7989470be9560662ca6a09f9513ebb67715c83e38b305bf63192c4307084518f9502150c44a
data/README.md CHANGED
@@ -1,8 +1,12 @@
1
1
  # Swarm
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/swarm`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ [![Gem Version](https://badge.fury.io/rb/swarm.svg)](https://badge.fury.io/rb/swarm)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ Swarm is a (very much still work-in-progress) workflow engine for Ruby, combining a process definition DSL, a set of built-in expressions, a worker framework to actually run process instances, and a storage mechanism to persist instances and deferred expressions.
6
+
7
+ Currently it requires Redis as a backend to persist state (though for testing you can use transient in-process memory storage). The goal is to extract the storage so other backends can be swapped in as the process state repository.
8
+
9
+ For performing the work itself, Swarm only comes with an in-process memory storage, which means jobs queued up at the time of stopping the worker will be lost. For a non-volatile worker, you'll want to use [swarm-beanstalk](http://github.com/bumbleworks/swarm-beanstalk), which uses [beanstalkd](http://kr.github.io/beanstalkd/) (via [beaneater](https://github.com/beanstalkd/beaneater)).
6
10
 
7
11
  ## Installation
8
12
 
@@ -22,18 +26,16 @@ Or install it yourself as:
22
26
 
23
27
  ## Usage
24
28
 
25
- TODO: Write usage instructions here
26
-
27
- ## Development
29
+ Instructions coming soon!
28
30
 
29
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
31
+ ## Acknowledgements
30
32
 
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` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
33
+ Huge thanks to @jmettraux, author of (now discontinued) [ruote](https://github.com/jmettraux/ruote), for getting me excited about workflow engines, and for inspiring this fun and educational project of mine.
32
34
 
33
35
  ## Contributing
34
36
 
35
- 1. Fork it ( https://github.com/[my-github-username]/swarm/fork )
36
- 2. Create your feature branch (`git checkout -b my-new-feature`)
37
- 3. Commit your changes (`git commit -am 'Add some feature'`)
38
- 4. Push to the branch (`git push origin my-new-feature`)
39
- 5. Create a new Pull Request
37
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/swarm-beanstalk. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/lib/swarm.rb CHANGED
@@ -17,7 +17,8 @@ require "swarm/stored_workitem"
17
17
  require "swarm/participants/trace_participant"
18
18
  require "swarm/participants/storage_participant"
19
19
  require "swarm/pollen/reader"
20
- require "redis"
20
+ require "swarm/storage"
21
+
21
22
 
22
23
  module Swarm
23
24
  # Your code goes here...
data/lib/swarm/hive.rb CHANGED
@@ -1,5 +1,3 @@
1
- require_relative "storage"
2
-
3
1
  module Swarm
4
2
  class Hive
5
3
  class MissingTypeError < StandardError; end
@@ -34,7 +32,11 @@ module Swarm
34
32
  end
35
33
 
36
34
  def inspect
37
- "#<Swarm::Hive storage: #{storage.backend.class}, work_queue: #{work_queue.name}>"
35
+ "#<Swarm::Hive storage: #{storage_class}, work_queue: #{work_queue.name}>"
36
+ end
37
+
38
+ def storage_class
39
+ storage.class.name.split('::').last
38
40
  end
39
41
 
40
42
  def traced
@@ -55,7 +55,8 @@ module Swarm
55
55
  def save
56
56
  if new? || changed?
57
57
  @id ||= Swarm::Support.uuid_with_timestamp
58
- storage[storage_id] = to_hash
58
+ storage[storage_id] = to_hash.merge(:updated_at => Time.now)
59
+ reload!
59
60
  end
60
61
  self
61
62
  end
@@ -95,14 +96,22 @@ module Swarm
95
96
  super
96
97
  subclass.instance_variable_set(:@columns, [])
97
98
  subclass.instance_variable_set(:@associations, [])
99
+ subclass.set_columns :updated_at, :created_at
98
100
  end
99
101
 
100
102
  def set_columns(*args)
101
- attr_reader *args
102
103
  args.each do |arg|
103
104
  define_method("#{arg}=") { |value|
104
105
  change_attribute(arg, value)
105
106
  }
107
+
108
+ define_method(arg) {
109
+ val = instance_variable_get(:"@#{arg}")
110
+ if /_at$/.match(arg) && val.is_a?(String)
111
+ val = Time.parse(val)
112
+ end
113
+ val
114
+ }
106
115
  end
107
116
  @columns = @columns | args
108
117
  end
@@ -121,7 +130,7 @@ module Swarm
121
130
  end
122
131
 
123
132
  def create(hive: Hive.default, **args)
124
- new(hive: hive, **args).save
133
+ new(hive: hive, created_at: Time.now, **args).save
125
134
  end
126
135
 
127
136
  def storage_type
@@ -39,7 +39,6 @@ module Swarm
39
39
  Process.create(
40
40
  args.merge({
41
41
  :workitem => workitem,
42
- :hive => hive,
43
42
  :process_definition_id => id
44
43
  })
45
44
  )
data/lib/swarm/storage.rb CHANGED
@@ -1,56 +1,7 @@
1
- module Swarm
2
- class Storage
3
- attr_reader :backend
4
-
5
- def initialize(backend)
6
- @backend = backend
7
- end
8
-
9
- def regex_for_type(type)
10
- /^#{type}\:(.*)/
11
- end
12
-
13
- def ids_for_type(type)
14
- keys = if backend.is_a?(Redis)
15
- backend.keys("#{type}:*")
16
- else
17
- backend.keys.select { |key| key.match(regex_for_type(type)) }
18
- end
19
- keys.map { |key| key.gsub(regex_for_type(type), '\1') }
20
- end
21
-
22
- def serialize(value)
23
- return nil if value.nil?
24
- value.to_json
25
- end
1
+ require_relative "storage/hash_storage"
2
+ require_relative "storage/redis_storage"
26
3
 
27
- def deserialize(value)
28
- return nil if value.nil?
29
- JSON.parse(value)
30
- end
31
-
32
- def [](key)
33
- deserialize(backend[key])
34
- end
35
-
36
- def []=(key, value)
37
- backend[key] = serialize(value)
38
- end
39
-
40
- def delete(key)
41
- if backend.respond_to?(:del)
42
- backend.del(key)
43
- else
44
- backend.delete(key)
45
- end
46
- end
47
-
48
- def truncate
49
- if backend.respond_to?(:flushdb)
50
- backend.flushdb
51
- else
52
- backend.clear
53
- end
54
- end
4
+ module Swarm
5
+ module Storage
55
6
  end
56
7
  end
@@ -0,0 +1,20 @@
1
+ require_relative "key_value_storage"
2
+
3
+ module Swarm
4
+ module Storage
5
+ class HashStorage < KeyValueStorage
6
+ def ids_for_type(type)
7
+ keys = store.keys.select { |key| key.match(regex_for_type(type)) }
8
+ keys.map { |key| key.gsub(regex_for_type(type), '\1') }
9
+ end
10
+
11
+ def delete(key)
12
+ store.delete(key)
13
+ end
14
+
15
+ def truncate
16
+ store.clear
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,45 @@
1
+ module Swarm
2
+ module Storage
3
+ class KeyValueStorage
4
+ attr_reader :store
5
+
6
+ def initialize(store)
7
+ @store = store
8
+ end
9
+
10
+ def regex_for_type(type)
11
+ /^#{type}\:(.*)/
12
+ end
13
+
14
+ def ids_for_type(type)
15
+ raise "Not implemented yet!"
16
+ end
17
+
18
+ def serialize(value)
19
+ return nil if value.nil?
20
+ value.to_json
21
+ end
22
+
23
+ def deserialize(value)
24
+ return nil if value.nil?
25
+ JSON.parse(value)
26
+ end
27
+
28
+ def [](key)
29
+ deserialize(store[key])
30
+ end
31
+
32
+ def []=(key, value)
33
+ store[key] = serialize(value)
34
+ end
35
+
36
+ def delete(key)
37
+ raise "Not implemented yet!"
38
+ end
39
+
40
+ def truncate
41
+ raise "Not implemented yet!"
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,20 @@
1
+ require "redis"
2
+ require_relative "key_value_storage"
3
+
4
+ module Swarm
5
+ module Storage
6
+ class RedisStorage < KeyValueStorage
7
+ def ids_for_type(type)
8
+ store.keys("#{type}:*").map { |key| key.gsub(regex_for_type(type), '\1') }
9
+ end
10
+
11
+ def delete(key)
12
+ store.del(key)
13
+ end
14
+
15
+ def truncate
16
+ store.flushdb
17
+ end
18
+ end
19
+ end
20
+ end
data/lib/swarm/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Swarm
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: swarm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ravi Gadad
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-01-01 00:00:00.000000000 Z
11
+ date: 2016-02-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redis
@@ -168,6 +168,9 @@ files:
168
168
  - lib/swarm/process_definition.rb
169
169
  - lib/swarm/router.rb
170
170
  - lib/swarm/storage.rb
171
+ - lib/swarm/storage/hash_storage.rb
172
+ - lib/swarm/storage/key_value_storage.rb
173
+ - lib/swarm/storage/redis_storage.rb
171
174
  - lib/swarm/stored_workitem.rb
172
175
  - lib/swarm/support.rb
173
176
  - lib/swarm/version.rb