stay_awake 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Tobias Bühlmann
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # stay_awake
2
+
3
+ ## About
4
+ stay_awake requests websites periodically. It's written especially for Heroku, that puts applications to sleep when there are no requests coming in.
5
+
6
+ ## Installation
7
+ ### Gem
8
+ $ gem install stay_awake
9
+ ### Bundler
10
+ ```ruby
11
+ gem 'stay_awake'
12
+ ```
13
+ ### Bundler (head)
14
+ ```ruby
15
+ gem 'stay_awake', :github => 'tbuehlmann/stay_awake'
16
+ ```
17
+
18
+ ## Usage
19
+ ```ruby
20
+ buzzer = StayAwake.configure do |c|
21
+ c.app_name = 'app_name'
22
+ end
23
+
24
+ buzzer.buzz
25
+ ```
26
+
27
+ ### Sinatra
28
+ Put the `StayAwake.configure` block inside Sinatra's `configure` block, run `buzz` and you're done.
29
+
30
+ ### Rails
31
+ Put the StayAwake.configure block inside an initializer, run `buzz` and you're done.
32
+
33
+ ### Configuration Defaults
34
+ | Configuration | Type | Default |
35
+ |:-:|:-:|:-:|
36
+ | `app_name` | `String` | `nil` |
37
+ | `url` | `string` | `nil`|
38
+ | `request_method` | `Symbol` | `:head` |
39
+ | `interval` | `Integer` | `300` |
40
+ | `logger` | `Logger` | `Logger.new(STDOUT)` |
41
+ | `strategy` | `StayAwake::Strategy` | `StayAwake.strategies` |
42
+
43
+ You can either use `app_name` or `url`, but `url` has precedence over `app_name`. If just `app_name` is given, it will be translated to `http://app_name.herokuapp.com/`.
44
+
45
+ When using em-http-request, the buzzing will begin on the `next_tick`.
46
+
47
+ ### Supported HTTP Libraries
48
+ - [em-http-request](https://github.com/igrigorik/em-http-request "em-http-request")
49
+ - [HTTParty](https://github.com/jnunemaker/httparty "HTTParty")
50
+ - [net/http](http://www.ruby-doc.org/stdlib-1.9.3/libdoc/net/http/rdoc/index.html "net/http")
data/lib/stay_awake.rb ADDED
@@ -0,0 +1,45 @@
1
+ require 'logger'
2
+ require 'securerandom'
3
+ require 'singleton'
4
+
5
+ module StayAwake
6
+ def self.config
7
+ Configuration.instance
8
+ end
9
+
10
+ def self.configure(&block)
11
+ yield config
12
+ self
13
+ end
14
+
15
+ def self.logger
16
+ config.logger
17
+ end
18
+
19
+ def self.strategies
20
+ @strategies ||= []
21
+ end
22
+
23
+ def self.buzz
24
+ Buzzer.instance.buzz
25
+ end
26
+
27
+ def self.buzzing?
28
+ Buzzer.instance.buzzing?
29
+ end
30
+
31
+ def self.shut_off
32
+ Buzzer.instance.shut_off
33
+ end
34
+
35
+ require 'stay_awake/buzzer'
36
+ require 'stay_awake/configuration'
37
+ require 'stay_awake/strategy'
38
+ require 'stay_awake/version'
39
+
40
+ module Strategies
41
+ require 'stay_awake/strategies/em_http_request'
42
+ require 'stay_awake/strategies/httparty'
43
+ require 'stay_awake/strategies/net_http'
44
+ end
45
+ end
@@ -0,0 +1,47 @@
1
+ module StayAwake
2
+ class Buzzer
3
+ include Singleton
4
+
5
+ def initialize
6
+ @buzzing = false
7
+ @strategy = nil
8
+ end
9
+
10
+ def buzzing?
11
+ @buzzing
12
+ end
13
+
14
+ def buzz
15
+ unless buzzing?
16
+ if StayAwake.config.url
17
+ url = StayAwake.config.url
18
+ elsif StayAwake.config.app_name
19
+ url = "http://#{StayAwake.config.app_name}.herokuapp.com/"
20
+ else
21
+ raise ConfigurationError.new('config.url or config.app_name needed.')
22
+ end
23
+
24
+ start_buzzing(url)
25
+ @buzzing = true
26
+ end
27
+ end
28
+
29
+ def shut_off
30
+ @strategy.shut_off if buzzing?
31
+ @buzzing = false
32
+ end
33
+
34
+ private
35
+
36
+ def start_buzzing(url)
37
+ strategies = [StayAwake.config.strategy].flatten
38
+ @strategy = strategies.select(&:available?).first
39
+
40
+ if @strategy
41
+ @strategy.buzz(url)
42
+ else
43
+ raise ConfigurationError.new('No valid Strategy given.')
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,22 @@
1
+ module StayAwake
2
+ class Configuration
3
+ include Singleton
4
+
5
+ attr_accessor :app_name
6
+ attr_accessor :url
7
+ attr_accessor :request_method
8
+ attr_accessor :interval
9
+ attr_accessor :logger
10
+ attr_accessor :strategy
11
+
12
+ def initialize
13
+ @request_method = :head
14
+ @interval = 5*60
15
+ @logger = Logger.new(STDOUT)
16
+ @logger.progname = :stay_awake
17
+ @strategy = StayAwake.strategies
18
+ end
19
+ end
20
+
21
+ class ConfigurationError < StandardError; end
22
+ end
@@ -0,0 +1,4 @@
1
+ module StayAwake
2
+ module Sinatra
3
+ end
4
+ end
@@ -0,0 +1,40 @@
1
+ module StayAwake
2
+ module Strategies
3
+ class EmHttpRequest
4
+ include Singleton, Strategy
5
+
6
+ def self.available?
7
+ defined?(EventMachine::HttpRequest) ? true : false
8
+ end
9
+
10
+ def self.buzz(url)
11
+ StayAwake.logger.info 'Starting buzzing using EventMachine::HttpRequest.'
12
+
13
+ EM.next_tick do
14
+ @timer = EM::PeriodicTimer.new(StayAwake.config.interval) do
15
+ uuid = SecureRandom.uuid
16
+ request = EventMachine::HttpRequest.new(url)
17
+ StayAwake.logger.debug "Starting HTTP Request (#{uuid})."
18
+ request = request.send(StayAwake.config.request_method)
19
+
20
+ request.callback do
21
+ StayAwake.logger.debug "HTTP Response arrived (#{uuid})."
22
+ end
23
+
24
+ request.errback do
25
+ StayAwake.logger.error "HTTP Request failed (#{uuid})."
26
+ end
27
+
28
+ # TODO: Timeout?
29
+ end
30
+ end
31
+ end
32
+
33
+ def self.shut_off
34
+ @timer.cancel
35
+ @timer = nil
36
+ StayAwake.logger.info 'Stopped buzzing.'
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,42 @@
1
+ module StayAwake
2
+ module Strategies
3
+ class Httparty
4
+ include Singleton, Strategy
5
+
6
+ def self.available?
7
+ defined?(HTTParty) ? true : false
8
+ end
9
+
10
+ def self.buzz(url)
11
+ require 'timeout'
12
+ StayAwake.logger.info 'Starting buzzing using HTTParty.'
13
+
14
+ @thread = Thread.new do
15
+ loop do
16
+ begin
17
+ Timeout.timeout(30) do
18
+ uuid = SecureRandom.uuid
19
+ StayAwake.logger.debug "Starting HTTP Request (#{uuid})."
20
+ HTTParty.send(StayAwake.config.request_method, url)
21
+ StayAwake.logger.debug "HTTP Response arrived (#{uuid})."
22
+ # TODO: Failing request?
23
+ end
24
+ rescue Timeout::Error
25
+ StayAwake.logger.error "HTTP Timeout Error after 30 seconds (#{uuid})."
26
+ rescue StandardError => e
27
+ StayAwake.logger.error e.message
28
+ end
29
+
30
+ sleep StayAwake.config.interval
31
+ end
32
+ end
33
+ end
34
+
35
+ def self.shut_off
36
+ @thread.kill
37
+ @thread = nil
38
+ StayAwake.logger.info 'Stopped buzzing.'
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,54 @@
1
+ module StayAwake
2
+ module Strategies
3
+ class NetHttp
4
+ include Singleton, Strategy
5
+
6
+ def self.available?
7
+ begin
8
+ require 'net/http'
9
+ require 'uri'
10
+ true
11
+ rescue LoadError
12
+ false
13
+ end
14
+ end
15
+
16
+ def self.buzz(url)
17
+ require 'timeout'
18
+ StayAwake.logger.info 'Starting buzzing using net/http.'
19
+
20
+ @thread = Thread.new do
21
+ loop do
22
+ begin
23
+ Timeout.timeout(30) do
24
+ uuid = SecureRandom.uuid
25
+ StayAwake.logger.debug "Starting HTTP Request (#{uuid})."
26
+
27
+ uri = URI.parse(url)
28
+ http = Net::HTTP.new(uri.host, uri.port)
29
+ request_method = StayAwake.config.request_method.to_s.capitalize
30
+ request_class = Kernel.const_get("Net::HTTP::#{request_method}")
31
+ response = http.request(request_class.new(uri.request_uri))
32
+
33
+ StayAwake.logger.debug "HTTP Response arrived (#{uuid})."
34
+ # TODO: Failing request?
35
+ end
36
+ rescue Timeout::Error
37
+ StayAwake.logger.error "HTTP Timeout Error after 30 seconds (#{uuid})."
38
+ rescue StandardError => e
39
+ StayAwake.logger.error e.message
40
+ end
41
+
42
+ sleep StayAwake.config.interval
43
+ end
44
+ end
45
+ end
46
+
47
+ def self.shut_off
48
+ @thread.kill
49
+ @thread = nil
50
+ StayAwake.logger.info 'Stopped buzzing.'
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,22 @@
1
+ module StayAwake
2
+ module Strategy
3
+ def self.included(base)
4
+ StayAwake.strategies << base
5
+ base.extend ClassMethods
6
+ end
7
+
8
+ module ClassMethods
9
+ def available?(*args)
10
+ raise NotImplementedError
11
+ end
12
+
13
+ def buzz(*args)
14
+ raise NotImplementedError
15
+ end
16
+
17
+ def shut_off(*args)
18
+ raise NotImplementedError
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ module StayAwake
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,24 @@
1
+ # encoding: utf-8
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+
6
+ require 'stay_awake/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'stay_awake'
10
+ spec.version = StayAwake::VERSION
11
+ spec.authors = ['Tobias Bühlmann']
12
+ spec.email = ['tobias.buehlmann@gmx.de']
13
+ spec.description = "Requests websites so they won't fall asleep. Like on Heroku."
14
+ spec.summary = "stay_awake requests websites like Heroku so they won't fall asleep. It supports the HTTP libraries em-http-request, HTTParty and net/http."
15
+ spec.homepage = 'https://github.com/tbuehlmann/stay_awake'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files`.split($/)
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'bundler', '~> 1.2'
22
+ spec.add_development_dependency 'em-http-request', '~> 1.0'
23
+ spec.add_development_dependency 'httparty', '~> 0.10'
24
+ end
metadata ADDED
@@ -0,0 +1,109 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stay_awake
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Tobias Bühlmann
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-24 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.2'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.2'
30
+ - !ruby/object:Gem::Dependency
31
+ name: em-http-request
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: httparty
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.10'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.10'
62
+ description: Requests websites so they won't fall asleep. Like on Heroku.
63
+ email:
64
+ - tobias.buehlmann@gmx.de
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - lib/stay_awake.rb
74
+ - lib/stay_awake/buzzer.rb
75
+ - lib/stay_awake/configuration.rb
76
+ - lib/stay_awake/sinatra.rb
77
+ - lib/stay_awake/strategies/em_http_request.rb
78
+ - lib/stay_awake/strategies/httparty.rb
79
+ - lib/stay_awake/strategies/net_http.rb
80
+ - lib/stay_awake/strategy.rb
81
+ - lib/stay_awake/version.rb
82
+ - stay_awake.gemspec
83
+ homepage: https://github.com/tbuehlmann/stay_awake
84
+ licenses:
85
+ - MIT
86
+ post_install_message:
87
+ rdoc_options: []
88
+ require_paths:
89
+ - lib
90
+ required_ruby_version: !ruby/object:Gem::Requirement
91
+ none: false
92
+ requirements:
93
+ - - '>='
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 1.8.25
105
+ signing_key:
106
+ specification_version: 3
107
+ summary: stay_awake requests websites like Heroku so they won't fall asleep. It supports
108
+ the HTTP libraries em-http-request, HTTParty and net/http.
109
+ test_files: []