libis-workflow-mongoid 2.0.beta.13-java

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: 8f7cd89f49cbe129eb23fb80e198faa04fb78fd6
4
+ data.tar.gz: da74e5eadbcda157fe4cb3643806398a35971a4a
5
+ SHA512:
6
+ metadata.gz: 18456535f45deb883997f265b568ed638b9d0c1f0354dda2659664769137abf90f49704f8cd88b22aca9511357410ccb46323f24582abf29eb6e5e10db200311
7
+ data.tar.gz: 78c5fe4f27a5a140baf5bd0b5fb6965e5807a176dff500790e92975b03853153012b9699a8458774ad64dbe7e582dd38a5feaf64ad948fcc830c56d234f4e42e
data/.gitignore ADDED
@@ -0,0 +1,38 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /test/tmp/
9
+ /test/version_tmp/
10
+ /tmp/
11
+
12
+ ## Specific to RubyMotion:
13
+ .dat*
14
+ .repl_history
15
+ build/
16
+
17
+ ## Documentation cache and generated files:
18
+ /.yardoc/
19
+ /_yardoc/
20
+ /doc/
21
+ /rdoc/
22
+
23
+ ## Environment normalisation:
24
+ /.bundle/
25
+ /lib/bundler/man/
26
+
27
+ # for a library or gem, you might want to ignore these files since the code is
28
+ # intended to run in multiple environments; otherwise, check them in:
29
+ Gemfile.lock
30
+ .ruby-version
31
+ .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
35
+
36
+ .idea
37
+ projectFilesBackup
38
+ spec/data
@@ -0,0 +1,31 @@
1
+ db = db.getSiblingDB("admin");
2
+
3
+ db.createUser(
4
+ {
5
+ "user" : "useradmin",
6
+ "pwd" : "useradmin",
7
+ "roles" : [
8
+ {
9
+ "role" : "userAdminAnyDatabase",
10
+ "db" : "admin"
11
+ }
12
+ ]
13
+ }
14
+ );
15
+
16
+ db.auth("useradmin", "useradmin");
17
+
18
+ db = db.getSiblingDB("workflow_test");
19
+
20
+ db.createUser(
21
+ {
22
+ "user" : "test",
23
+ "pwd" : "abc123",
24
+ "roles" : [
25
+ {
26
+ "role" : "readWriteAnyDatabase",
27
+ "db" : "admin"
28
+ }
29
+ ]
30
+ }
31
+ );
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ OLD_DIR=`pwd`
3
+ cd "$( dirname "${BASH_SOURCE[0]}" )" > /dev/null
4
+ # Creates not-authorized problems on MongoDB 3.0.2 on my machine
5
+ # Using authorization-less code for now. To be investigated later ...
6
+ # mongo create_users.js
7
+ cd "${OLD_DIR}"
data/.travis.yml ADDED
@@ -0,0 +1,45 @@
1
+ language: ruby
2
+ sudo: false
3
+ bundler_args: --without development
4
+ cache: bundler
5
+
6
+ rvm:
7
+ - 2.1.7
8
+ - 2.2.3
9
+ - ruby-head
10
+ - jruby-9.0.1.0
11
+ jdk:
12
+ - openjdk7
13
+ - oraclejdk7
14
+ - oraclejdk8
15
+ matrix:
16
+ exclude:
17
+ - rvm: 2.1.7
18
+ jdk: oraclejdk7
19
+ - rvm: 2.1.7
20
+ jdk: oraclejdk8
21
+ - rvm: 2.2.3
22
+ jdk: oraclejdk7
23
+ - rvm: 2.2.3
24
+ jdk: oraclejdk8
25
+ - rvm: ruby-head
26
+ jdk: oraclejdk7
27
+ - rvm: ruby-head
28
+ jdk: oraclejdk8
29
+ allow_failures:
30
+ - rvm: ruby-head
31
+ branches:
32
+ only:
33
+ - master
34
+ env:
35
+ global:
36
+ - MONGODB_VERSION=3.2.0
37
+ - JRUBY_OPTS="-Xcli.debug=true --debug"
38
+ before_install:
39
+ - wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-$MONGODB_VERSION.tgz
40
+ - tar xfz mongodb-linux-x86_64-$MONGODB_VERSION.tgz
41
+ - export PATH=`pwd`/mongodb-linux-x86_64-$MONGODB_VERSION/bin:$PATH
42
+ - mkdir -p data/db
43
+ - mongod --dbpath=data/db &> /dev/null &
44
+ - sleep 3
45
+ before_script: ./.travis/db_prepare.sh
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec name: 'libis-workflow-mongoid', development_group: :test
4
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 LIBIS
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,8 @@
1
+
2
+ [![Build Status](https://travis-ci.org/libis/workflow-mongoid.svg?branch=master)](https://travis-ci.org/libis/workflow-mongoid)
3
+ [![Coverage Status](https://coveralls.io/repos/libis/workflow-mongoid/badge.png)](https://coveralls.io/r/libis/workflow-monoid)
4
+
5
+ workflow-mongoid
6
+ ================
7
+
8
+ Mongoid persistence for the workflow framework
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new('spec')
5
+
6
+ desc 'run tests'
7
+ task :default => :spec
@@ -0,0 +1,58 @@
1
+ # encoding: utf-8
2
+ require 'mongoid'
3
+ require 'mongoid/document'
4
+ # require 'mongoid_indifferent_access'
5
+ require_relative 'sequence'
6
+
7
+ require 'active_support/core_ext/object/deep_dup'
8
+
9
+ module Libis
10
+ module Workflow
11
+ module Mongoid
12
+
13
+ module Base
14
+
15
+ def self.included(klass)
16
+ klass.class_eval do
17
+ include ::Mongoid::Document
18
+ include ::Mongoid::Timestamps::Created::Short
19
+ # include ::Mongoid::Extensions::Hash::IndifferentAccess
20
+ include ::Libis::Workflow::Mongoid::Sequence
21
+ field :_id, type: Integer, overwrite: true
22
+ sequence :_id
23
+ index c_at: 1
24
+ end
25
+ end
26
+
27
+ def dup
28
+ new_obj = self.class.new
29
+ new_obj.copy_attributes(self)
30
+ end
31
+
32
+ def info
33
+ self.attributes.deep_dup.reject { |k,v| v.blank? || volatile_attributes.include?(k) }.to_hash
34
+ end
35
+
36
+ protected
37
+
38
+ def volatile_attributes
39
+ %w'_id c_at'
40
+ end
41
+ private
42
+
43
+ def copy_attributes(other)
44
+ self.set(
45
+ other.attributes.reject do |k, _|
46
+ %W(_id c_at).include? k.to_s
47
+ end.each_with_object({}) do |(k, v), h|
48
+ h[k] = v.duplicable? ? v.dup : v
49
+ end
50
+ )
51
+ self
52
+ end
53
+
54
+ end
55
+
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,27 @@
1
+ # encoding: utf-8
2
+ require 'singleton'
3
+ require 'mongoid'
4
+
5
+ require 'libis/workflow/config'
6
+ require 'libis/workflow/mongoid/run'
7
+
8
+ module Libis
9
+ module Workflow
10
+ module Mongoid
11
+
12
+ # noinspection RubyConstantNamingConvention
13
+ Config = ::Libis::Workflow::Config
14
+
15
+ Config.define_singleton_method(:database_connect) do |config_file = './mongoid.yml', environment = nil|
16
+ # noinspection RubyResolve
17
+ instance.database_connect(config_file, environment)
18
+ end
19
+
20
+ Config.send(:define_method, :database_connect) do |config_file = './mongoid.yml', environment = nil|
21
+ ::Mongoid.load! config_file, environment
22
+ ::Mongo::Logger.logger.level = Logger::ERROR
23
+ end
24
+
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ require 'mongoid'
2
+ require 'mongoid/document'
3
+
4
+ module Libis
5
+ module Workflow
6
+ module Mongoid
7
+ module Dynamic
8
+ def self.included(klass)
9
+ klass.class_eval do
10
+ include ::Mongoid::Document
11
+ include ::Mongoid::Attributes::Dynamic
12
+ include ::Libis::Workflow::Mongoid::Sequence
13
+ field :_id, type: Integer, overwrite: true
14
+ sequence :_id
15
+ index _id: 1
16
+
17
+ def has_key?(key)
18
+ self.attributes.has_key?(key.to_s) || self.attributes.has_key?(key.to_sym)
19
+ end
20
+
21
+ def each(&block)
22
+ self.attributes.reject { |k,_| k == '_id' }.each(&block)
23
+ end
24
+
25
+ end
26
+ end
27
+
28
+
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,47 @@
1
+ # encoding: utf-8
2
+
3
+ require 'libis/workflow/base/job'
4
+ require 'libis/workflow/mongoid/base'
5
+
6
+ module Libis
7
+ module Workflow
8
+ module Mongoid
9
+
10
+ module Job
11
+
12
+ def self.included(klass)
13
+ klass.class_eval do
14
+ include ::Libis::Workflow::Base::Job
15
+ include ::Libis::Workflow::Mongoid::Base
16
+
17
+ store_in collection: 'workflow_jobs'
18
+
19
+ field :name, type: String
20
+ field :description, type: String
21
+ field :input, type: Hash, default: -> { Hash.new }
22
+ field :run_object, type: String
23
+
24
+ index({name: 1}, {unique: 1})
25
+
26
+ def klass.run_class(run_klass)
27
+ has_many :runs, inverse_of: :job, class_name: run_klass.to_s,
28
+ dependent: :destroy, autosave: true, order: :c_at.asc
29
+ end
30
+
31
+ def klass.workflow_class(workflow_klass)
32
+ belongs_to :workflow, inverse_of: :jobs, class_name: workflow_klass.to_s
33
+ end
34
+
35
+ # def create_run_object
36
+ # # noinspection RubyResolve
37
+ # self.runs.build
38
+ # end
39
+
40
+ end
41
+
42
+ end
43
+
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: utf-8
2
+ require 'libis-workflow'
3
+ require 'libis/workflow/mongoid/base'
4
+
5
+ require 'mongoid/extensions/time_with_zone'
6
+
7
+ module Libis
8
+ module Workflow
9
+ module Mongoid
10
+
11
+ class LogEntry
12
+ include Libis::Workflow::Mongoid::Base
13
+
14
+ store_in collection: 'log'
15
+
16
+ field :severity, type: String
17
+ field :task, type: String, default: '*UNKNOWN*'
18
+ field :code, type: Integer
19
+ field :message, type: String
20
+ field :status, type: String
21
+
22
+ belongs_to :logger, polymorphic: true
23
+
24
+ end
25
+
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,72 @@
1
+ # encoding: utf-8
2
+ require 'fileutils'
3
+
4
+ require 'libis/workflow/base/run'
5
+ require 'libis/workflow/mongoid/work_item_base'
6
+
7
+ module Libis
8
+ module Workflow
9
+ module Mongoid
10
+
11
+ module Run
12
+ # extend ActiveSupport::Concern
13
+
14
+ def self.included(klass)
15
+ klass.class_eval do
16
+ include ::Libis::Workflow::Base::Run
17
+ include ::Libis::Workflow::Mongoid::WorkItemBase
18
+
19
+ store_in collection: 'workflow_runs'
20
+
21
+ field :start_date, type: Time, default: -> { Time.now }
22
+
23
+ set_callback(:destroy, :before) do |document|
24
+ document.items.each { |item| item.destroy }
25
+ wd = document.work_dir
26
+ FileUtils.rmtree wd if wd && !wd.blank? && Dir.exist?(wd)
27
+ end
28
+
29
+ index start_date: 1
30
+
31
+ def klass.job_class(job_klass)
32
+ belongs_to :job, inverse_of: :runs, class_name: job_klass.to_s
33
+ end
34
+
35
+ def klass.item_class(item_klass)
36
+ has_many :items, inverse_of: :run, class_name: item_klass.to_s,
37
+ dependent: :destroy, autosave: true, order: :c_at.asc
38
+ end
39
+ end
40
+ end
41
+
42
+ def run
43
+ self.tasks = []
44
+ self.items = []
45
+ # noinspection RubySuperCallWithoutSuperclassInspection
46
+ super
47
+ end
48
+
49
+ # Add a child work item
50
+ #
51
+ # @param [Libis::Workflow::Mongoid::WorkItem] item to be added to the child list :items
52
+ def add_item(item)
53
+ # noinspection RubyResolve
54
+ item.run = self
55
+ super
56
+ end
57
+
58
+ alias_method :<<, :add_item
59
+
60
+ def parent
61
+ nil
62
+ end
63
+
64
+ def parent=(_)
65
+ nil
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,79 @@
1
+ #require 'mongoid/fields/validators/macro'
2
+
3
+ module Libis
4
+ module Workflow
5
+ module Mongoid
6
+
7
+ # Include this module to add automatic sequence feature (also works for _id field, so SQL-Like autoincrement primary key can easily be simulated)
8
+ # usage:
9
+ # class KlassName
10
+ # include Mongoid::Document
11
+ # include LIBIS::Mongoid::Sequence
12
+ # ...
13
+ # field :number, :type=>Integer
14
+ # sequence :number
15
+ # ...
16
+ # Note: only tested on Mongoid 3.x and 4.x
17
+ module Sequence
18
+ extend ActiveSupport::Concern
19
+
20
+ #noinspection ALL
21
+ module ClassMethods
22
+
23
+ def sequence(_field, prefix = nil)
24
+ # REPLACE FIELD DEFAULT VALUE
25
+ _field = _field.to_s
26
+ options = fields[_field].options.merge(
27
+ default: lambda {
28
+ self.class.set_from_sequence(_field, prefix)
29
+ },
30
+ pre_processed: false,
31
+ overwrite: true
32
+ )
33
+ (options.keys - ::Mongoid::Fields::Validators::Macro::OPTIONS).each { |key| options.delete key }
34
+ field(_field, options)
35
+ end
36
+
37
+ def set_from_sequence(_field, prefix)
38
+ # Increase the sequence value and also avoids conflicts
39
+ catch(:value) do
40
+ value = nil
41
+ begin
42
+ value = seq_upsert(counter_id(_field), {'$inc' => {'value' => 1}}).send('[]', 'value')
43
+ value = "#{prefix.is_a?(Proc) ? instance_eval(prefix.call) : prefix}_#{value}" if prefix
44
+ end until self.where(_field => value).count == 0
45
+ throw :value, value
46
+ end
47
+ end
48
+
49
+ def reset_sequence(_field, value = 0)
50
+ seq_upsert(counter_id(_field), {'$set' => {'value' => value}})
51
+ end
52
+
53
+ protected
54
+
55
+ def counter_id(_field)
56
+ #"#{self.name.underscore}##{_field}"
57
+ "#{collection_name.to_s}##{_field}"
58
+ end
59
+
60
+ def sequences
61
+ # mongo_session["#{self.collection_name.to_s}__seq"]
62
+ mongo_client["__sequences__"]
63
+ end
64
+
65
+ def seq_upsert(counter_id, change)
66
+ sequences.find_one_and_update({_id: counter_id},
67
+ change,
68
+ upsert: true,
69
+ return_document: :after
70
+ )
71
+ end
72
+
73
+ end
74
+
75
+ end
76
+
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,9 @@
1
+ # encoding: utf-8
2
+
3
+ module Libis
4
+ module Workflow
5
+ module Mongoid
6
+ VERSION = '2.0.beta.13' unless const_defined? :VERSION # the guard is against a redefinition warning that happens on Travis
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: utf-8
2
+ require 'libis/workflow/mongoid/work_item_base'
3
+
4
+ module Libis
5
+ module Workflow
6
+ module Mongoid
7
+
8
+ module WorkItem
9
+
10
+ def self.included(klass)
11
+ klass.class_eval do
12
+ include Libis::Workflow::Mongoid::WorkItemBase
13
+
14
+ store_in collection: 'workflow_items'
15
+
16
+ has_many :items, inverse_of: :parent, class_name: klass.to_s,
17
+ dependent: :destroy, autosave: true, order: :_id.asc
18
+ belongs_to :parent, inverse_of: :items, class_name: klass.to_s
19
+
20
+ set_callback(:destroy, :before) do |document|
21
+ document.items.each { |item| item.destroy! }
22
+ end
23
+
24
+ def klass.run_class(run_klass)
25
+ belongs_to :run, inverse_of: :items, class_name: run_klass.to_s
26
+ end
27
+
28
+ end
29
+ end
30
+
31
+ def get_parent
32
+ self.parent || self.run
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,107 @@
1
+ # encoding: utf-8
2
+ require 'libis-workflow'
3
+ require_relative 'dynamic'
4
+
5
+ module Libis
6
+ module Workflow
7
+ module Mongoid
8
+
9
+ module WorkItemBase
10
+
11
+ class Options
12
+ include Libis::Workflow::Mongoid::Dynamic
13
+ embedded_in :work_item, class_name: Libis::Workflow::Mongoid::WorkItemBase.to_s
14
+ end
15
+
16
+ class Properties
17
+ include Libis::Workflow::Mongoid::Dynamic
18
+ embedded_in :work_item, class_name: Libis::Workflow::Mongoid::WorkItemBase.to_s
19
+ end
20
+
21
+ class Summary
22
+ include Libis::Workflow::Mongoid::Dynamic
23
+ embedded_in :work_item, class_name: Libis::Workflow::Mongoid::WorkItemBase.to_s
24
+ end
25
+
26
+ def self.included(klass)
27
+ klass.class_eval do
28
+ include Libis::Workflow::Base::WorkItem
29
+ include Libis::Workflow::Mongoid::Base
30
+
31
+ embeds_one :options, class_name: Libis::Workflow::Mongoid::WorkItemBase::Options.to_s
32
+ embeds_one :properties, class_name: Libis::Workflow::Mongoid::WorkItemBase::Properties.to_s
33
+ embeds_one :summary, class_name: Libis::Workflow::Mongoid::WorkItemBase::Summary.to_s
34
+
35
+ has_many :logs, as: :logger, class_name: Libis::Workflow::Mongoid::LogEntry.to_s,
36
+ dependent: :destroy, autosave: true, order: :_id.asc do
37
+ def log_history
38
+ where(:status.exists => false)
39
+ end
40
+
41
+ def status_log
42
+ where(:status.exists => true)
43
+ end
44
+ end
45
+
46
+ # def destroy
47
+ # # noinspection RubyResolve
48
+ # self.logs.each { |log| log.destroy }
49
+ # end
50
+
51
+ set_callback(:destroy, :before) do |document|
52
+ # noinspection RubyResolve
53
+ document.logs.each { |log| log.destroy! }
54
+ end
55
+
56
+ set_callback(:initialize, :after) do |document|
57
+ document.options = {}
58
+ document.properties = {}
59
+ document.summary = {}
60
+ end
61
+
62
+ end
63
+
64
+ end
65
+
66
+ def each
67
+ self.items.each { |item| yield item }
68
+ end
69
+
70
+ def get_items
71
+ self.items.to_a
72
+ end
73
+
74
+ def item_count
75
+ self.items.count
76
+ end
77
+
78
+ alias_method :count, :item_count
79
+ alias_method :size, :item_count
80
+
81
+ def log_history
82
+ # noinspection RubyResolve
83
+ self.logs.log_history.all || []
84
+ end
85
+
86
+ def status_log
87
+ # noinspection RubyResolve
88
+ self.logs.status_log.all || []
89
+ end
90
+
91
+ protected
92
+
93
+ def add_log_entry(msg)
94
+ # noinspection RubyResolve
95
+ self.logs.build(msg)
96
+ end
97
+
98
+ def add_status_log(info)
99
+ # noinspection RubyResolve
100
+ self.logs.build(info)
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+ end
107
+ end