duties 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: edbe40e92993677875bf20a969da0da52510e54a
4
+ data.tar.gz: 1285e2050b670d768cca9e561d2524117d92a3aa
5
+ SHA512:
6
+ metadata.gz: c1cde1edaf89b8d427d6b59a23b59727efe16e79b9692d6a7919b3ead4244107bbaccbc466c29cc2b97aa0721d669e9d17347a869dffe0c999478347651d84e0
7
+ data.tar.gz: bf9e023647f0f35f8d2a0ea6cc618ef6f2f9f22abf3abc389cc87ebaa88fb32ffc3648e78308f0125cbe97e53fd6353d1c2678c5ce41e63e5841b5ad1ae1830f
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Pat Allan & Inspire9
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,52 @@
1
+ # Duties
2
+
3
+ A Rails engine that runs activities related to a duty in a specific order, via Sidekiq.
4
+
5
+ Duties are composed of one or more activities. These activities have positions, and they can share positions, which allows for parallel processing.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'duties', '0.0.1'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```ruby
18
+ # config/initializers/duties.rb
19
+ Duties.duty_namespace = MyDuties
20
+ Duties.activity_namespace = MyActivities
21
+
22
+ # app/lib/my_duties/compile.rb
23
+ class MyDuties::Compile < Duties::Duty
24
+ def enqueue_activities
25
+ enqueue_activity 'clean', at: 1
26
+ enqueue_activity 'configure', at: 2
27
+ enqueue_activity 'make', at: 3
28
+ end
29
+ end
30
+
31
+ # app/lib/my_activities/clean.rb
32
+ class MyActivities::Clean < Duties::Activity
33
+ def clean
34
+ # can use data to access duty information provided when queued.
35
+ end
36
+ end
37
+
38
+ # Wherever you want to queue up the compile duty
39
+ Duties::Duty.enqueue 'compile', 'foo' => 'bar'
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it ( https://github.com/inspire9/duties/fork )
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create a new Pull Request
49
+
50
+ ## Credits
51
+
52
+ Copyright (c) 2014, Duties is developed and maintained by Pat Allan and [Inspire9](http://inspire9.com), and is released under the open MIT Licence.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler/gem_tasks'
2
+
@@ -0,0 +1,16 @@
1
+ class Duties::ActivityRecord < ActiveRecord::Base
2
+ self.table_name = 'duties_activity_records'
3
+
4
+ belongs_to :duty_record, class_name: 'Duties::DutyRecord'
5
+
6
+ validates :name, presence: true
7
+ validates :duty_record, presence: true
8
+ validates :position, presence: true
9
+
10
+ scope :by_position, lambda { |position| where(position: position) }
11
+ scope :failures, lambda { where status: 'failure' }
12
+
13
+ def success?
14
+ status == 'success'
15
+ end
16
+ end
@@ -0,0 +1,8 @@
1
+ class Duties::DutyRecord < ActiveRecord::Base
2
+ self.table_name = 'duties_duty_records'
3
+
4
+ has_many :activity_records, class_name: 'Duties::ActivityRecord'
5
+ serialize :data, JSON
6
+
7
+ validates :name, presence: true
8
+ end
data/config.ru ADDED
@@ -0,0 +1,7 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler.require :default, :development
5
+
6
+ Combustion.initialize!
7
+ run Combustion::Application
@@ -0,0 +1,23 @@
1
+ class CreateDutiesAndActivities < ActiveRecord::Migration
2
+ def change
3
+ create_table :duties_duty_records do |t|
4
+ t.string :name, null: false
5
+ t.text :data, default: '{}'
6
+ t.timestamps
7
+ end
8
+
9
+ add_index :duties_duty_records, :created_at, order: {created_at: :desc}
10
+ add_index :duties_duty_records, :name
11
+
12
+ create_table :duties_activity_records do |t|
13
+ t.string :name, null: false
14
+ t.integer :duty_record_id, null: false
15
+ t.integer :position, null: false
16
+ t.string :status, null: false, default: 'pending'
17
+ t.timestamps
18
+ end
19
+
20
+ add_index :duties_activity_records, :duty_record_id
21
+ add_index :duties_activity_records, :name
22
+ end
23
+ end
data/duties.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = 'duties'
4
+ spec.version = '0.0.1'
5
+ spec.authors = ['Pat Allan']
6
+ spec.email = ['pat@freelancing-gods.com']
7
+ spec.summary = %q{Run activities related to a duty in a specific order}
8
+ spec.description = %q{A Rails engine that runs activities related to a duty in a specific order, via Sidekiq.}
9
+ spec.homepage = 'https://github.com/inspire9/duties'
10
+ spec.license = 'MIT'
11
+
12
+ spec.files = `git ls-files -z`.split("\x0")
13
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
14
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
15
+ spec.require_paths = ['lib']
16
+
17
+ spec.add_runtime_dependency 'json'
18
+ spec.add_runtime_dependency 'rails', '~> 4.0'
19
+ spec.add_runtime_dependency 'sidekiq', '> 2.17.0'
20
+
21
+ spec.add_development_dependency 'combustion', '0.5.1'
22
+ spec.add_development_dependency 'rspec-rails', '~> 3.0.2'
23
+ spec.add_development_dependency 'sqlite3', '~> 1.3.8'
24
+ end
@@ -0,0 +1,24 @@
1
+ class Duties::Activity
2
+ def self.call(record)
3
+ activity = new(record)
4
+ begin
5
+ activity.call
6
+ record.update_attributes status: 'success'
7
+
8
+ Duties::Next.call record.duty_record, record.position
9
+ rescue => error
10
+ record.update_attributes status: 'failure'
11
+ end
12
+ end
13
+
14
+ def initialize(activity)
15
+ @activity = activity
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :activity
21
+
22
+ delegate :duty, to: :activity
23
+ delegate :data, to: :duty
24
+ end
@@ -0,0 +1,19 @@
1
+ class Duties::ActivityJob
2
+ include Sidekiq::Worker
3
+
4
+ sidekiq_options :retry => true
5
+
6
+ def perform(activity_id)
7
+ activity = Duties::ActivityRecord.find_by_id activity_id
8
+ return if activity.nil?
9
+ klass = activity_class activity.name.classify
10
+
11
+ klass.call activity
12
+ end
13
+
14
+ private
15
+
16
+ def activity_class(name)
17
+ Duties.activity_namespace.const_get name
18
+ end
19
+ end
@@ -0,0 +1,40 @@
1
+ class Duties::Duty
2
+ def self.call(record)
3
+ new(record).call
4
+ end
5
+
6
+ def self.enqueue(name, data = {})
7
+ duty = Duties::DutyRecord.create! name: name, data: data
8
+
9
+ Duties::DutyJob.perform_async duty.id
10
+
11
+ duty.id
12
+ end
13
+
14
+ def initialize(record)
15
+ @record = record
16
+ end
17
+
18
+ def enqueue_activities
19
+ # Hook for subclasses to implement
20
+ end
21
+
22
+ def call
23
+ enqueue_activities
24
+ start_activities
25
+ end
26
+
27
+ private
28
+
29
+ attr_reader :record
30
+
31
+ def enqueue_activity(name, options)
32
+ record.activity_records.create! name: name, position: options[:at]
33
+ end
34
+
35
+ def start_activities
36
+ record.activity_records.by_position(1).each do |activity|
37
+ Duties::ActivityJob.perform_async activity.id
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,18 @@
1
+ class Duties::DutyJob
2
+ include Sidekiq::Worker
3
+
4
+ sidekiq_options retry: true
5
+
6
+ def perform(duty_id)
7
+ duty = Duties::DutyRecord.find duty_id
8
+ klass = duty_class duty.name.classify
9
+
10
+ klass.call duty
11
+ end
12
+
13
+ private
14
+
15
+ def duty_class(name)
16
+ Duties.duty_namespace.const_get name
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ class Duties::Engine < Rails::Engine
2
+ engine_name :duties
3
+ end
@@ -0,0 +1,37 @@
1
+ class Duties::Next
2
+ def self.call(record, position)
3
+ new(record, position).call
4
+ end
5
+
6
+ def initialize(record, position)
7
+ @record, @position = record, position
8
+ end
9
+
10
+ def call
11
+ # We're done here. Stop.
12
+ return if any_failures?
13
+ return unless finished_current? && more_to_come?
14
+
15
+ activity_records.by_position(position + 1).each do |activity|
16
+ Duties::ActivityJob.perform_async activity.id
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ attr_reader :record, :position
23
+
24
+ delegate :activity_records, to: :record
25
+
26
+ def any_failures?
27
+ activity_records.by_position(position).failures.any?
28
+ end
29
+
30
+ def finished_current?
31
+ activity_records.by_position(position).all? &:success?
32
+ end
33
+
34
+ def more_to_come?
35
+ activity_records.by_position(position + 1).any?
36
+ end
37
+ end
data/lib/duties.rb ADDED
@@ -0,0 +1,12 @@
1
+ require 'sidekiq'
2
+
3
+ module Duties
4
+ mattr_accessor :duty_namespace, :activity_namespace
5
+ end
6
+
7
+ require 'duties/activity'
8
+ require 'duties/activity_job'
9
+ require 'duties/duty'
10
+ require 'duties/duty_job'
11
+ require 'duties/engine'
12
+ require 'duties/next'
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ module Running
4
+ module Duties
5
+ class ExampleOne < ::Duties::Duty
6
+ def enqueue_activities
7
+ enqueue_activity 'success_one', at: 1
8
+ enqueue_activity 'success_two', at: 2
9
+ end
10
+ end
11
+ end
12
+
13
+ module Activities
14
+ class SuccessOne < ::Duties::Activity
15
+ def call
16
+ #
17
+ end
18
+ end
19
+
20
+ class SuccessTwo < ::Duties::Activity
21
+ def call
22
+ #
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ describe 'Running duties' do
29
+ before :each do
30
+ Duties.duty_namespace = Running::Duties
31
+ Duties.activity_namespace = Running::Activities
32
+ end
33
+
34
+ it 'processes each activity for the duty' do
35
+ id = Duties::Duty.enqueue 'example_one'
36
+
37
+ duty = Duties::DutyRecord.find(id)
38
+ expect(duty.activity_records.length).to eq(2)
39
+ expect(duty.activity_records.pluck(:status)).to eq(['success', 'success'])
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ test:
2
+ adapter: sqlite3
3
+ database: db/combustion_test.sqlite
@@ -0,0 +1,3 @@
1
+ Rails.application.routes.draw do
2
+ #
3
+ end
@@ -0,0 +1,3 @@
1
+ ActiveRecord::Schema.define do
2
+ #
3
+ end
@@ -0,0 +1 @@
1
+ *.log
File without changes
@@ -0,0 +1,18 @@
1
+ require 'bundler'
2
+
3
+ Bundler.setup :default, :development
4
+
5
+ require 'rails'
6
+ require 'combustion'
7
+ require 'duties'
8
+ require 'sidekiq/testing'
9
+
10
+ Combustion.initialize! :action_controller, :active_record
11
+
12
+ require 'rspec/rails'
13
+
14
+ Sidekiq::Testing.inline!
15
+
16
+ RSpec.configure do |config|
17
+ config.use_transactional_fixtures = true
18
+ end
metadata ADDED
@@ -0,0 +1,162 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: duties
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Pat Allan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sidekiq
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">"
46
+ - !ruby/object:Gem::Version
47
+ version: 2.17.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">"
53
+ - !ruby/object:Gem::Version
54
+ version: 2.17.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: combustion
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '='
60
+ - !ruby/object:Gem::Version
61
+ version: 0.5.1
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '='
67
+ - !ruby/object:Gem::Version
68
+ version: 0.5.1
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec-rails
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 3.0.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 3.0.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: sqlite3
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 1.3.8
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 1.3.8
97
+ description: A Rails engine that runs activities related to a duty in a specific order,
98
+ via Sidekiq.
99
+ email:
100
+ - pat@freelancing-gods.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Gemfile
107
+ - LICENSE.txt
108
+ - README.md
109
+ - Rakefile
110
+ - app/models/duties/activity_record.rb
111
+ - app/models/duties/duty_record.rb
112
+ - config.ru
113
+ - db/migrate/1_create_duties_and_activities.rb
114
+ - duties.gemspec
115
+ - lib/duties.rb
116
+ - lib/duties/activity.rb
117
+ - lib/duties/activity_job.rb
118
+ - lib/duties/duty.rb
119
+ - lib/duties/duty_job.rb
120
+ - lib/duties/engine.rb
121
+ - lib/duties/next.rb
122
+ - spec/acceptance/running_duties_spec.rb
123
+ - spec/internal/config/database.yml
124
+ - spec/internal/config/routes.rb
125
+ - spec/internal/db/combustion_test.sqlite
126
+ - spec/internal/db/schema.rb
127
+ - spec/internal/log/.gitignore
128
+ - spec/internal/public/favicon.ico
129
+ - spec/spec_helper.rb
130
+ homepage: https://github.com/inspire9/duties
131
+ licenses:
132
+ - MIT
133
+ metadata: {}
134
+ post_install_message:
135
+ rdoc_options: []
136
+ require_paths:
137
+ - lib
138
+ required_ruby_version: !ruby/object:Gem::Requirement
139
+ requirements:
140
+ - - ">="
141
+ - !ruby/object:Gem::Version
142
+ version: '0'
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ requirements: []
149
+ rubyforge_project:
150
+ rubygems_version: 2.3.0
151
+ signing_key:
152
+ specification_version: 4
153
+ summary: Run activities related to a duty in a specific order
154
+ test_files:
155
+ - spec/acceptance/running_duties_spec.rb
156
+ - spec/internal/config/database.yml
157
+ - spec/internal/config/routes.rb
158
+ - spec/internal/db/combustion_test.sqlite
159
+ - spec/internal/db/schema.rb
160
+ - spec/internal/log/.gitignore
161
+ - spec/internal/public/favicon.ico
162
+ - spec/spec_helper.rb