sinatra-run-later 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +15 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +41 -0
- data/Rakefile +1 -0
- data/lib/sinatra/run-later.rb +110 -0
- data/lib/sinatra/run-later/version.rb +5 -0
- data/sinatra-run-later.gemspec +23 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
MzI4NWNlNzIyNDZjZTg2ZGVlNzU0N2FlZDgzZWVlODNiMzA3MTEyZQ==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
MWI3OTM2MzBkZTJhY2UxOTAyZmI2ZjdhYjlkZGRhZmFkODRlOGFhYw==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
ZmQ0Nzg5MWRmMDAzMmYzNzMwYzJmMmZlZTliNTVkNGZmNTA5ZTU1MThhNTFi
|
10
|
+
OTMzMmUxYWUzMjc5Zjg4MDcxZTM4NTE1ZDBiNWE3MmYwNmNkNTA1ODU4MGJm
|
11
|
+
NGQ3MGUxNDdiZmQ3ZTRmNjU3NmIxMTkxNTNhYTUzNGExYzUyMzM=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
Y2M0NjY5ZjFjZjdiN2JlMjJkOWZjNDBlY2UzZjMyZDE5N2QyODQ4OWUxNDQ3
|
14
|
+
NDJhMDhjYjJmMzY0YWEwNjYxNWEyZjQ4YTQ1YzQ0NmZjYTkwZTQ4NDdiMjQ5
|
15
|
+
MDdkMDkxNDRiMjUxMDMyM2M2ZWNkMjA5ODM0YWI0NWEzMzE3ZjE=
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Evan Lecklider
|
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,41 @@
|
|
1
|
+
# Sinatra::RunLater
|
2
|
+
|
3
|
+
A simple way to do real after filtering with Sinatra.
|
4
|
+
|
5
|
+
A gem-ed up version of a slight modification of [this version](https://github.com/pmamediagroup/sinatra_run_later) that cleans up nicely when used through `rackup` and `ctrl-c` is sent.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'sinatra-run-later', :require => 'sinatra/run-later'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install sinatra-run-later
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
Anywhere in your Sinatra app, just drop in something like this:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
get '/' do
|
27
|
+
run_later do
|
28
|
+
# some task that you don't want to block.
|
29
|
+
end
|
30
|
+
|
31
|
+
"Hello World"
|
32
|
+
end
|
33
|
+
```
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
1. Fork it
|
38
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
39
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
40
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
41
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,110 @@
|
|
1
|
+
require 'timeout'
|
2
|
+
require 'sinatra/base'
|
3
|
+
require 'sinatra/run-later/version'
|
4
|
+
|
5
|
+
module Sinatra
|
6
|
+
module RunLater
|
7
|
+
@@run_now = false
|
8
|
+
@@queue = ::Queue.new
|
9
|
+
|
10
|
+
def self.queue
|
11
|
+
@@queue
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.run_now?
|
15
|
+
@@run_now
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.run_now=(run_now)
|
19
|
+
@@run_now = run_now
|
20
|
+
end
|
21
|
+
|
22
|
+
module InstanceMethods
|
23
|
+
def run_later(&block)
|
24
|
+
if RunLater.run_now?
|
25
|
+
block.call
|
26
|
+
else
|
27
|
+
@@run_later ||= RunLater::Worker.instance
|
28
|
+
RunLater.queue << block
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Worker
|
34
|
+
attr_accessor :thread
|
35
|
+
attr_accessor :logger
|
36
|
+
|
37
|
+
def initialize
|
38
|
+
@thread = Thread.new {
|
39
|
+
|
40
|
+
# removing this seems to allow it shut down nicely
|
41
|
+
# trap :INT do
|
42
|
+
# RunLater::Worker.shutdown
|
43
|
+
# exit
|
44
|
+
# end
|
45
|
+
|
46
|
+
loop {
|
47
|
+
process_queue
|
48
|
+
}
|
49
|
+
}
|
50
|
+
end
|
51
|
+
|
52
|
+
def self.instance
|
53
|
+
@worker ||= RunLater::Worker.new
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.shutdown
|
57
|
+
begin
|
58
|
+
Timeout::timeout 10 do
|
59
|
+
loop { break unless instance.thread[:running] }
|
60
|
+
end
|
61
|
+
rescue Timeout::Error
|
62
|
+
# logger.error("Worker thread timed out. Forcing shutdown.")
|
63
|
+
ensure
|
64
|
+
instance.thread.kill
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.cleanup
|
69
|
+
begin
|
70
|
+
Timeout::timeout 10 do
|
71
|
+
loop do
|
72
|
+
break unless instance.thread[:running]
|
73
|
+
|
74
|
+
# When run in Passenger, explicitly pass control to another thread
|
75
|
+
# which will in return hand over control to the worker thread.
|
76
|
+
# However, it doesn't work in Passenger 2.1.0, since it removes
|
77
|
+
# all its classes before handing the request over to Rails.
|
78
|
+
|
79
|
+
# Thread.pass if defined?(::Passenger)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
rescue Timeout::Error
|
83
|
+
# logger.warn("Worker thread takes too long and will be killed.")
|
84
|
+
# logger.flush
|
85
|
+
instance.thread.kill
|
86
|
+
@worker = RunLater::Worker.new
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def process_queue
|
91
|
+
begin
|
92
|
+
while block = RunLater.queue.pop
|
93
|
+
Thread.pass
|
94
|
+
Thread.current[:running] = true
|
95
|
+
block.call
|
96
|
+
Thread.current[:running] = false
|
97
|
+
# logger.flush
|
98
|
+
end
|
99
|
+
rescue Exception => e
|
100
|
+
# logger.error("Worker thread crashed, retrying. Error was: #{e}")
|
101
|
+
# logger.flush
|
102
|
+
Thread.current[:running] = false
|
103
|
+
retry
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
helpers RunLater::InstanceMethods
|
110
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
$:.unshift File.expand_path('../lib', __FILE__)
|
2
|
+
require 'sinatra/run-later/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = 'sinatra-run-later'
|
6
|
+
spec.version = Sinatra::RunLater::VERSION
|
7
|
+
spec.authors = ['Evan Lecklider']
|
8
|
+
spec.email = ['evan.lecklider@gmail.com']
|
9
|
+
spec.description = %q{A simple way to do real after filtering with Sinatra.}
|
10
|
+
spec.summary = spec.description
|
11
|
+
spec.homepage = 'https://github.com/l3ck/sinatra-run-later'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
|
14
|
+
spec.files = `git ls-files`.split($/)
|
15
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ['lib']
|
18
|
+
|
19
|
+
spec.add_dependency 'sinatra', '~> 1.4.0'
|
20
|
+
|
21
|
+
spec.add_development_dependency 'bundler', '~> 1.3'
|
22
|
+
spec.add_development_dependency 'rake'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sinatra-run-later
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Evan Lecklider
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-16 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sinatra
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.4.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.4.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A simple way to do real after filtering with Sinatra.
|
56
|
+
email:
|
57
|
+
- evan.lecklider@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- .gitignore
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- lib/sinatra/run-later.rb
|
68
|
+
- lib/sinatra/run-later/version.rb
|
69
|
+
- sinatra-run-later.gemspec
|
70
|
+
homepage: https://github.com/l3ck/sinatra-run-later
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- lib
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.0.6
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: A simple way to do real after filtering with Sinatra.
|
94
|
+
test_files: []
|