swarm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rspec +3 -0
  4. data/.ruby-version +1 -0
  5. data/CODE_OF_CONDUCT.md +13 -0
  6. data/Gemfile +4 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +39 -0
  9. data/Rakefile +13 -0
  10. data/bin/console +14 -0
  11. data/bin/setup +7 -0
  12. data/lib/swarm/engine/base/job.rb +31 -0
  13. data/lib/swarm/engine/base/queue.rb +60 -0
  14. data/lib/swarm/engine/volatile/job.rb +57 -0
  15. data/lib/swarm/engine/volatile/queue.rb +85 -0
  16. data/lib/swarm/engine/worker/command.rb +61 -0
  17. data/lib/swarm/engine/worker.rb +73 -0
  18. data/lib/swarm/evaluation/expression_evaluator.rb +40 -0
  19. data/lib/swarm/evaluation/workitem_context.rb +17 -0
  20. data/lib/swarm/expression.rb +107 -0
  21. data/lib/swarm/expressions/activity_expression.rb +11 -0
  22. data/lib/swarm/expressions/branch_expression.rb +44 -0
  23. data/lib/swarm/expressions/concurrence_expression.rb +41 -0
  24. data/lib/swarm/expressions/conditional_expression.rb +36 -0
  25. data/lib/swarm/expressions/sequence_expression.rb +16 -0
  26. data/lib/swarm/expressions/subprocess_expression.rb +14 -0
  27. data/lib/swarm/hive.rb +69 -0
  28. data/lib/swarm/hive_dweller.rb +170 -0
  29. data/lib/swarm/observers/base.rb +17 -0
  30. data/lib/swarm/participant.rb +18 -0
  31. data/lib/swarm/participants/storage_participant.rb +12 -0
  32. data/lib/swarm/participants/trace_participant.rb +27 -0
  33. data/lib/swarm/pollen/parser.rb +95 -0
  34. data/lib/swarm/pollen/reader.rb +22 -0
  35. data/lib/swarm/pollen/transformer.rb +66 -0
  36. data/lib/swarm/process.rb +57 -0
  37. data/lib/swarm/process_definition.rb +53 -0
  38. data/lib/swarm/router.rb +18 -0
  39. data/lib/swarm/storage.rb +56 -0
  40. data/lib/swarm/stored_workitem.rb +15 -0
  41. data/lib/swarm/support.rb +81 -0
  42. data/lib/swarm/version.rb +3 -0
  43. data/lib/swarm.rb +24 -0
  44. data/swarm.gemspec +31 -0
  45. metadata +199 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac77d75a6ccdfd4d0c9109f0cc85e95b17f78531
4
+ data.tar.gz: 7ec1237fac5184c8f72fb80c53d65c66283378ff
5
+ SHA512:
6
+ metadata.gz: a9d1c231467a7d4635abc5d6534c7792a886d7d05813dbe2e0cc6993c11802eef6e8059d807e8601721db5984b6c18dd76345191c52f01320433f9816a92e025
7
+ data.tar.gz: aa831187f5085cad88a575d4ca4ef1259386112aa46906d09b6f1501fa579c6bfad34674d1211904cc386f28263c1673def8d8afa5282f76fb8602bdc70431bc
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format documentation
3
+ --require spec_helper
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.1.
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in swarm.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Ravi Gadad
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Swarm
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.
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 'swarm'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install swarm
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 `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` to 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
+ 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
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rspec/core/rake_task'
4
+
5
+ RSpec::Core::RakeTask.new(:spec, :tag) do |t, task_args|
6
+ if task_args[:tag]
7
+ t.rspec_opts = "--tag #{task_args[:tag]}"
8
+ end
9
+ end
10
+
11
+ task :default => [:spec]
12
+
13
+ Rake::TaskManager.record_task_metadata = true
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "swarm"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,31 @@
1
+ module Swarm
2
+ module Engine
3
+ class Job
4
+ class AlreadyReservedError < StandardError; end
5
+
6
+ def to_h
7
+ raise "Not implemented yet!"
8
+ end
9
+
10
+ def reserved?
11
+ raise "Not implemented yet!"
12
+ end
13
+
14
+ def bury
15
+ raise "Not implemented yet!"
16
+ end
17
+
18
+ def release
19
+ raise "Not implemented yet!"
20
+ end
21
+
22
+ def delete
23
+ raise "Not implemented yet!"
24
+ end
25
+
26
+ def exists?
27
+ raise "Not implemented yet!"
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,60 @@
1
+ require_relative "job"
2
+
3
+ module Swarm
4
+ module Engine
5
+ class Queue
6
+ class JobReservationFailed < StandardError; end
7
+ class JobNotFoundError < StandardError; end
8
+
9
+ attr_reader :name
10
+
11
+ def initialize(name:)
12
+ @name = name
13
+ end
14
+
15
+ def prepare_for_work(worker)
16
+ raise "Not implemented yet!"
17
+ end
18
+
19
+ def add_job(data)
20
+ raise "Not implemented yet!"
21
+ end
22
+
23
+ def reserve_job(worker)
24
+ raise "Not implemented yet!"
25
+ end
26
+
27
+ def delete_job(job)
28
+ job.delete
29
+ end
30
+
31
+ def bury_job(job)
32
+ job.bury if job.exists?
33
+ end
34
+
35
+ def clean_up_job(job)
36
+ job.bury if job.exists? && job.reserved?
37
+ end
38
+
39
+ def remove_worker(worker, stop_job:)
40
+ if worker_count <= 1
41
+ stop_job.delete
42
+ else
43
+ stop_job.release
44
+ end
45
+ end
46
+
47
+ def clear
48
+ raise "Not implemented yet!"
49
+ end
50
+
51
+ def idle?
52
+ raise "Not implemented yet!"
53
+ end
54
+
55
+ def worker_count
56
+ raise "Not implemented yet!"
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,57 @@
1
+ module Swarm
2
+ module Engine
3
+ module Volatile
4
+ class Job < Swarm::Engine::Job
5
+ attr_reader :queue, :data, :id, :reserved_by, :buried
6
+
7
+ def initialize(queue:, data:)
8
+ @queue = queue
9
+ @data = data
10
+ @id = SecureRandom.uuid
11
+ @reserved_by = nil
12
+ @buried = false
13
+ end
14
+
15
+ def to_h
16
+ Swarm::Support.symbolize_keys(data)
17
+ end
18
+
19
+ def ==(other)
20
+ other.is_a?(self.class) &&
21
+ other.id == id
22
+ end
23
+
24
+ def reserve!(worker)
25
+ if reserved_by && reserved_by != worker
26
+ raise AlreadyReservedError
27
+ end
28
+ @reserved_by = worker
29
+ end
30
+
31
+ def reserved?
32
+ !reserved_by.nil?
33
+ end
34
+
35
+ def bury
36
+ @buried = true
37
+ end
38
+
39
+ def available?
40
+ !reserved? && !buried
41
+ end
42
+
43
+ def release
44
+ @reserved_by = nil
45
+ end
46
+
47
+ def delete
48
+ queue.delete_job(self)
49
+ end
50
+
51
+ def exists?
52
+ queue.has_job?(self)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,85 @@
1
+ require_relative "job"
2
+
3
+ module Swarm
4
+ module Engine
5
+ module Volatile
6
+ class Queue < Swarm::Engine::Queue
7
+ Tube = Struct.new(:jobs, :workers)
8
+
9
+ extend Forwardable
10
+ def_delegators :tube, :jobs, :workers
11
+
12
+ attr_reader :tube, :name
13
+
14
+ class << self
15
+ def tubes
16
+ @tubes ||= {}
17
+ end
18
+
19
+ def get_tube(name)
20
+ tubes[name] ||= Tube.new([], [])
21
+ end
22
+ end
23
+
24
+ def initialize(name:)
25
+ @name = name
26
+ @tube = self.class.get_tube(name)
27
+ end
28
+
29
+ def prepare_for_work(worker)
30
+ add_worker(worker) unless workers.include?(worker)
31
+ self
32
+ end
33
+
34
+ def add_job(data)
35
+ new_job = Job.new(queue: self, data: data)
36
+ jobs << new_job
37
+ new_job
38
+ end
39
+
40
+ def wait_for_job
41
+ delay_time = 0
42
+ until jobs.count > 0
43
+ delay_time += 0.01 unless delay_time > 1.0
44
+ sleep(delay_time)
45
+ end
46
+ end
47
+
48
+ def reserve_job(worker)
49
+ wait_for_job
50
+ index = jobs.index { |job| job.available? }
51
+ raise JobNotFoundError unless index
52
+ job = jobs[index]
53
+ job.reserve!(worker)
54
+ job
55
+ rescue JobNotFoundError, Job::AlreadyReservedError
56
+ raise JobReservationFailed
57
+ end
58
+
59
+ def delete_job(job_to_delete)
60
+ jobs.delete_if { |job| job == job_to_delete }
61
+ end
62
+
63
+ def has_job?(job_to_find)
64
+ jobs.any? { |job| job == job_to_find }
65
+ end
66
+
67
+ def clear
68
+ tube.jobs = []
69
+ end
70
+
71
+ def worker_count
72
+ workers.count
73
+ end
74
+
75
+ def add_worker(worker)
76
+ workers << worker
77
+ end
78
+
79
+ def idle?
80
+ jobs.empty?
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,61 @@
1
+ module Swarm
2
+ module Engine
3
+ class Worker
4
+ class Command
5
+ class MissingObjectError < StandardError; end
6
+
7
+ class << self
8
+ def from_job(job, hive: Hive.default)
9
+ data = job.to_h
10
+ action, metadata = data.values_at(:action, :metadata)
11
+ new(action: action, metadata: metadata, hive: hive)
12
+ end
13
+ end
14
+
15
+ attr_reader :action, :metadata, :hive
16
+
17
+ def initialize(hive: Hive.default, action:, metadata:)
18
+ @hive = hive
19
+ @action = action
20
+ @metadata = Swarm::Support.symbolize_keys(metadata || {})
21
+ end
22
+
23
+ def run!
24
+ raise MissingObjectError if object.nil?
25
+ observers.each(&:before_action)
26
+ object.send("_#{action}")
27
+ observers.each(&:after_action)
28
+ end
29
+
30
+ def observers
31
+ @observers ||= hive.registered_observers.map { |observer_class|
32
+ observer_class.new(self)
33
+ }
34
+ end
35
+
36
+ def object
37
+ @object ||= begin
38
+ return nil unless metadata[:type] && metadata[:id]
39
+ hive.fetch(metadata[:type], metadata[:id])
40
+ end
41
+ end
42
+
43
+ def stop?
44
+ action == "stop_worker"
45
+ end
46
+
47
+ def to_hash
48
+ {
49
+ action: action,
50
+ metadata: metadata,
51
+ object: object
52
+ }
53
+ end
54
+
55
+ def ==(other)
56
+ other.is_a?(self.class) && other.to_hash == to_hash
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,73 @@
1
+ require_relative "worker/command"
2
+
3
+ module Swarm
4
+ module Engine
5
+ class Worker
6
+ class NotRunningError < StandardError; end
7
+
8
+ attr_reader :hive, :queue
9
+
10
+ def initialize(hive: Hive.default)
11
+ @hive = hive
12
+ end
13
+
14
+ def setup
15
+ @queue = hive.work_queue.prepare_for_work(self)
16
+ end
17
+
18
+ def teardown
19
+ @queue = nil
20
+ end
21
+
22
+ def run!
23
+ setup
24
+ @running = true
25
+ while running?
26
+ process_next_job
27
+ end
28
+ teardown
29
+ end
30
+
31
+ def process_next_job
32
+ begin
33
+ @current_job = queue.reserve_job(self)
34
+ @working = true
35
+ work_on(@current_job)
36
+ queue.delete_job(@current_job) if @current_job
37
+ rescue Queue::JobReservationFailed
38
+ retry
39
+ rescue StandardError
40
+ queue.bury_job(@current_job) if @current_job
41
+ ensure
42
+ queue.clean_up_job(@current_job) if @current_job
43
+ @working = false
44
+ @current_job = nil
45
+ end
46
+ end
47
+
48
+ def working?
49
+ @working == true
50
+ end
51
+
52
+ def running?
53
+ @running == true && queue
54
+ end
55
+
56
+ def stop!
57
+ @running = false
58
+ @current_job = nil
59
+ end
60
+
61
+ def work_on(queue_job)
62
+ raise NotRunningError unless running?
63
+ command = Command.from_job(queue_job, hive: hive)
64
+ if command.stop?
65
+ queue.remove_worker(self, :stop_job => queue_job)
66
+ stop!
67
+ else
68
+ command.run!
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,40 @@
1
+ require_relative "workitem_context"
2
+
3
+ module Swarm
4
+ class ExpressionEvaluator
5
+ extend Forwardable
6
+
7
+ attr_reader :expression
8
+ def_delegators :expression, :workitem, :arguments
9
+
10
+ def initialize(expression)
11
+ @expression = expression
12
+ end
13
+
14
+ def workitem_context
15
+ @workitem_context ||= WorkitemContext.new(workitem)
16
+ end
17
+
18
+ def eval(string)
19
+ workitem_context.instance_eval(string)
20
+ end
21
+
22
+ def all_conditions_met?
23
+ conditions.all? { |type, exp|
24
+ check_condition(type, exp)
25
+ }
26
+ end
27
+
28
+ def check_condition(type, exp)
29
+ unless ["if", "unless"].include?(type)
30
+ raise ArgumentError.new("Not a conditional")
31
+ end
32
+ result = eval(exp)
33
+ type == "if" ? result : !result
34
+ end
35
+
36
+ def conditions
37
+ Swarm::Support.slice(arguments, "if", "unless")
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ module Swarm
2
+ class WorkitemContext
3
+ attr_reader :workitem
4
+
5
+ def initialize(workitem)
6
+ @workitem = Support.symbolize_keys(workitem)
7
+ end
8
+
9
+ def method_missing(method, *args)
10
+ if workitem.has_key?(method)
11
+ workitem.fetch(method, nil)
12
+ else
13
+ super
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,107 @@
1
+ require_relative "evaluation/expression_evaluator"
2
+
3
+ module Swarm
4
+ class Expression < HiveDweller
5
+ class << self
6
+ def storage_type
7
+ "Expression"
8
+ end
9
+
10
+ def inherited(subclass)
11
+ super
12
+ subclass.set_columns *columns
13
+ end
14
+ end
15
+
16
+ set_columns :parent_id, :position, :workitem, :child_ids, :milestones, :process_id
17
+ many_to_one :process, :class_name => "Swarm::Process"
18
+
19
+ def branch_position
20
+ @branch_position ||= position.last
21
+ end
22
+
23
+ def evaluator
24
+ @evaluator ||= Swarm::ExpressionEvaluator.new(self)
25
+ end
26
+
27
+ def meets_conditions?
28
+ evaluator.all_conditions_met?
29
+ end
30
+
31
+ def root?
32
+ process_id == parent_id
33
+ end
34
+
35
+ def parent
36
+ if root?
37
+ process
38
+ else
39
+ Expression.fetch(parent_id, hive: hive)
40
+ end
41
+ end
42
+
43
+ def apply
44
+ hive.queue('apply', self)
45
+ end
46
+
47
+ def reply
48
+ save
49
+ hive.queue('reply', self)
50
+ end
51
+
52
+ def _apply
53
+ set_milestone("applied_at")
54
+ meets_conditions? ? work : reply
55
+ save
56
+ end
57
+
58
+ def _reply
59
+ set_milestone("replied_at")
60
+ save
61
+ reply_to_parent
62
+ end
63
+
64
+ def reply_to_parent
65
+ parent.move_on_from(self)
66
+ end
67
+
68
+ def replied_at
69
+ get_milestone("replied_at")
70
+ end
71
+
72
+ def replied?
73
+ reload!
74
+ !!replied_at
75
+ end
76
+
77
+ def node
78
+ @node ||= parent.node_at_position(branch_position)
79
+ end
80
+
81
+ def command
82
+ node[0]
83
+ end
84
+
85
+ def arguments
86
+ node[1]
87
+ end
88
+
89
+ def tree
90
+ node[2]
91
+ end
92
+
93
+ def node_at_position(position)
94
+ tree[position]
95
+ end
96
+
97
+ private
98
+
99
+ def set_milestone(name, at: Time.now.to_i)
100
+ self.milestones = (milestones || {}).merge(name => at)
101
+ end
102
+
103
+ def get_milestone(name)
104
+ (milestones || {})[name]
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,11 @@
1
+ module Swarm
2
+ class ActivityExpression < Expression
3
+ def work
4
+ if command == "trace"
5
+ TraceParticipant.new(hive: hive, expression: self).work
6
+ else
7
+ StorageParticipant.new(hive: hive, expression: self).work
8
+ end
9
+ end
10
+ end
11
+ end