octokiq 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 387dcbd87d4bf68d9cef986923fe654c1d62a1a13ed9288f1cc2c4c5edc7647d
4
+ data.tar.gz: ad3bf793c30fa11cb61bc59f5bf2479eb884030f3590a2650af671170214deb5
5
+ SHA512:
6
+ metadata.gz: 2b5ffccf25e4d3033c886dddaaaf4446bb62a6e29fe4312b8fa147931c9b0aafcbdd47fe15fdcf1621a03c4eb32c1bc15d070a8f5225d6ce6733f18c00fb4f6d
7
+ data.tar.gz: 795233440f34f446b6897615bac42923f5e1a848a10ae9ebcdfd131f626ac0c55f11a50ccf812de8fc8459460a61714f520a90f386488e045d62d6c45fdb9862
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /log/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
@@ -0,0 +1,5 @@
1
+ # Changelog
2
+
3
+ ## 0.1.0 - 2020-12-29
4
+
5
+ Initial release 🚀
@@ -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 bran.liang@shopify.com. 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 [https://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: https://contributor-covenant.org
74
+ [version]: https://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in octokiq.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
8
+ gem "pry-byebug"
@@ -0,0 +1,33 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ octokiq (0.1.0)
5
+ redis (~> 4.2)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ byebug (11.1.3)
11
+ coderay (1.1.3)
12
+ method_source (1.0.0)
13
+ minitest (5.14.2)
14
+ pry (0.13.1)
15
+ coderay (~> 1.1)
16
+ method_source (~> 1.0)
17
+ pry-byebug (3.9.0)
18
+ byebug (~> 11.0)
19
+ pry (~> 0.13.0)
20
+ rake (12.3.3)
21
+ redis (4.2.5)
22
+
23
+ PLATFORMS
24
+ x86_64-darwin-19
25
+
26
+ DEPENDENCIES
27
+ minitest (~> 5.0)
28
+ octokiq!
29
+ pry-byebug
30
+ rake (~> 12.0)
31
+
32
+ BUNDLED WITH
33
+ 2.2.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Bran Liang
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,44 @@
1
+ # Octokiq
2
+
3
+ Next generation background processing for ruby
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'octokiq'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install octokiq
20
+
21
+ ## Usage
22
+
23
+ Run worker server:
24
+
25
+ $ bundle exec octokiq
26
+
27
+ ## Development
28
+
29
+ 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.
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`, 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/BranLiang/octokiq. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/BranLiang/octokiq/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the Octokiq project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/BranLiang/octokiq/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'pry-byebug'
5
+ require 'octokiq'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
@@ -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,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'octokiq'
5
+
6
+ OptionParser.new do |opts|
7
+ opts.banner = 'Usage: bundle exec octokiq [options]'
8
+
9
+ opts.on '-r', '--require [PATH]', 'Location of file to require' do |arg|
10
+ require File.expand_path(arg)
11
+ end
12
+ end.parse!
13
+
14
+ cmd = Octokiq::CMD.new
15
+ cmd.run
@@ -0,0 +1,39 @@
1
+ require 'redis'
2
+
3
+ require 'etc'
4
+ require 'logger'
5
+ require 'json'
6
+ require 'optparse'
7
+
8
+ require 'octokiq/version'
9
+ require 'octokiq/configuration'
10
+ require 'octokiq/connection'
11
+ require 'octokiq/worker'
12
+ require 'octokiq/processor'
13
+ require 'octokiq/server'
14
+ require 'octokiq/cmd'
15
+
16
+ module Octokiq
17
+ extend self
18
+ class Error < StandardError; end
19
+
20
+ def configure
21
+ yield(configuration)
22
+ end
23
+
24
+ def configuration
25
+ @configuration ||= Configuration.new
26
+ end
27
+
28
+ def server_connection
29
+ @server_connection ||= Connection.new
30
+ end
31
+
32
+ def client_connection
33
+ @client_connection ||= Connection.new
34
+ end
35
+
36
+ def logger
37
+ Logger.new($stdout)
38
+ end
39
+ end
@@ -0,0 +1,37 @@
1
+ module Octokiq
2
+ class CMD
3
+ def run
4
+ puts logo
5
+ server.start
6
+ rescue Interrupt
7
+ Octokiq.logger.info 'Bye!'
8
+ end
9
+
10
+ private
11
+
12
+ def server
13
+ @server ||= Server.new
14
+ end
15
+
16
+ def logo
17
+ <<-EOF
18
+ _,gggggg,_
19
+ ,d8P""d8P"Y8b, I8 ,dPYb,
20
+ ,d8' Y8 "8b,dP I8 IP'`Yb
21
+ d8' `Ybaaad88P' 88888888 I8 8I gg
22
+ 8P `""""Y8 I8 I8 8bgg, ""
23
+ 8b d8 ,gggg, I8 ,ggggg, I8 dP" "8 gg ,gggg,gg
24
+ Y8, ,8P dP" "Yb I8 dP" "Y8ggg I8d8bggP" 88 dP" "Y8I
25
+ `Y8, ,8P' i8' ,I8, i8' ,8I I8P' "Yb, 88 i8' ,8I
26
+ `Y8b,,__,,d8P' ,d8,_ _ ,d88b, ,d8, ,d8' ,d8 `Yb,_,88,_,d8, ,d8b
27
+ `"Y8888P"' P""Y8888PP 8P""Y8 P"Y8888P" 88P Y88P""Y8P"Y8888P"88d
28
+ I8P
29
+ I8'
30
+ I8
31
+ I8
32
+ I8
33
+ I8
34
+ EOF
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,15 @@
1
+ module Octokiq
2
+ class Configuration
3
+ attr_accessor :processors_count,
4
+ :queue_prefix,
5
+ :default_queue,
6
+ :queues
7
+
8
+ def initialize
9
+ @processors_count = Etc.nprocessors
10
+ @queue_prefix = 'octokiq'
11
+ @default_queue = 'default'
12
+ @queues = [@default_queue]
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,28 @@
1
+ module Octokiq
2
+ class Connection
3
+ attr_accessor :redis
4
+
5
+ def initialize
6
+ @redis = Redis.new
7
+ end
8
+
9
+ def fetch(queues)
10
+ _, job = redis.blpop(*build_queues(queues), 0)
11
+ JSON.parse(job)
12
+ end
13
+
14
+ def push(queue, data)
15
+ redis.rpush(build_queue(queue), data.to_json)
16
+ end
17
+
18
+ private
19
+
20
+ def build_queues(queues)
21
+ queues.map { |q| build_queue(q) }
22
+ end
23
+
24
+ def build_queue(queue)
25
+ "#{Octokiq.configuration.queue_prefix}:#{queue}"
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,4 @@
1
+ module Octokiq
2
+ class Logger < ::Logger
3
+ end
4
+ end
@@ -0,0 +1,15 @@
1
+ module Octokiq
2
+ class Processor
3
+ attr_accessor :job
4
+ def initialize(job)
5
+ @job = job
6
+ end
7
+
8
+ def run
9
+ Octokiq.logger.info "Job: #{job}"
10
+ klass = job.fetch(Worker::CLASS_KEY)
11
+ args = job.fetch(Worker::ARGS_KEY)
12
+ Object.const_get(klass).new.perform(*args)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,32 @@
1
+ module Octokiq
2
+ class Server
3
+ def start
4
+ (1..Octokiq.configuration.processors_count).map do
5
+ Ractor.new(pipe) do |pipe|
6
+ job = pipe.take
7
+ processor = Processor.new(job)
8
+ processor.run
9
+ end
10
+ end
11
+
12
+ loop do
13
+ job = Octokiq.server_connection.fetch(queues)
14
+ pipe << job
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ def queues
21
+ Octokiq.configuration.queues
22
+ end
23
+
24
+ def pipe
25
+ @pipe ||= Ractor.new do
26
+ loop do
27
+ Ractor.yield Ractor.receive
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Octokiq
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ module Octokiq
2
+ module Worker
3
+ CLASS_KEY = 'class'.freeze
4
+ ARGS_KEY = 'args'.freeze
5
+
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ base.queue(Octokiq.configuration.default_queue)
9
+ end
10
+
11
+ module ClassMethods
12
+ def perform_async(*args)
13
+ client_push(CLASS_KEY => self, ARGS_KEY => args)
14
+ end
15
+
16
+ def queue(name)
17
+ @@queue = name
18
+ end
19
+
20
+ private
21
+
22
+ def client_push(item)
23
+ Octokiq.client_connection.push(@@queue, item)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ require_relative 'lib/octokiq/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'octokiq'
5
+ spec.version = Octokiq::VERSION
6
+ spec.authors = ['Bran Liang']
7
+ spec.email = ['bran@corran.cn']
8
+
9
+ spec.summary = 'Next generation background processing for ruby'
10
+ spec.description = 'Ractor based worker system.'
11
+ spec.homepage = 'https://github.com/BranLiang/octokiq'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 3.0.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/BranLiang/octokiq'
17
+ spec.metadata['changelog_uri'] = 'https://github.com/BranLiang/octokiq/CHANGELOG.md'
18
+
19
+ # Specify which files should be added to the gem when it is released.
20
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
21
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
22
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|examples)/}) }
23
+ end
24
+ spec.bindir = 'exe'
25
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
26
+ spec.require_paths = ['lib']
27
+ spec.add_runtime_dependency('redis', '~> 4.2')
28
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: octokiq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Bran Liang
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: redis
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
27
+ description: Ractor based worker system.
28
+ email:
29
+ - bran@corran.cn
30
+ executables:
31
+ - octokiq
32
+ extensions: []
33
+ extra_rdoc_files: []
34
+ files:
35
+ - ".gitignore"
36
+ - ".travis.yml"
37
+ - CHANGELOG.md
38
+ - CODE_OF_CONDUCT.md
39
+ - Gemfile
40
+ - Gemfile.lock
41
+ - LICENSE.txt
42
+ - README.md
43
+ - Rakefile
44
+ - bin/console
45
+ - bin/setup
46
+ - exe/octokiq
47
+ - lib/octokiq.rb
48
+ - lib/octokiq/cmd.rb
49
+ - lib/octokiq/configuration.rb
50
+ - lib/octokiq/connection.rb
51
+ - lib/octokiq/logger.rb
52
+ - lib/octokiq/processor.rb
53
+ - lib/octokiq/server.rb
54
+ - lib/octokiq/version.rb
55
+ - lib/octokiq/worker.rb
56
+ - octokiq.gemspec
57
+ homepage: https://github.com/BranLiang/octokiq
58
+ licenses:
59
+ - MIT
60
+ metadata:
61
+ homepage_uri: https://github.com/BranLiang/octokiq
62
+ source_code_uri: https://github.com/BranLiang/octokiq
63
+ changelog_uri: https://github.com/BranLiang/octokiq/CHANGELOG.md
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 3.0.0
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubygems_version: 3.1.4
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Next generation background processing for ruby
83
+ test_files: []