canto 0.1.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 43e960399cb0a94b0062d5c7ac3752de9a42faa039bc8319408d4b05112eb992
4
+ data.tar.gz: 68f6804de4d4f0b590178c42522ff9edab6936bd8f7e6ac95f72938795ac47a7
5
+ SHA512:
6
+ metadata.gz: 274c5e3277fc0bc29b8698c7e428f1d606ce2b52e5cda3eda9a753b31823898b32eb5f6a090c501782d48b98552f7c19bc5b076f0d0860a1a37d9fd32a7f3177
7
+ data.tar.gz: 3b88b4c04acd0569d1d6ae5ceff2da8323dba55031be16d006220adcd510a42c778803fb9b17440b880aba38a061be3e74ebd63d0de956bdd97be2303f221565
@@ -0,0 +1,39 @@
1
+ version: 2.1
2
+ orbs:
3
+ ruby: circleci/ruby@1.0.0
4
+
5
+ jobs:
6
+ build:
7
+ docker:
8
+ - image: circleci/ruby:2.7.2
9
+
10
+ steps:
11
+ - checkout
12
+ - ruby/install-deps
13
+
14
+ minitest:
15
+ docker:
16
+ - image: circleci/ruby:2.7.2
17
+ environment:
18
+ BUNDLE_JOBS: 3
19
+ BUNDLE_RETRY: 3
20
+ BUNDLE_PATH: vendor/bundle
21
+
22
+ steps:
23
+ - checkout
24
+ - ruby/install-deps
25
+
26
+ - run:
27
+ name: Stale code fix
28
+ command: git fetch && git reset --hard origin/${CIRCLE_BRANCH}
29
+ - run:
30
+ name: Minitest
31
+ command: bundle exec rake test
32
+
33
+ workflows:
34
+ tests:
35
+ jobs:
36
+ - build
37
+ - minitest:
38
+ requires:
39
+ - build
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ *.gem
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.2
data/.standard.yml ADDED
@@ -0,0 +1,22 @@
1
+ #fix: true # default: false
2
+ parallel: true # default: false
3
+ #format: progress # default: Standard::Formatter
4
+ #ruby_version: 2.3.3 # default: RUBY_VERSION
5
+ #default_ignores: false # default: true
6
+ #
7
+ ignore:
8
+ - './**/*':
9
+ - Style/RescueModifier
10
+ - Style/StringLiterals
11
+ - Style/StringLiteralsInInterpolation
12
+ - Lint/AssignmentInCondition
13
+ - Security/YAMLLoad
14
+ - Layout/SpaceInsideHashLiteralBraces
15
+ - Layout/ArgumentAlignment
16
+ - Standard/SemanticBlocks
17
+ # - 'db/schema.rb'
18
+ # - 'vendor/**/*'
19
+ - 'config/**/*'
20
+ - 'spec/**/*':
21
+ - Layout/EmptyLinesAroundBlockBody
22
+ # - Layout/AlignHash
@@ -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 merkulov@uchi.ru. 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,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in canto.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,21 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ canto (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.14.3)
10
+ rake (12.3.3)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ canto!
17
+ minitest (~> 5.0)
18
+ rake (~> 12.0)
19
+
20
+ BUNDLED WITH
21
+ 2.1.4
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Alexander Merkulov
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.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # Canto
2
+
3
+ Canto is a tool to run plain non-blocking ruby programs, like pubsub.
4
+ Canto has simple IO.pipe interface, watching for signals, makes your non-blocking running.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'canto'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle install
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install canto
21
+
22
+ ## Usage
23
+
24
+ bundle exec canto -r PATH_TO_YOUR_RUBY_FILE -e production
25
+
26
+ ## Development
27
+
28
+ 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.
29
+
30
+ 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).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/merqlove/canto. 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/merqloveu/canto/blob/master/CODE_OF_CONDUCT.md).
35
+
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
40
+
41
+ ## Code of Conduct
42
+
43
+ Everyone interacting in the Canto project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/merqloveu/canto/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -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
data/bin/canto ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../lib/canto'
4
+ require_relative '../lib/canto/cli'
5
+
6
+ begin
7
+ cli = Canto::CLI.instance
8
+ cli.parse
9
+ cli.run
10
+ rescue => e
11
+ raise e if $DEBUG
12
+
13
+ exit 1
14
+ end
data/canto.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ require_relative 'lib/canto/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = 'canto'
5
+ spec.version = Canto::VERSION
6
+ spec.authors = ['Alexander Merkulov']
7
+ spec.email = ['merkulov@uchi.ru']
8
+
9
+ spec.summary = 'Canto is a tool to run non-blocking ruby programs, like pubsub.'
10
+ spec.description = 'Canto has simple IO.pipe interface, watching for signals, makes your non-blocking running.'
11
+ spec.homepage = 'https://github.com/merqloveu/canto'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.5.0')
14
+
15
+ spec.metadata['homepage_uri'] = spec.homepage
16
+ spec.metadata['source_code_uri'] = 'https://github.com/merqloveu/canto'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+
24
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+ end
data/lib/canto.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'canto/version'
2
+
3
+ module Canto
4
+ DEFAULTS = {
5
+ environment: nil
6
+ }.freeze
7
+ end
data/lib/canto/cli.rb ADDED
@@ -0,0 +1,150 @@
1
+ require 'singleton'
2
+ require 'optparse'
3
+ require 'fileutils'
4
+
5
+ $stdout.sync = true
6
+
7
+ class Canto
8
+ class CLI
9
+ include Singleton
10
+
11
+ attr_reader :logger
12
+ attr_accessor :environment
13
+
14
+ def parse(args = ARGV)
15
+ setup_options(args)
16
+ initialize_logger
17
+ validate!
18
+ end
19
+
20
+ def run(launcher, boot_app: true)
21
+ boot_application if boot_app
22
+
23
+ self_read, self_write = IO.pipe
24
+ sigs = %w[INT TERM TTIN TSTP]
25
+ sigs.each do |sig|
26
+ trap sig do
27
+ self_write.puts(sig)
28
+ end
29
+ rescue ArgumentError
30
+ puts "Signal #{sig} not supported"
31
+ end
32
+
33
+ launch(self_read)
34
+ end
35
+
36
+ def launch(self_read)
37
+ begin
38
+ while (readable_io = IO.select([self_read]))
39
+ signal = readable_io.first[0].gets.strip
40
+ handle_signal(signal)
41
+ end
42
+ rescue Interrupt
43
+ Canto.logger.info 'Shutting down'
44
+ Canto.logger.info 'Bye!'
45
+
46
+ # Explicitly exit so busy Processor threads won't block process shutdown.
47
+ #
48
+ # NB: slow at_exit handlers will prevent a timely exit if they take
49
+ # a while to run. If Sidekiq is getting here but the process isn't exiting,
50
+ # use the TTIN signal to determine where things are stuck.
51
+ exit(0)
52
+ end
53
+ end
54
+
55
+ SIGNAL_HANDLERS = {
56
+ # Ctrl-C in terminal
57
+ 'INT' => ->(_) { raise Interrupt },
58
+ # TERM is the signal that Pipe must exit.
59
+ # Heroku sends TERM and then waits 30 seconds for process to exit.
60
+ 'TERM' => ->(_) { raise Interrupt },
61
+ 'TSTP' => ->(_) {
62
+ Canto.logger.info 'Received TSTP, no longer accepting new work'
63
+ },
64
+ 'TTIN' => ->(_) {
65
+ Thread.list.each do |thread|
66
+ Canto.logger.warn "Thread TID-#{(thread.object_id ^ ::Process.pid).to_s(36)} #{thread.name}"
67
+ if thread.backtrace
68
+ Canto.logger.warn thread.backtrace.join("\n")
69
+ else
70
+ Canto.logger.warn '<no backtrace available>'
71
+ end
72
+ end
73
+ }
74
+ }.freeze
75
+ UNHANDLED_SIGNAL_HANDLER = ->(_) { Canto.logger.info 'No signal handler registered, ignoring' }
76
+ SIGNAL_HANDLERS.default = UNHANDLED_SIGNAL_HANDLER
77
+
78
+ def handle_signal(sig)
79
+ Canto.logger.debug "Got #{sig} signal"
80
+ SIGNAL_HANDLERS[sig].call(self)
81
+ end
82
+
83
+ def setup_options(args)
84
+ # parse CLI options
85
+ opts = parse_options(args)
86
+
87
+ set_environment opts[:environment]
88
+
89
+ options.merge!(opts)
90
+ end
91
+
92
+ def parse_options(argv)
93
+ opts = {}
94
+ @parser = option_parser(opts)
95
+ @parser.parse!(argv)
96
+ opts
97
+ end
98
+
99
+ def set_environment(cli_env)
100
+ @environment = cli_env || ENV['APP_ENV'] || ENV['RAILS_ENV'] || ENV['RACK_ENV'] || 'development'
101
+ end
102
+
103
+ def initialize_logger
104
+ @logger = Logger.new($stdout, level: Logger::INFO)
105
+ end
106
+
107
+ def boot_application
108
+ ENV['RACK_ENV'] = ENV['RAILS_ENV'] = environment
109
+
110
+ require options[:require]
111
+ end
112
+
113
+ def validate!
114
+ if !File.exist?(options[:require]) ||
115
+ (File.directory?(options[:require]) && !File.exist?("#{options[:require]}/config/application.rb"))
116
+ logger.info '=================================================================='
117
+ logger.info ' Please point Canto to Ruby file '
118
+ logger.info ' to load your classes with -r [FILE].'
119
+ logger.info '=================================================================='
120
+ logger.info @parser
121
+ die(1)
122
+ end
123
+ end
124
+
125
+ def option_parser(opts)
126
+ parser = OptionParser.new { |o|
127
+ o.on '-e', '--environment ENV', 'Application environment' do |arg|
128
+ opts[:environment] = arg
129
+ end
130
+
131
+ o.on '-r', '--require [PATH]', 'Location of ruby file to require' do |arg|
132
+ opts[:require] = arg
133
+ end
134
+
135
+ o.on '-V', '--version', 'Print version and exit' do |arg|
136
+ puts "Canto #{Canto::VERSION}"
137
+ die(0)
138
+ end
139
+ }
140
+
141
+ parser.banner = 'canto [options]'
142
+ parser.on_tail '-h', '--help', 'Show help' do
143
+ logger.info parser
144
+ die 1
145
+ end
146
+
147
+ parser
148
+ end
149
+ end
150
+ end
@@ -0,0 +1,3 @@
1
+ module Canto
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: canto
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alexander Merkulov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-03-19 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Canto has simple IO.pipe interface, watching for signals, makes your
14
+ non-blocking running.
15
+ email:
16
+ - merkulov@uchi.ru
17
+ executables:
18
+ - canto
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - ".circleci/config.yml"
23
+ - ".gitignore"
24
+ - ".ruby-version"
25
+ - ".standard.yml"
26
+ - CODE_OF_CONDUCT.md
27
+ - Gemfile
28
+ - Gemfile.lock
29
+ - LICENSE.txt
30
+ - README.md
31
+ - Rakefile
32
+ - bin/canto
33
+ - canto.gemspec
34
+ - lib/canto.rb
35
+ - lib/canto/cli.rb
36
+ - lib/canto/version.rb
37
+ homepage: https://github.com/merqloveu/canto
38
+ licenses:
39
+ - MIT
40
+ metadata:
41
+ homepage_uri: https://github.com/merqloveu/canto
42
+ source_code_uri: https://github.com/merqloveu/canto
43
+ post_install_message:
44
+ rdoc_options: []
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: 2.5.0
52
+ required_rubygems_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: '0'
57
+ requirements: []
58
+ rubygems_version: 3.1.4
59
+ signing_key:
60
+ specification_version: 4
61
+ summary: Canto is a tool to run non-blocking ruby programs, like pubsub.
62
+ test_files: []