helma-opentox-ruby-api-wrapper 0.1.2 → 0.1.3

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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
@@ -1,5 +1,6 @@
1
1
  require 'rest_client'
2
2
  require 'crack/xml'
3
+ require 'spork'
3
4
 
4
5
  ENV['OPENTOX_COMPOUND'] = 'http://webservices.in-silico.ch/compound/v0/' unless ENV['OPENTOX_COMPOUND']
5
6
  ENV['OPENTOX_FEATURE'] = 'http://webservices.in-silico.ch/feature/v0/' unless ENV['OPENTOX_FEATURE']
data/lib/spork.rb ADDED
@@ -0,0 +1,81 @@
1
+ # A way to cleanly handle process forking in Sinatra when using Passenger, aka "sporking some code".
2
+ # This will allow you to properly execute some code asynchronously, which otherwise does not work correctly.
3
+ #
4
+ # Written by Ron Evans
5
+ # More info at http://deadprogrammersociety.com
6
+ #
7
+ # Mostly lifted from the Spawn plugin for Rails (http://github.com/tra/spawn)
8
+ # but with all of the Rails stuff removed.... cause you are using Sinatra. If you are using Rails, Spawn is
9
+ # what you need. If you are using something else besides Sinatra that is Rack-based under Passenger, and you are having trouble with
10
+ # asynch processing, let me know if spork helped you.
11
+ #
12
+ module Spork
13
+ # things to close in child process
14
+ @@resources = []
15
+ def self.resources
16
+ @@resources
17
+ end
18
+
19
+ # set the resource to disconnect from in the child process (when forking)
20
+ def self.resource_to_close(resource)
21
+ @@resources << resource
22
+ end
23
+
24
+ # close all the resources added by calls to resource_to_close
25
+ def self.close_resources
26
+ @@resources.each do |resource|
27
+ resource.close if resource && resource.respond_to?(:close) && !resource.closed?
28
+ end
29
+ @@resources = []
30
+ end
31
+
32
+ # actually perform the fork... er, spork
33
+ # valid options are:
34
+ # :priority => to set the process priority of the child
35
+ # :logger => a logger object to use from the child
36
+ # :no_detach => true if you want to keep the child process under the parent control. usually you do NOT want this
37
+ def self.spork(options={})
38
+ logger = options[:logger]
39
+ logger.debug "spork> parent PID = #{Process.pid}" if logger
40
+ child = fork do
41
+ begin
42
+ start = Time.now
43
+ logger.debug "spork> child PID = #{Process.pid}" if logger
44
+
45
+ # set the nice priority if needed
46
+ Process.setpriority(Process::PRIO_PROCESS, 0, options[:priority]) if options[:priority]
47
+
48
+ # disconnect from the rack
49
+ Spork.close_resources
50
+
51
+ # run the block of code that takes so long
52
+ yield
53
+
54
+ rescue => ex
55
+ logger.error "spork> Exception in child[#{Process.pid}] - #{ex.class}: #{ex.message}" if logger
56
+ ensure
57
+ logger.info "spork> child[#{Process.pid}] took #{Time.now - start} sec" if logger
58
+ # this form of exit doesn't call at_exit handlers
59
+ exit!(0)
60
+ end
61
+ end
62
+
63
+ # detach from child process (parent may still wait for detached process if they wish)
64
+ Process.detach(child) unless options[:no_detach]
65
+
66
+ return child
67
+ end
68
+
69
+ end
70
+
71
+ # Patch to work with passenger
72
+ if defined? Passenger::Rack::RequestHandler
73
+ class Passenger::Rack::RequestHandler
74
+ alias_method :orig_process_request, :process_request
75
+ def process_request(env, input, output)
76
+ Spork.resource_to_close(input)
77
+ Spork.resource_to_close(output)
78
+ orig_process_request(env, input, output)
79
+ end
80
+ end
81
+ end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{opentox-ruby-api-wrapper}
8
- s.version = "0.1.2"
8
+ s.version = "0.1.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Christoph Helma"]
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
24
24
  "Rakefile",
25
25
  "VERSION",
26
26
  "lib/opentox-ruby-api-wrapper.rb",
27
+ "lib/spork.rb",
27
28
  "opentox-ruby-api-wrapper.gemspec",
28
29
  "test/opentox-ruby-api-wrapper_test.rb",
29
30
  "test/test_helper.rb"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: helma-opentox-ruby-api-wrapper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christoph Helma
@@ -59,6 +59,7 @@ files:
59
59
  - Rakefile
60
60
  - VERSION
61
61
  - lib/opentox-ruby-api-wrapper.rb
62
+ - lib/spork.rb
62
63
  - opentox-ruby-api-wrapper.gemspec
63
64
  - test/opentox-ruby-api-wrapper_test.rb
64
65
  - test/test_helper.rb