the_lone_dyno 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +9 -0
- data/README.md +18 -4
- data/lib/the_lone_dyno.rb +5 -1
- data/lib/the_lone_dyno/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57ba91bea8834c6e34ddbb1c1538ba646e9d82a0
|
4
|
+
data.tar.gz: afe9c78b57d5a1d032f36f28a9e450e4a6cff5f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48adc6fdab84f9347f174352227ae8e3ee9504f9a88f3774a22d0a556b9d97cd4278a8ca5d0f23ccfe1d34f5a194b8cb13ae0f1a44f95c5f19e2b5b5950a1693
|
7
|
+
data.tar.gz: dada3ca815f8a0643103f834a62a29e5078f5aa84b2b3a824bb8e88188683c2983034a0775bfd7daec014b0d565f84120a13653b21ae83b8166a96e3bbdd8e88
|
data/CHANGELOG.md
ADDED
data/README.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
|
3
3
|
# TheLoneDyno
|
4
4
|
|
5
|
-
Isolate code to only run on a certain number of Heroku dynos.
|
5
|
+
Isolate code to only run on a certain number of Heroku dynos. Uses Postgres [advisory locks](http://www.postgresql.org/docs/9.1/static/explicit-locking.html) to isolate behavior and Postgres [LISTEN/NOTIFY](http://www.postgresql.org/docs/9.1/static/sql-notify.html) to trigger custom events.
|
6
6
|
|
7
7
|
[![Build Status](https://travis-ci.org/schneems/the_lone_dyno.svg?branch=master)](https://travis-ci.org/schneems/the_lone_dyno)
|
8
8
|
|
@@ -12,7 +12,7 @@ Why would you want to run code on only one dyno? Maybe you want to add some perf
|
|
12
12
|
|
13
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
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.
|
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
16
|
|
17
17
|
## Installation
|
18
18
|
|
@@ -46,7 +46,7 @@ end
|
|
46
46
|
puts "Does not block future code execution"
|
47
47
|
```
|
48
48
|
|
49
|
-
This code will only run on one dyno
|
49
|
+
This code will only run on one dyno (by default in a "web" process type). The 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
50
|
|
51
51
|
```
|
52
52
|
Does not block future code execution
|
@@ -105,7 +105,19 @@ TheLoneDyno.exclusive do |signal|
|
|
105
105
|
end
|
106
106
|
```
|
107
107
|
|
108
|
-
##
|
108
|
+
## Restricting to Process Type
|
109
|
+
|
110
|
+
By default `the_lone_dyno` is restricted to only run in the `web` process type dynos. It does this using the [DYNO environment variable](https://devcenter.heroku.com/articles/dynos#local-environment-variables). You can set this to another process type:
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
TheLoneDyno.exclusive(process_type: "worker") do
|
114
|
+
# Code you only want to run on 5 dyno
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
This will attempt to lock any DYNO environment variable containing "worker" string. You can use a string or a regex. if the `$DYNO` environment variable isn't present or if `process_type` is set to `false` then the check will be skipped and the first process to boot will aquire the lock regardless of dyno type. This behavior is purposful so you can aquire and lock on your local develoment machine.
|
119
|
+
|
120
|
+
## Running on more than 1 Dyno
|
109
121
|
|
110
122
|
You can control the number of dynos you run code on by passing in an integer:
|
111
123
|
|
@@ -115,6 +127,8 @@ TheLoneDyno.exclusive(dynos: 5) do
|
|
115
127
|
end
|
116
128
|
```
|
117
129
|
|
130
|
+
## Using multiple locks on the same app
|
131
|
+
|
118
132
|
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
133
|
|
120
134
|
```ruby
|
data/lib/the_lone_dyno.rb
CHANGED
@@ -3,10 +3,14 @@ require "pg_lock"
|
|
3
3
|
|
4
4
|
module TheLoneDyno
|
5
5
|
DEFAULT_KEY = "the_lone_dyno_hi_ho_silver"
|
6
|
+
WEB_PROCESS_TYPE_REGEX = /\Aweb/
|
6
7
|
|
7
8
|
# Use to ensure only `dynos` count of dynos are exclusively running
|
8
9
|
# the given block
|
9
|
-
def self.exclusive(background: true, dynos: 1, key_base: DEFAULT_KEY, connection: ::PgLock::DEFAULT_CONNECTION_CONNECTOR.call, &block)
|
10
|
+
def self.exclusive(background: true, dynos: 1, process_type: WEB_PROCESS_TYPE_REGEX, key_base: DEFAULT_KEY, connection: ::PgLock::DEFAULT_CONNECTION_CONNECTOR.call, &block)
|
11
|
+
|
12
|
+
return if process_type && ENV["DYNO"] && !ENV["DYNO"].match(process_type)
|
13
|
+
|
10
14
|
if background
|
11
15
|
Thread.new do
|
12
16
|
forever_block = Proc.new { |*args| block.call(*args); while true do; sleep 180 ; end; }
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: the_lone_dyno
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- schneems
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg_lock
|
@@ -104,6 +104,7 @@ files:
|
|
104
104
|
- ".gitignore"
|
105
105
|
- ".rspec"
|
106
106
|
- ".travis.yml"
|
107
|
+
- CHANGELOG.md
|
107
108
|
- CODE_OF_CONDUCT.md
|
108
109
|
- Gemfile
|
109
110
|
- LICENSE.txt
|