active_switch 0.0.1

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6ced59de3ae362b7e1caf48f0bc1b6a0e3ef8788
4
+ data.tar.gz: 2242e9403fd6eacf2e0cd80bcd0c66f8a0329b41
5
+ SHA512:
6
+ metadata.gz: 56df8d8a4940f35a8b7060c039f3b2abb1910c1c8112b5a25a28cbb98ed2691ea0aebf7a4aae3f2f7e196c56d6cbbfd20c6cded69488ba46b309ae19f5b10dbd
7
+ data.tar.gz: 59398e3ce94ac1b15593068978d50f4d04aece9640f3e68e2bb1554c82ba7c7329890bcca838403d75f52f6f12985ccc86c34cd231bd5e911bbe3cb08cf202e2
@@ -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,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ before_install: gem install bundler -v 1.12.5
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in active_switch.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Brendon Murphy
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,156 @@
1
+ # ActiveSwitch
2
+
3
+ ActiveSwitch stores last reported at timestamps in Redis so you can detect if cron style jobs are running at an expected interval.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_switch'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_switch
20
+
21
+ ## Usage
22
+
23
+ ### Configuration & Registration
24
+
25
+ First, configure `ActiveSwitch`, for instance in a Rails initializer:
26
+
27
+ ```ruby
28
+ # config/initializers/active_switch.rb
29
+
30
+ # configure the Redis client
31
+ ActiveSwitch.redis = Redis.new
32
+
33
+ # expect the big batch job to run within past day
34
+ ActiveSwitch.register(:big_batch_job, 1.day)
35
+
36
+ # expect the weekly mailer to run within the past week
37
+ ActiveSwitch.register(:weekly_mailer, 1.week)
38
+ ```
39
+
40
+ Attempting to register the same name more than once will raise an `ActiveSwitch::AlreadyRegistered` exception.
41
+
42
+ Alternatively, you can register in one call with a hash:
43
+
44
+ ```ruby
45
+ ActiveSwitch.register({
46
+ big_batch_job: 1.day,
47
+ weekly_mailer: 1.week
48
+ })
49
+ ```
50
+
51
+ ### Reporting In
52
+
53
+ After your scheduled task or background job completes, you can report it complete:
54
+
55
+ ```ruby
56
+ ActiveSwitch.report(:weekly_mailer) # => true
57
+ ```
58
+
59
+ Attempting to report on an unregistered name will raise an `ActiveSwitch::UnknownName` exception. This prevents
60
+ dead code paths or typos of names.
61
+
62
+ Alternatively, you can provide `.report` a block to yield:
63
+
64
+ ```ruby
65
+ ActiveSwitch.report(:weekly_mailer) { 2 + 2 } # => 4
66
+ ```
67
+
68
+ ### Status Retrieval
69
+
70
+ Statuses can be retrieved with `.all`, `.active`, or `.inactive`:
71
+
72
+ ```ruby
73
+ ActiveSwitch.report(:weekly_mailer)
74
+
75
+ # Returns hash of statuses with keys "big_batch_job" and "weekly_mailer"
76
+ #
77
+ # {
78
+ # "big_batch_job"=>#<ActiveSwitch::Status:0x007fbb9309e880 @name="big_batch_job", @last_reported_at=nil, @threshold_seconds=86400>},
79
+ # "weekly_mailer"=>#<ActiveSwitch::Status:0x007fbb9309f990 @name="weekly_mailer", @last_reported_at=2017-12-03 23:02:42 -0800, @threshold_seconds=604800>}
80
+ # }
81
+ ActiveSwitch.all
82
+
83
+ # Returns hash of statuses with key "weekly_mailer"
84
+ #
85
+ # {
86
+ # "weekly_mailer"=>#<ActiveSwitch::Status:0x007fbb9309f990 @name="weekly_mailer", @last_reported_at=2017-12-03 23:02:42 -0800, @threshold_seconds=604800>}
87
+ # }
88
+ ActiveSwitch.active
89
+
90
+ # Returns hash of statuses with key "big_batch_job"
91
+ #
92
+ # {
93
+ # "big_batch_job"=>#<ActiveSwitch::Status:0x007fbb9309e880 @name="big_batch_job", @last_reported_at=nil, @threshold_seconds=86400>}
94
+ # }
95
+ ActiveSwitch.inactive
96
+ ```
97
+
98
+ Individual status may also be retrieved with `.status`
99
+
100
+ ```ruby
101
+ ActiveSwitch.status(:weekly_mailer)
102
+ # => <ActiveSwitch::Status:0x007fbb9309f990 @name="weekly_mailer", @last_reported_at=2017-12-03 23:02:42 -0800, @threshold_seconds=604800>
103
+ ```
104
+
105
+ ### Status instances
106
+
107
+ A status instance can be asked for its values:
108
+
109
+ ```ruby
110
+ status = ActiveSwitch.status(:weekly_mailer)
111
+ status.name #=> "weekly_mailer"
112
+ status.last_reported_at #=> 2017-12-03 23:02:42 -0800
113
+ status.threshold_seconds #=> 604800
114
+ ```
115
+
116
+ It can also be checked if active or inactive:
117
+
118
+ ```ruby
119
+ status.active? #=> true
120
+ status.inactive? #=> false
121
+ status.state #=> "ACTIVE"
122
+ ```
123
+
124
+ A status is considered active if it was last reported within its registered threshold seconds.
125
+
126
+ ## Redis Storage
127
+
128
+ All data is stored in a single Redis hash to avoid n+1 roundtrip lookups to Redis when gathering all statuses. Care should
129
+ be taken to avoid tracking too many switches to avoid overloading the hash. A maximum of about 1000 would be sane, and likely
130
+ beyond typical use.
131
+
132
+ The hash is stored under the key `active_switch_last_reported_ats` and reflects the following format:
133
+
134
+ ```ruby
135
+ # Values are epoch seconds
136
+ {
137
+ "weekly_mailer" => "1512371591",
138
+ "big_batch_job" => "1512285202"
139
+ }
140
+ ```
141
+
142
+ ## Development
143
+
144
+ 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.
145
+
146
+ 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).
147
+
148
+ ## Contributing
149
+
150
+ Bug reports and pull requests are welcome on GitHub at https://github.com/bemurphy/active_switch.
151
+
152
+
153
+ ## License
154
+
155
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
156
+
@@ -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,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "active_switch/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_switch"
8
+ spec.version = ActiveSwitch::VERSION
9
+ spec.authors = ["Brendon Murphy"]
10
+ spec.email = ["xternal1+github@gmail.com"]
11
+
12
+ spec.summary = %q{Check that your scheduled tasks are still alive.}
13
+ spec.description = spec.summary
14
+ spec.homepage = "https://github.com/bemurphy/active_switch"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "mock_redis"
26
+
27
+ # Redis is added as a development dependency and expects the parent application to set the dependency
28
+ spec.add_development_dependency "redis"
29
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "active_switch"
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,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,86 @@
1
+ require "active_switch/version"
2
+ require "active_switch/status"
3
+
4
+ module ActiveSwitch
5
+ extend self
6
+
7
+ NoRedisClient = Class.new(StandardError)
8
+ AlreadyRegistered = Class.new(ArgumentError)
9
+ UnknownName = Class.new(ArgumentError)
10
+
11
+ STORAGE_KEY = "active_switch_last_reported_ats".freeze
12
+ REGISTRATIONS = {}
13
+
14
+ class << self
15
+ attr_writer :redis
16
+
17
+ def redis
18
+ @redis or raise NoRedisClient, "No redis client configured"
19
+ end
20
+ end
21
+
22
+ def register(*args)
23
+ if args[0].is_a?(Hash)
24
+ args[0].each { |name, threshold_seconds| register(name, threshold_seconds) }
25
+ else
26
+ name, threshold_seconds = args[0].to_s, args[1]
27
+
28
+ if REGISTRATIONS[name]
29
+ raise AlreadyRegistered, "#{name} already registered"
30
+ else
31
+ REGISTRATIONS[name] = threshold_seconds
32
+ end
33
+ end
34
+ end
35
+
36
+ def report(name)
37
+ name = cast_name(name)
38
+
39
+ if block_given?
40
+ yield.tap { mark_reported(name) }
41
+ else
42
+ mark_reported(name)
43
+ true
44
+ end
45
+ end
46
+
47
+ def status(name)
48
+ name = cast_name(name)
49
+ ts = redis.hget(STORAGE_KEY, name)
50
+
51
+ Status.new(name: name, last_reported_at: ts, threshold_seconds: REGISTRATIONS[name])
52
+ end
53
+
54
+ def all
55
+ data = redis.hgetall(STORAGE_KEY)
56
+
57
+ REGISTRATIONS.each_with_object({}) do |(name, threshold_seconds), obj|
58
+ obj[name] = Status.new(name: name, last_reported_at: data[name],
59
+ threshold_seconds: threshold_seconds)
60
+ end
61
+ end
62
+
63
+ def active
64
+ all.select { |_, s| s.active? }
65
+ end
66
+
67
+ def inactive
68
+ all.select { |_, s| s.inactive? }
69
+ end
70
+
71
+ def report_on_inactive
72
+ inactive.keys.map { |name| report(name) }
73
+ end
74
+
75
+ # Considered private API
76
+
77
+ def mark_reported(name)
78
+ redis.hset(STORAGE_KEY, name, Time.now.to_i)
79
+ end
80
+
81
+ def cast_name(name)
82
+ name.to_s.tap do |name|
83
+ raise UnknownName, "#{name} not found" unless REGISTRATIONS[name]
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,41 @@
1
+ module ActiveSwitch
2
+ class Status
3
+ ACTIVE = "ACTIVE".freeze
4
+ INACTIVE = "INACTIVE".freeze
5
+
6
+ attr_reader :name, :last_reported_at, :threshold_seconds
7
+
8
+ def initialize(name:, last_reported_at:, threshold_seconds:)
9
+ @name = name.to_s
10
+ @last_reported_at = cast_timestamp(last_reported_at, cast_null: false)
11
+ @threshold_seconds = threshold_seconds.to_i
12
+ end
13
+
14
+ def active?
15
+ (cast_timestamp(last_reported_at) + threshold_seconds) > cast_timestamp(now)
16
+ end
17
+
18
+ def inactive?
19
+ !active?
20
+ end
21
+
22
+ def state
23
+ active? ? ACTIVE : INACTIVE
24
+ end
25
+ alias_method :to_s, :state
26
+
27
+ private
28
+
29
+ def now
30
+ Time.now
31
+ end
32
+
33
+ def cast_timestamp(ts, cast_null: true)
34
+ if cast_null
35
+ Time.at((ts || 0).to_i)
36
+ else
37
+ ts ? Time.at(ts.to_i) : nil
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveSwitch
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_switch
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Brendon Murphy
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-09 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: mock_redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: redis
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Check that your scheduled tasks are still alive.
84
+ email:
85
+ - xternal1+github@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE.txt
95
+ - README.md
96
+ - Rakefile
97
+ - active_switch.gemspec
98
+ - bin/console
99
+ - bin/setup
100
+ - lib/active_switch.rb
101
+ - lib/active_switch/status.rb
102
+ - lib/active_switch/version.rb
103
+ homepage: https://github.com/bemurphy/active_switch
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.5.1
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Check that your scheduled tasks are still alive.
127
+ test_files: []