qyu 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +56 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +5 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE +21 -0
  8. data/README.md +90 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +14 -0
  11. data/bin/server +17 -0
  12. data/bin/setup +8 -0
  13. data/examples/bin/simple +7 -0
  14. data/examples/config.rb +22 -0
  15. data/examples/simple/create_workflow.rb +18 -0
  16. data/examples/simple/enqueue_job.rb +8 -0
  17. data/examples/simple/worker.rb +32 -0
  18. data/lib/qyu.rb +74 -0
  19. data/lib/qyu/config.rb +35 -0
  20. data/lib/qyu/errors.rb +4 -0
  21. data/lib/qyu/errors/base.rb +8 -0
  22. data/lib/qyu/errors/could_not_fetch_task.rb +18 -0
  23. data/lib/qyu/errors/invalid_queue_name.rb +12 -0
  24. data/lib/qyu/errors/invalid_task_attributes.rb +12 -0
  25. data/lib/qyu/errors/job_not_found.rb +14 -0
  26. data/lib/qyu/errors/lock_already_acquired.rb +12 -0
  27. data/lib/qyu/errors/lock_not_acquired.rb +12 -0
  28. data/lib/qyu/errors/message_not_received.rb +12 -0
  29. data/lib/qyu/errors/not_implemented_error.rb +12 -0
  30. data/lib/qyu/errors/payload_validation_error.rb +12 -0
  31. data/lib/qyu/errors/task_not_found.rb +15 -0
  32. data/lib/qyu/errors/task_status_update_failed.rb +15 -0
  33. data/lib/qyu/errors/unknown_validation_option.rb +12 -0
  34. data/lib/qyu/errors/unsync_error.rb +12 -0
  35. data/lib/qyu/errors/workflow_descriptor_validation_error.rb +14 -0
  36. data/lib/qyu/errors/workflow_not_found.rb +15 -0
  37. data/lib/qyu/factory.rb +26 -0
  38. data/lib/qyu/models.rb +9 -0
  39. data/lib/qyu/models/concerns/workflow_descriptor_validator.rb +117 -0
  40. data/lib/qyu/models/enums/status.rb +44 -0
  41. data/lib/qyu/models/job.rb +174 -0
  42. data/lib/qyu/models/task.rb +218 -0
  43. data/lib/qyu/models/workflow.rb +85 -0
  44. data/lib/qyu/queue.rb +5 -0
  45. data/lib/qyu/queue/base.rb +46 -0
  46. data/lib/qyu/queue/memory/adapter.rb +90 -0
  47. data/lib/qyu/store.rb +5 -0
  48. data/lib/qyu/store/base.rb +106 -0
  49. data/lib/qyu/store/memory/adapter.rb +187 -0
  50. data/lib/qyu/ui.rb +56 -0
  51. data/lib/qyu/ui/helpers/pagination.rb +35 -0
  52. data/lib/qyu/ui/public/bootstrap.min.css +5 -0
  53. data/lib/qyu/ui/public/paper-dashboard.css +3315 -0
  54. data/lib/qyu/ui/public/script.js +28 -0
  55. data/lib/qyu/ui/public/style.css +6 -0
  56. data/lib/qyu/ui/views/footer.erb +18 -0
  57. data/lib/qyu/ui/views/helpers/pagination.erb +49 -0
  58. data/lib/qyu/ui/views/jobs.erb +58 -0
  59. data/lib/qyu/ui/views/kaminari/_first_page.html.erb +3 -0
  60. data/lib/qyu/ui/views/kaminari/_gap.html.erb +3 -0
  61. data/lib/qyu/ui/views/kaminari/_last_page.html.erb +3 -0
  62. data/lib/qyu/ui/views/kaminari/_next_page.html.erb +3 -0
  63. data/lib/qyu/ui/views/kaminari/_page.html.erb +9 -0
  64. data/lib/qyu/ui/views/kaminari/_paginator.html.erb +15 -0
  65. data/lib/qyu/ui/views/kaminari/_prev_page.html.erb +3 -0
  66. data/lib/qyu/ui/views/layout.erb +33 -0
  67. data/lib/qyu/ui/views/navbar.erb +29 -0
  68. data/lib/qyu/ui/views/pagination.erb +19 -0
  69. data/lib/qyu/ui/views/show_job.erb +55 -0
  70. data/lib/qyu/ui/views/sidebar.erb +17 -0
  71. data/lib/qyu/ui/views/task_row.erb +26 -0
  72. data/lib/qyu/utils.rb +17 -0
  73. data/lib/qyu/version.rb +3 -0
  74. data/lib/qyu/workers.rb +10 -0
  75. data/lib/qyu/workers/base.rb +126 -0
  76. data/lib/qyu/workers/concerns/callback.rb +38 -0
  77. data/lib/qyu/workers/concerns/failure_queue.rb +23 -0
  78. data/lib/qyu/workers/concerns/payload_validator.rb +124 -0
  79. data/lib/qyu/workers/sync.rb +63 -0
  80. data/qyu.gemspec +36 -0
  81. metadata +278 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d9b20d25c456278bced692a847ebacc7f6d47793b84dd4aea02df255ddb441b3
4
+ data.tar.gz: 00006250c1b37a994c7cbe344fe786a7487c35495538d661506cac37e2e3eba1
5
+ SHA512:
6
+ metadata.gz: a9752fc4799da6be2ce17a797a7621019fe4fc39da356f39861fbbf069f3785708ba702030ab8c4c3c58ac08398b7e66086da278031bcbc7ef2fde90850a0066
7
+ data.tar.gz: 1234718f10d09022557ffb1b939a4976b43ea51e1ecf941adb981f658d61c1427edffd8582e47fdd8bf94c220df07c1754b24f80f9cfa4c003015bf37054040a
@@ -0,0 +1,56 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ ## Specific to RubyMotion:
17
+ .dat*
18
+ .repl_history
19
+ build/
20
+ *.bridgesupport
21
+ build-iPhoneOS/
22
+ build-iPhoneSimulator/
23
+
24
+ ## Specific to RubyMotion (use of CocoaPods):
25
+ #
26
+ # We recommend against adding the Pods directory to your .gitignore. However
27
+ # you should judge for yourself, the pros and cons are mentioned at:
28
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
29
+ #
30
+ # vendor/Pods/
31
+
32
+ ## Documentation cache and generated files:
33
+ /.yardoc/
34
+ /_yardoc/
35
+ /doc/
36
+ /rdoc/
37
+
38
+ ## Environment normalization:
39
+ /.bundle/
40
+ /vendor/bundle
41
+ /lib/bundler/man/
42
+
43
+ # for a library or gem, you might want to ignore these files since the code is
44
+ # intended to run in multiple environments; otherwise, check them in:
45
+ Gemfile.lock
46
+ .ruby-version
47
+ .ruby-gemset
48
+
49
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
50
+ .rvmrc
51
+
52
+ # rspec failure tracking
53
+ .rspec_status
54
+
55
+ # byebug history
56
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.4.2
5
+ before_install: gem install bundler -v 1.16.0
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at oss@findhotel.net. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in qyu.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 FindHotel B.V.
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.
@@ -0,0 +1,90 @@
1
+ # Qyu 九
2
+
3
+ [![Gem Version](https://img.shields.io/gem/v/qyu.svg)](https://rubygems.org/gems/qyu)
4
+ [![Build Status](https://travis-ci.org/FindHotel/qyu.svg)](https://travis-ci.org/FindHotel/qyu)
5
+
6
+ ## Requirements:
7
+ * Ruby 2.4.0 or newer
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'qyu'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install qyu
24
+
25
+ ## Configuration
26
+ To start using Qyu; you need a queue configuration and a state store configuration. Here's an example:
27
+ ```ruby
28
+ Qyu.configure(
29
+ queue: {
30
+ type: :memory
31
+ },
32
+ store: {
33
+ type: :memory,
34
+ lease_period: 60
35
+ },
36
+ # optional Defaults to STDOUT
37
+ logger: Logger.new(STDOUT)
38
+ )
39
+ ```
40
+
41
+ ## Usage
42
+ TODO: Write usage instructions here
43
+
44
+ ## Plugins
45
+ The memory queue and store is just for testing purposes. For production; use one of the following:
46
+
47
+ #### Stores
48
+ *ActiveRecord:* https://github.com/FindHotel/qyu-store-activerecord
49
+ *Redis:* https://github.com/FindHotel/qyu-store-redis
50
+
51
+ #### Queues
52
+ *Amazon SQS:* https://github.com/FindHotel/qyu-queue-sqs
53
+ *Redis:* https://github.com/FindHotel/qyu-queue-redis
54
+
55
+ ## Glossary
56
+
57
+ #### Workflow
58
+ The workflow specifies the entry points (`starts`), the tasks, their order, eventual dependencies between them, and synchronisation conditions.
59
+
60
+ #### Job
61
+ A job is essentially a collection of tasks and an initial JSON payload.
62
+
63
+ #### Task
64
+ A task is one unit of work. It is an instance of an entry from a workflow. You can think of it as the workflow's entries define the classes, while a task is a materialised instance of it, saved in the state store and enqueued on the message queue.
65
+
66
+ In the state store a task has:
67
+ * `id`
68
+ * `name` - it appears as the key in the workflow's `tasks`
69
+ * `queue_name` - the queue where the task was enqueued on creation
70
+ * `payload` - the entry/input parameters for the particular task
71
+ * `parent_task_id` - the ID of the task which created/enqueued the current task
72
+
73
+ When a task is created (saved & enqueued) then its `id` is put in a JSON message `{ task_id: task.id}` and enqueued on the specified task's message queue.
74
+ When a worker picks up the message from the queue, decodes the task id, and retrieves it from the state store.
75
+
76
+ #### Worker
77
+ A worker is sitting on a queue, waiting for something.
78
+
79
+ #### Sync Worker
80
+ A worker waiting for other workers to finish
81
+
82
+ ## Development
83
+
84
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
85
+
86
+ 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).
87
+
88
+ ## Contributing
89
+
90
+ Bug reports and pull requests are welcome on GitHub at https://github.com/FindHotel/qyu.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "qyu"
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(__FILE__)
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'qyu'
6
+ require 'qyu/ui'
7
+
8
+ queue_configuration = {
9
+ type: :memory
10
+ }
11
+
12
+ store_configuration = {
13
+ type: :memory
14
+ }
15
+
16
+ Qyu.config = Qyu::Config.new(queue: queue_configuration, store: store_configuration)
17
+ Qyu::UI.run!
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../simple/create_workflow'
4
+ require_relative '../simple/enqueue_job'
5
+ require_relative '../simple/worker'
6
+
7
+ SimpleWorker.new.run
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+ require 'bundler/setup'
5
+ Bundler.setup
6
+
7
+ require 'qyu'
8
+ logger = Logger.new(STDOUT)
9
+ logger.level = :info
10
+
11
+ Qyu.configure(
12
+ queue: {
13
+ type: :memory
14
+ },
15
+ store: {
16
+ type: :memory,
17
+ lease_period: 60
18
+ },
19
+ logger: logger
20
+ )
21
+
22
+ Qyu.test_connections
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../config'
4
+
5
+ descriptor = {
6
+ 'starts' => %w(
7
+ print_hello
8
+ ),
9
+ 'tasks' => {
10
+ 'print_hello' => {
11
+ 'queue' => 'print-hello'
12
+ }
13
+ }
14
+ }
15
+
16
+ name = 'say-hello'
17
+ Qyu.logger.info "Creating workflow #{name}"
18
+ Qyu::Workflow.create(name: name, descriptor: descriptor)
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../config'
4
+
5
+ payload = { 'times' => 5 }
6
+ Qyu.logger.info "Enqueuing job with payload #{payload}"
7
+ job = Qyu::Job.create(workflow: 'say-hello', payload: payload)
8
+ job.start
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../config'
4
+
5
+ class SimpleWorker
6
+ def initialize
7
+ @worker = Qyu::Worker.new do
8
+ callback :execute, :before do
9
+ Qyu.logger.info 'Waiting for task..'
10
+ end
11
+
12
+ callback :execute, :after do
13
+ Qyu.logger.info 'Done'
14
+ end
15
+
16
+ # Payload validation
17
+ validates :times, presence: true, type: :integer
18
+ end
19
+ end
20
+
21
+ def run
22
+ # Consumes message from print-hello queue
23
+ @worker.work('print-hello') do |task|
24
+ task.payload['times'].times do |i|
25
+ Qyu.logger.info "#{i + 1}. Hello"
26
+ end
27
+ rescue StandardError => ex
28
+ Qyu.logger.error 'OMG :('
29
+ Qyu.logger.error ex.message
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,74 @@
1
+ require 'active_support'
2
+ require 'json'
3
+ require 'logger'
4
+ require 'time'
5
+ require 'securerandom'
6
+ require 'qyu/version'
7
+ require 'qyu/config'
8
+ require 'qyu/factory'
9
+ require 'qyu/utils'
10
+ require 'qyu/errors'
11
+ require 'qyu/models'
12
+ require 'qyu/queue'
13
+ require 'qyu/store'
14
+ require 'qyu/workers'
15
+
16
+ module Qyu
17
+ class << self
18
+ def configure(queue:, store:, logger: nil)
19
+ self.config = Qyu::Config.new(
20
+ queue: queue,
21
+ store: store
22
+ )
23
+ self.logger = logger || default_logger unless defined?(@@__logger)
24
+ self.test_connections
25
+ end
26
+
27
+ def config
28
+ fail 'Undefined configuration' unless defined?(@@__config)
29
+
30
+ @@__config
31
+ end
32
+ alias configuration config
33
+
34
+ def configured?
35
+ defined?(@@__config)
36
+ end
37
+
38
+ def logger=(logger)
39
+ @@__logger = logger
40
+ end
41
+
42
+ def logger
43
+ @@__logger ||= default_logger
44
+ end
45
+
46
+ def queue
47
+ @@__queue ||= Qyu::Factory::QueueFactory.get(config.queue)
48
+ end
49
+
50
+ def store
51
+ @@__store ||= Qyu::Factory::StoreFactory.get(config.store)
52
+ end
53
+
54
+ def test_connections
55
+ queue
56
+ store
57
+ end
58
+
59
+ private
60
+
61
+ def config=(config)
62
+ fail 'Can not re-define configuration' if configured?
63
+ fail 'Invalid configuration' unless config.is_a?(Qyu::Config)
64
+
65
+ @@__config = config
66
+ end
67
+
68
+ def default_logger
69
+ logger = Logger.new(STDOUT)
70
+ logger.level = Logger::DEBUG
71
+ logger
72
+ end
73
+ end
74
+ end