the_lone_dyno 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
+ SHA1:
3
+ metadata.gz: 8cc00080214d127a40dd7ffde3cd0ae7e7298bda
4
+ data.tar.gz: 8948b5958cae2c5cf67e17cdf4c82ab8949a376e
5
+ SHA512:
6
+ metadata.gz: 56a23c590fdcce8d35b9891e99482feafb9a932d7623a398e085fec5d8f1fbc2510415f6353c94b62afdb9da0bf27dabe6afd0241e65ea7c8a88ed271aeb0dda
7
+ data.tar.gz: 35e29cfe0431f04c0b5e2c01a41c5d2dd04e0b65561086dbb461c83a96047a6f6e91e081eccf6a391e8a85c494d2e20c1072378338dd0295fa80e9c01f80830f
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,7 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
5
+
6
+ before_script:
7
+ - psql -c 'create database the_lone_dyno_test;' -U postgres
@@ -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 the_lone_dyno.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 schneems
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,206 @@
1
+ ![The Lone Dyno logo](https://www.dropbox.com/s/m7v8yndgcgt8sf3/Screenshot%202015-10-08%2014.42.32.png?dl=1)
2
+
3
+ # TheLoneDyno
4
+
5
+ Isolate code to only run on a certain number of Heroku dynos. Using and Postgres [advisory locks](http://www.postgresql.org/docs/9.1/static/explicit-locking.html). You can later trigger events using Postgres listen/notify.
6
+
7
+ [![Build Status](https://travis-ci.org/schneems/the_lone_dyno.svg?branch=master)](https://travis-ci.org/schneems/the_lone_dyno)
8
+
9
+ ## Why?
10
+
11
+ Why would you want to run code on only one dyno? Maybe you want to add some performance monitoring code to a critical path in production, you don't want to slow down all your dynos, so you can retain throughput by isolating to only run on one dyno. Maybe you want to test out a refactoring or change, instead of rolling it out to all of your service, you could run it on a select number. Whatever the reason, if you want to run some code on only a few dynos, this is the gem for you!
12
+
13
+ Why is this needed? All Heroku dynos operate independently of one another. Once one is running you can't change it. You can `$ heroku run bash` but this gives you a new dyno that doesn't receieve any web traffic. If you want to run code on only a certain number of dynos it's been difficult to do so until now.
14
+
15
+ > Be warned, only changing behavior on 1 dyno in your app, could cause difficult to reproduce problems. "I'm getting an error but only once fore every 20 requests". Using this library is an advanced technique and should be implemented with care. Make sure to tell the rest of your team what you're doing, and remove the code from your codebase as soon as you're done.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'the_lone_dyno'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install the_lone_dyno
32
+
33
+ ## Usage
34
+
35
+ In an initializer like `config/initializers/the_lone_dyno.rb` configure your code:
36
+
37
+ ```ruby
38
+ TheLoneDyno.exclusive do
39
+ while true do
40
+ # Code that only runs in 1 dyno in the background
41
+ puts "bump-ba-da-bump-ba-da-bump"
42
+ sleep 1
43
+ end
44
+ end
45
+
46
+ puts "Does not block future code execution"
47
+ ```
48
+
49
+ This code will only run on one dyno. By default code passed into the block will run in the background and the lock will be held for as long as your process is alive. For example if we run the above code we should get:
50
+
51
+ ```
52
+ Does not block future code execution
53
+ bump-ba-da-bump-ba-da-bump
54
+ bump-ba-da-bump-ba-da-bump
55
+ bump-ba-da-bump-ba-da-bump
56
+ bump-ba-da-bump-ba-da-bump
57
+ bump-ba-da-bump-ba-da-bump
58
+ # ...
59
+ ```
60
+
61
+ So your app will continue to load and run as usual.
62
+
63
+ Note: The Lone Dyno restricts code at the process/machine level. While it will prevent this code from being executed on multiple machines it is not restricted from running multiple times in the same process. If you are calling this code in multiple threads it will execute in every thread. If that's not what you want, you'll need to add your own mutex.
64
+
65
+ ## Triggering Events
66
+
67
+ Sometimes you'll need to interact with your Lone Dyno externally. You can trigger events with listen/notify.
68
+
69
+ ```ruby
70
+ TheLoneDyno.exclusive do |signal|
71
+ # Code you only want to run on 1 dyno
72
+
73
+ signal.watch do |payload|
74
+ puts "I only get called when I receive the signal: #{payload}"
75
+ end
76
+ end
77
+ ```
78
+
79
+ The code you are running inside of the `watch` block will run in a background thread waiting for a notification. How do you send a signal? You can do so with `$ heroku run bash`
80
+
81
+ ```
82
+ $ heroku run bash
83
+ $ rails console
84
+ >
85
+ > TheLoneDyno.signal("hi ho silver!")
86
+ ```
87
+
88
+ When you do this 1 and only one dyno will emit
89
+
90
+ ```
91
+ "I only get called when I receive the signal: hi ho silver"
92
+ ```
93
+
94
+ Whatever configuration you pass into `TheLoneDyno#exclusive` need to be passed into `TheLoneDyno#signal` such as `key_base` and `dynos`. See configuration options below. This is needed since each dyno maintains a unique lock, we re-use the same name to signal the same dyno using postgres' [Listen/Notify](http://www.postgresql.org/docs/9.1/static/sql-notify.html).
95
+
96
+ By default the background thread wakes up every 60 seconds and waits 0.1 seconds to see if there is a message. You can customize this behavior using `sleep` and `ttl`. So to have it check every 10 seconds, and not wait at all you could run
97
+
98
+ ```ruby
99
+ TheLoneDyno.exclusive do |signal|
100
+ # Code you only want to run on 1 dyno
101
+
102
+ signal.watch(sleep: 10, ttl: 0) do |payload|
103
+ puts "I only get called when I receive the signal: #{payload}"
104
+ end
105
+ end
106
+ ```
107
+
108
+ ## Config
109
+
110
+ You can control the number of dynos you run code on by passing in an integer:
111
+
112
+ ```ruby
113
+ TheLoneDyno.exclusive(dynos: 5) do
114
+ # Code you only want to run on 5 dyno
115
+ end
116
+ ```
117
+
118
+ Under the hood this uses PG advisory locks. If you need to customize the default advisory key, for instance if you want to have multiple processes you want to isolate to a set of dynos you can use the `key_base:` key, for example:
119
+
120
+ ```ruby
121
+ TheLoneDyno.exclusive(key_base: "reticulate splines") do
122
+ # Code for reticulating splines
123
+ end
124
+
125
+ TheLoneDyno.exclusive(dynos: 42, key_base: "extrapolate conclusions") do
126
+ # Code for extrapolating conclusions
127
+ end
128
+ ```
129
+
130
+ ## Run Syncronously in the Foreground
131
+
132
+ If you don't want to run your exclusive process in the background you can force it to run syncronously using `background: false`. For example:
133
+
134
+
135
+ ```ruby
136
+ TheLoneDyno.exclusive(background: false) do |signal|
137
+ while true do
138
+ puts "bump-ba-da-bump-ba-da-bump"
139
+ sleep 1
140
+ end
141
+ end
142
+
143
+ puts "Does block execution"
144
+ ```
145
+
146
+ produces
147
+
148
+ ```
149
+ bump-ba-da-bump-ba-da-bump
150
+ bump-ba-da-bump-ba-da-bump
151
+ bump-ba-da-bump-ba-da-bump
152
+ bump-ba-da-bump-ba-da-bump
153
+ bump-ba-da-bump-ba-da-bump
154
+ # ...
155
+ ```
156
+
157
+ Notice the `puts "Does block execution"` never gets called because our Lone Dyno block never exits.
158
+
159
+ Keep in mind that your lock is only held for the duration of your block, so if the code in your block exits, another machine may be able to pick up the lock. For this reason it is recommended to not run short tasks syncronously in the foreground.
160
+
161
+
162
+ ## Connection
163
+
164
+ By default The Lone Dyno assumes you're using Active Record and already have a connection configured. If you want to use a different ORM, you'll need to provide TheLoneDyno with an object that responds to `exec` that executes arbitrary SQL. Under the hood this library uses [pg_lock](https://github.com/heroku/pg_lock#database-connection). So that's how you can configure the connection
165
+
166
+ ```
167
+ connection = Module do
168
+ def self.exec(sql, bind)
169
+ DB.fetch(sql, bind)
170
+ end
171
+ end
172
+
173
+ TheLoneDyno.exclusive(connection: connection) do
174
+
175
+ end
176
+ ```
177
+
178
+ You can alternatively set the `DEFAULT_CONNECTION`
179
+
180
+ ```
181
+ Pg::Lock = Module do
182
+ def self.exec(sql, bind)
183
+ DB.fetch(sql, bind)
184
+ end
185
+ end
186
+
187
+ TheLoneDyno.exclusive do
188
+
189
+ end
190
+ ```
191
+
192
+ ## Development
193
+
194
+ 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.
195
+
196
+ 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).
197
+
198
+ ## Contributing
199
+
200
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/the_lone_dyno. 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.
201
+
202
+
203
+ ## License
204
+
205
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
206
+
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+ task :test => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "the_lone_dyno"
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,72 @@
1
+ require "the_lone_dyno/version"
2
+ require "pg_lock"
3
+
4
+ module TheLoneDyno
5
+ DEFAULT_KEY = "the_lone_dyno_hi_ho_silver"
6
+
7
+ # Use to ensure only `dynos` count of dynos are exclusively running
8
+ # the given block
9
+ def self.exclusive(background: true, dynos: 1, key_base: DEFAULT_KEY, connection: ::PgLock::DEFAULT_CONNECTION_CONNECTOR.call, &block)
10
+ if background
11
+ Thread.new do
12
+ forever_block = Proc.new { |*args| block.call(*args); while true do; sleep 180 ; end; }
13
+ Lock.new(key_base, dynos).lock(connection, &forever_block)
14
+ end
15
+ else
16
+ Lock.new(key_base, dynos).lock(connection, &block)
17
+ end
18
+ end
19
+
20
+ # Use to send a custom signal to any exclusive running dynos
21
+ def self.signal(payload = "", dynos: 1, key_base: DEFAULT_KEY, connection: ::PgLock::DEFAULT_CONNECTION_CONNECTOR.call, &block)
22
+ Lock.new(key_base, dynos).keys.each do |key|
23
+ message = "NOTIFY #{key}, '#{payload}'"
24
+ puts message
25
+ connection.exec(message)
26
+ end
27
+ end
28
+
29
+ # Used for running a block when a pg NOTIFY is sent
30
+ class ListenWatch
31
+ def initialize(key, raw_connection)
32
+ @key = key
33
+ @raw_connection = raw_connection
34
+ end
35
+
36
+ def watch(sleep: 60, ttl: 0.01, &block)
37
+ @raw_connection.exec "LISTEN #{@key}"
38
+
39
+ @thread = Thread.new do
40
+ while true do
41
+ sleep sleep
42
+
43
+ @raw_connection.wait_for_notify(ttl) do |channel, pid, payload|
44
+ block.call(payload)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ # Used for generating lock and listen keys. Isolates
52
+ # advisory locking behavior.
53
+ class Lock
54
+ def initialize(key_base, dynos, &block)
55
+ @key_base = key_base.to_s
56
+ @dynos = Integer(dynos)
57
+ @block = block
58
+ end
59
+
60
+ def keys
61
+ @dynos.times.map {|i| "#{@key_base}_#{i}" }
62
+ end
63
+
64
+ def lock(connection, &block)
65
+ keys.each do |key|
66
+ PgLock.new(name: key, ttl: false, connection: connection).lock do
67
+ block.call(ListenWatch.new(key, connection))
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,3 @@
1
+ module TheLoneDyno
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'the_lone_dyno/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "the_lone_dyno"
8
+ spec.version = TheLoneDyno::VERSION
9
+ spec.authors = ["schneems"]
10
+ spec.email = ["richard.schneeman@gmail.com"]
11
+
12
+ spec.summary = %q{Isolate code to only run on a certain number of Heroku dynos.}
13
+ spec.description = %q{Run code on only a certain number of Heroku dynos, isolated}
14
+ spec.homepage = "https://github.com/schneems/the_lone_dyno"
15
+ spec.license = "MIT"
16
+
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "pg_lock", "~> 0.1.0"
24
+
25
+ spec.add_development_dependency "pg", ">= 0.15"
26
+ spec.add_development_dependency "activerecord", ">= 2.3"
27
+ spec.add_development_dependency "bundler", "~> 1.10"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "rspec"
30
+ end
metadata ADDED
@@ -0,0 +1,141 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: the_lone_dyno
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - schneems
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-10-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pg_lock
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 0.1.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 0.1.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: pg
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.15'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0.15'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '2.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '2.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.10'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.10'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Run code on only a certain number of Heroku dynos, isolated
98
+ email:
99
+ - richard.schneeman@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - ".rspec"
106
+ - ".travis.yml"
107
+ - CODE_OF_CONDUCT.md
108
+ - Gemfile
109
+ - LICENSE.txt
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/the_lone_dyno.rb
115
+ - lib/the_lone_dyno/version.rb
116
+ - the_lone_dyno.gemspec
117
+ homepage: https://github.com/schneems/the_lone_dyno
118
+ licenses:
119
+ - MIT
120
+ metadata: {}
121
+ post_install_message:
122
+ rdoc_options: []
123
+ require_paths:
124
+ - lib
125
+ required_ruby_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - ">="
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ requirements: []
136
+ rubyforge_project:
137
+ rubygems_version: 2.4.5.1
138
+ signing_key:
139
+ specification_version: 4
140
+ summary: Isolate code to only run on a certain number of Heroku dynos.
141
+ test_files: []