abid 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ea0a29c738c86aebaf241507a7b0acab89dc3cf7
4
+ data.tar.gz: c708d6813dae9bd21cd8e1c2d4396d3a055dc50f
5
+ SHA512:
6
+ metadata.gz: 845024d893bf17650b189eabb90280840827f3e03910fb61e76c35e0446bceb9755ce8d79c46397aa0245ac26c54ecbc6c78bc98172594a2304bf850e35ff25d
7
+ data.tar.gz: 3e18c50195b334d8bebb1137ff45417198ae57466a3ef50aad14d630c23467bb423572047e9f1647f16ae3a14e9245fae9764479b4cb6bdf5c43002c2178485d
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.db
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
4
+ before_install: gem install bundler -v 1.10.6
@@ -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, ethnicity, 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 abid.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Hikaru Ojima
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.
@@ -0,0 +1,89 @@
1
+ # Abid
2
+
3
+ Abid is a simple Workflow Engine based on Rake.
4
+
5
+ ## Installation
6
+
7
+ 1. Install abid gem.
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'abid'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install abid
22
+
23
+ After installed execute:
24
+
25
+ 2. Setup a database.
26
+
27
+ $ bundle exec abid db:migrate
28
+
29
+ ## Usage
30
+
31
+ Abid is an extention of rake, so you can use any rake syntax.
32
+
33
+ First, you must write a “Abidfile” file which contains the tasks:
34
+
35
+ ```ruby
36
+ require 'open-uri'
37
+
38
+ task default: 'count'
39
+
40
+ play :fetch_source do
41
+ param :date, type: :date
42
+
43
+ def run
44
+ mkdir 'out'
45
+ open('http://example.com') do |f|
46
+ File.write("out/#{date.strftime('%Y-%m-%d')}/example.com", f.read)
47
+ end
48
+ end
49
+ end
50
+
51
+ play :count do
52
+ param :date, type: :date
53
+
54
+ def setup
55
+ needs 'fetch_source', date: date
56
+ end
57
+
58
+ def run
59
+ puts File.read("out/#{date.strftime('%Y-%m-%d')}/example.com").lines.length
60
+ end
61
+ end
62
+ ```
63
+
64
+ Then you can invoke the task:
65
+
66
+ ```
67
+ $ bundle exec abid count date=2016-01-01
68
+ ```
69
+
70
+ This Abidfile has two tasks: `fetch_source` and `count`. They can be treated as a normal rake task, but they have some additional features:
71
+
72
+ * A play can take parameters. They are declared with `param` keyword, and passed via environment variables.
73
+ * All play results are saved to the external database. If a play is invoked twice with same parameters, it will be ignored.
74
+ * Depending tasks can be declared in `setup` method. If a depending task is a play task, parameters can be passed.
75
+
76
+ ## Development
77
+
78
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
79
+
80
+ 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 tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
81
+
82
+ ## Contributing
83
+
84
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ojima-h/abid. 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.
85
+
86
+
87
+ ## License
88
+
89
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,32 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << 'test'
6
+ t.libs << 'lib'
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
11
+
12
+ namespace :db do
13
+ desc 'Run migrations'
14
+ task :migrate, [:version] do |_t, args|
15
+ require 'sqlite3'
16
+ require 'sequel'
17
+
18
+ database_url = 'sqlite://' + File.expand_path('../tmp/abid.db', __FILE__)
19
+ migrations_path = File.expand_path('../migrations', __FILE__)
20
+
21
+ require 'sequel'
22
+ Sequel.extension :migration
23
+ db = Sequel.connect(database_url)
24
+ if args[:version]
25
+ puts "Migrating to version #{args[:version]}"
26
+ Sequel::Migrator.run(db, migrations_path, target: args[:version].to_i)
27
+ else
28
+ puts 'Migrating to latest'
29
+ Sequel::Migrator.run(db, migrations_path)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'abid/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "abid"
8
+ spec.version = Abid::VERSION
9
+ spec.authors = ["Hikaru Ojima"]
10
+ spec.email = ["amijo4rihaku@gmail.com"]
11
+
12
+ spec.summary = "Abid is a simple Workflow Engine based on Rake."
13
+ spec.description = "Abid is a simple Workflow Engine based on Rake."
14
+ spec.homepage = "https://github.com/ojima-h/abid"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|sample)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "minitest"
24
+
25
+ spec.add_dependency "rake", "~> 10.0"
26
+ spec.add_dependency "concurrent-ruby-ext"
27
+ spec.add_dependency "inifile"
28
+ spec.add_dependency "sequel"
29
+ spec.add_dependency "sqlite3"
30
+ spec.add_dependency "rbtree"
31
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "abid"
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
@@ -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,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'abid'
3
+
4
+ Abid::Application.new.run
@@ -0,0 +1,80 @@
1
+ namespace :state do
2
+ task default: :list
3
+
4
+ desc 'Show play histories'
5
+ play :list do
6
+ set :volatile, true
7
+
8
+ param :started_after, type: :time, default: nil
9
+ param :started_before, type: :time, default: nil
10
+
11
+ def run
12
+ states = Abid::State.list(
13
+ started_after: started_after,
14
+ started_before: started_before
15
+ )
16
+
17
+ table = states.map do |state|
18
+ params = state[:params].map do |k, v|
19
+ v.to_s =~ /\s/ ? "#{k}='#{v}'" : "#{k}=#{v}"
20
+ end.join(' ')
21
+ [
22
+ state[:id].to_s,
23
+ state[:state].to_s,
24
+ state[:name],
25
+ params,
26
+ state[:start_time].to_s,
27
+ state[:end_time].to_s
28
+ ]
29
+ end
30
+
31
+ header = %w(id state name params start_time end_time)
32
+
33
+ tab_width = header.each_with_index.map do |c, i|
34
+ [c.length, table.map { |row| row[i].length }.max || 0].max
35
+ end
36
+
37
+ header.each_with_index do |c, i|
38
+ print c.ljust(tab_width[i] + 2)
39
+ end
40
+ puts
41
+
42
+ header.each_with_index do |_, i|
43
+ print '-' * (tab_width[i] + 2)
44
+ end
45
+ puts
46
+
47
+ table.map do |row|
48
+ row.each_with_index do |v, i|
49
+ print v.ljust(tab_width[i] + 2)
50
+ end
51
+ puts
52
+ end
53
+ end
54
+ end
55
+ end
56
+
57
+ namespace :db do
58
+ desc 'Delete play history'
59
+ task :revoke, [:task] do |_t, args|
60
+ task = Rake.application[args[:task]]
61
+ state = Abid::State.find(task)
62
+ state.revoke
63
+ end
64
+
65
+ desc 'Run migrations'
66
+ task :migrate, [:version] do |_t, args|
67
+ migrations_path = File.expand_path('../../migrations', __FILE__)
68
+
69
+ require 'sequel'
70
+ Sequel.extension :migration
71
+ db = Rake.application.database
72
+ if args[:version]
73
+ puts "Migrating to version #{args[:version]}"
74
+ Sequel::Migrator.run(db, migrations_path, target: args[:version].to_i)
75
+ else
76
+ puts 'Migrating to latest'
77
+ Sequel::Migrator.run(db, migrations_path)
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,28 @@
1
+ require 'rake'
2
+ require 'date'
3
+ require 'digest/md5'
4
+ require 'english'
5
+ require 'forwardable'
6
+ require 'time'
7
+ require 'yaml'
8
+ require 'concurrent'
9
+ require 'inifile'
10
+ require 'rbtree'
11
+ require 'sqlite3'
12
+ require 'sequel'
13
+
14
+ require 'abid/rake_extensions'
15
+ require 'abid/version'
16
+ require 'abid/waiter'
17
+ require 'abid/worker'
18
+ require 'abid/params_parser'
19
+ require 'abid/play'
20
+ require 'abid/state'
21
+ require 'abid/task'
22
+ require 'abid/task_manager'
23
+ require 'abid/dsl_definition'
24
+ require 'abid/application'
25
+
26
+ module Abid
27
+ FIXNUM_MAX = (2**(0.size * 8 - 2) - 1) # :nodoc:
28
+ end
@@ -0,0 +1,141 @@
1
+ module Abid
2
+ class Application < Rake::Application
3
+ include Abid::TaskManager
4
+
5
+ attr_reader :worker
6
+ attr_reader :config
7
+ attr_reader :futures
8
+
9
+ def initialize
10
+ super
11
+ @rakefiles = %w(abidfile Abidfile abidfile.rb Abidfile.rb) << abidfile
12
+ @futures = {}
13
+ @worker = Worker.new(self)
14
+ end
15
+
16
+ def run
17
+ Rake.application = self
18
+ super
19
+ end
20
+
21
+ def init(app_name = 'abid')
22
+ standard_exception_handling do
23
+ @config = IniFile.new(content: default_config)
24
+ @config.merge!(IniFile.load('config/abid.cfg'))
25
+ end
26
+
27
+ super(app_name)
28
+ end
29
+
30
+ # allows the `abid db:migrate` task to load without a abidfile
31
+ def abidfile
32
+ File.expand_path(File.join(File.dirname(__FILE__), '..', 'Abidfile.rb'))
33
+ end
34
+
35
+ def run_with_threads
36
+ yield
37
+ ensure
38
+ worker.shutdown
39
+ end
40
+
41
+ def invoke_task(task_string) # :nodoc:
42
+ name, args = parse_task_string(task_string)
43
+ self[name].async_invoke(*args).value!
44
+ end
45
+
46
+ def default_config
47
+ {}
48
+ end
49
+
50
+ def default_database_config
51
+ {
52
+ adapter: 'sqlite',
53
+ database: File.join(Dir.pwd, 'abid.db'),
54
+ max_connections: 1
55
+ }
56
+ end
57
+
58
+ def standard_rake_options
59
+ super.each do |opt|
60
+ case opt.first
61
+ when '--execute-print'
62
+ # disable short option
63
+ opt.delete_at(1)
64
+ when '--dry-run'
65
+ h = opt.last
66
+ opt[-1] = lambda do |value|
67
+ h.call(value)
68
+ options.disable_state = true
69
+ end
70
+ when '--version'
71
+ opt[-1] = lambda do |_value|
72
+ puts "Abid Version: #{Abid::VERSION} (Rake Version: #{RAKEVERSION})"
73
+ exit
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ def abid_options # :nodoc:
80
+ sort_options(
81
+ [
82
+ ['--check-parents', '-c',
83
+ 'Run the task if the parents was updated.',
84
+ proc { options.check_prerequisites = true }
85
+ ],
86
+ ['--preview', '-p',
87
+ 'Run tasks in preview mode.',
88
+ proc do
89
+ options.disable_state = true
90
+ options.preview = true
91
+ end
92
+ ],
93
+ ['--wait-external-task',
94
+ 'Wait a task finished if it is running in externl process',
95
+ proc do
96
+ options.wait_external_task_interval = true
97
+ end
98
+ ]
99
+ ]
100
+ )
101
+ end
102
+
103
+ def handle_options
104
+ options.rakelib = ['rakelib']
105
+ options.trace_output = $stderr
106
+
107
+ OptionParser.new do |opts|
108
+ opts.banner = 'See full documentation at https://github.com/ojima-h/abid.'
109
+ opts.separator ''
110
+ opts.separator 'Show available tasks:'
111
+ opts.separator ' bundle exec abid -T'
112
+ opts.separator ''
113
+ opts.separator 'Invoke (or simulate invoking) a task:'
114
+ opts.separator ' bundle exec abid [--dry-run | --preview] TASK'
115
+ opts.separator ''
116
+ opts.separator 'Abid options:'
117
+ abid_options.each { |args| opts.on(*args) }
118
+ opts.separator ''
119
+ opts.separator 'Advanced options:'
120
+ standard_rake_options.each { |args| opts.on(*args) }
121
+
122
+ opts.on_tail('-h', '--help', '-H', 'Display this help message.') do
123
+ puts opts
124
+ exit
125
+ end
126
+
127
+ opts.environment('RAKEOPT')
128
+ end.parse!
129
+ end
130
+
131
+ def database
132
+ return @database if @database
133
+ if config.sections.include?('abid database')
134
+ cfg = config['abid database'].map { |k, v| [k.to_sym, v] }.to_h
135
+ else
136
+ cfg = default_database_config
137
+ end
138
+ @database = Sequel.connect(**cfg)
139
+ end
140
+ end
141
+ end