opentox-ruby-api-wrapper 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,13 @@
1
+ ['rubygems', 'sinatra', 'sinatra/url_for', 'redis','builder', 'rest_client', 'yaml', 'cgi', 'spork', 'environment'].each do |lib|
2
+ require lib
3
+ end
4
+
5
+ begin
6
+ require 'openbabel'
7
+ rescue LoadError
8
+ puts "Please install Openbabel with 'rake openbabel:install' in the compound component"
9
+ end
10
+
11
+ ['opentox', 'compound','feature','dataset','algorithm','model','task','utils'].each do |lib|
12
+ require lib
13
+ end
@@ -0,0 +1,27 @@
1
+ module OpenTox
2
+
3
+ class OpenTox
4
+ attr_accessor :uri
5
+
6
+ def initialize(uri)
7
+ @uri = uri
8
+ end
9
+
10
+ # Get the object name
11
+ def name
12
+ RestClient.get @uri + '/name'
13
+ end
14
+
15
+ # Deletes an object
16
+ def destroy
17
+ RestClient.delete @uri
18
+ end
19
+
20
+ # Object path without hostname
21
+ def path
22
+ URI.split(@uri)[5]
23
+ end
24
+
25
+ end
26
+
27
+ end
@@ -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
@@ -0,0 +1,55 @@
1
+ module OpenTox
2
+
3
+ class Task < OpenTox
4
+ #private :new
5
+
6
+ def initialize(uri)
7
+ super(uri)
8
+ end
9
+
10
+ def self.create(params)
11
+ uri = RestClient.post @@config[:services]["opentox-task"], :resource_uri => params[:resource_uri]
12
+ Task.new uri
13
+ end
14
+
15
+ def self.find(params)
16
+ Task.new(params[:uri])
17
+ end
18
+
19
+ def self.base_uri
20
+ @@config[:services]["opentox-task"]
21
+ end
22
+
23
+ def start
24
+ RestClient.put @uri, :status => 'started'
25
+ end
26
+
27
+ def stop
28
+ RestClient.put @uri, :status => 'stopped'
29
+ end
30
+
31
+ def completed
32
+ RestClient.put @uri, :status => 'completed'
33
+ end
34
+
35
+ def status
36
+ RestClient.get File.join(@uri, 'status')
37
+ end
38
+
39
+ def completed?
40
+ self.status == 'completed'
41
+ end
42
+
43
+ def resource
44
+ RestClient.get @uri
45
+ end
46
+
47
+ def wait_for_completion
48
+ until self.completed?
49
+ sleep 1
50
+ end
51
+ end
52
+
53
+ end
54
+
55
+ end
@@ -0,0 +1,135 @@
1
+ require File.join(File.dirname(__FILE__), '..', 'opentox-ruby-api-wrapper.rb')
2
+ require File.join(File.dirname(__FILE__), 'redis.rb')
3
+
4
+ namespace :redis do
5
+
6
+ desc "Flush Redis testing database"
7
+ task :flush do
8
+ require 'redis'
9
+ r = Redis.new :db => 2
10
+ r.flush_db
11
+ end
12
+
13
+ desc "Flush all Redis databases"
14
+ task :flushall do
15
+ require 'redis'
16
+ r = Redis.new
17
+ r.flush_all
18
+ end
19
+
20
+
21
+ end
22
+
23
+ namespace :opentox do
24
+
25
+ desc "Install required gems"
26
+ task :install do
27
+ puts `sudo gem install #{@gems}`
28
+ end
29
+
30
+ desc "Update gems"
31
+ task :update do
32
+ puts `sudo gem update #{@gems}`
33
+ end
34
+
35
+ namespace :services do
36
+
37
+ desc "Run opentox services"
38
+ task :start do
39
+ @@config[:services].each do |service,uri|
40
+ dir = File.join(@@config[:base_dir], service)
41
+ server = @@config[:webserver]
42
+ case server
43
+ when /thin|mongrel|webrick/
44
+ port = uri.sub(/^.*:/,'').sub(/\/$/,'')
45
+ Dir.chdir dir
46
+ pid_file = File.join(@@tmp_dir,"#{service}.pid")
47
+ begin
48
+ `#{server} --trace --rackup config.ru start -p #{port} -e #{ENV['RACK_ENV']} -P #{pid_file} -d &`
49
+ puts "#{service} started on localhost:#{port} in #{ENV['RACK_ENV']} environment with PID file #{pid_file}."
50
+ rescue
51
+ puts "Cannot start #{service} on port #{port}."
52
+ end
53
+ when 'passenger'
54
+ `touch #{File.join(dir, 'tmp/restart.txt')}`
55
+ puts "#{service} restarted."
56
+ else
57
+ puts "not yet implemented"
58
+ end
59
+ end
60
+ end
61
+
62
+ desc "Stop opentox services"
63
+ task :stop do
64
+ server = @@config[:webserver]
65
+ if server =~ /thin|mongrel|webrick/
66
+ @@config[:services].each do |service,uri|
67
+ port = uri.sub(/^.*:/,'').sub(/\/$/,'')
68
+ pid_file = File.join(@@tmp_dir,"#{service}.pid")
69
+ begin
70
+ puts `#{server} stop -P #{pid_file}`
71
+ puts "#{service} stopped on localhost:#{port}"
72
+ rescue
73
+ puts "Cannot stop #{service} on port #{port}."
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ desc "Restart opentox services"
80
+ task :restart => [:stop, :start]
81
+
82
+ end
83
+
84
+ desc "Run all OpenTox tests"
85
+ task :test do
86
+ @@config[:services].each do |service,uri|
87
+ dir = File.join(@@config[:base_dir], service)
88
+ Dir.chdir dir
89
+ puts "Running tests in #{dir}"
90
+ `rake test -t 1>&2`
91
+ end
92
+ end
93
+
94
+ end
95
+
96
+ desc "Start service in current directory"
97
+ task :start do
98
+ service = File.basename(Dir.pwd).intern
99
+ server = @@config[:webserver]
100
+ case server
101
+ when /thin|mongrel|webrick/
102
+ port = @@config[:services][service].sub(/^.*:/,'').sub(/\/$/,'')
103
+ pid_file = File.join(@@tmp_dir,"#{service}.pid")
104
+ begin
105
+ `#{server} --trace --rackup config.ru start -p #{port} -e #{ENV['RACK_ENV']} -P #{pid_file} -d &`
106
+ puts "#{service} started on localhost:#{port} in #{ENV['RACK_ENV']} environment with PID file #{pid_file}."
107
+ rescue
108
+ puts "Cannot start #{service} on port #{port}."
109
+ end
110
+ when 'passenger'
111
+ `touch tmp/restart.txt`
112
+ puts "#{service} restarted."
113
+ else
114
+ puts "not yet implemented"
115
+ end
116
+ end
117
+
118
+ desc "Stop service in current directory"
119
+ task :stop do
120
+ service = File.basename(Dir.pwd).intern
121
+ server = @@config[:webserver]
122
+ if server =~ /thin|mongrel|webrick/
123
+ port = @@config[:services][service].sub(/^.*:/,'').sub(/\/$/,'')
124
+ pid_file = File.join(@@tmp_dir,"#{service}.pid")
125
+ begin
126
+ puts `thin stop -P #{pid_file}`
127
+ puts "#{service} stopped on localhost:#{port}"
128
+ rescue
129
+ puts "Cannot stop #{service} on port #{port}."
130
+ end
131
+ end
132
+ end
133
+
134
+ desc "Restart service in current directory"
135
+ task :restart => [:stop, :start]
@@ -0,0 +1,125 @@
1
+ # Inspired by rabbitmq.rake the Redbox project at http://github.com/rick/redbox/tree/master
2
+ require 'fileutils'
3
+ require 'open-uri'
4
+
5
+ class RedisRunner
6
+
7
+ def self.redisdir
8
+ "/tmp/redis/"
9
+ end
10
+
11
+ def self.redisconfdir
12
+ '/etc/redis.conf'
13
+ end
14
+
15
+ def self.dtach_socket
16
+ '/tmp/redis.dtach'
17
+ end
18
+
19
+ # Just check for existance of dtach socket
20
+ def self.running?
21
+ File.exists? dtach_socket
22
+ end
23
+
24
+ def self.start
25
+ puts 'Detach with Ctrl+\ Re-attach with rake redis:attach'
26
+ sleep 3
27
+ exec "dtach -A #{dtach_socket} redis-server #{redisconfdir}"
28
+ end
29
+
30
+ def self.attach
31
+ exec "dtach -a #{dtach_socket}"
32
+ end
33
+
34
+ def self.stop
35
+ sh 'echo "SHUTDOWN" | nc localhost 6379'
36
+ end
37
+
38
+ end
39
+
40
+ namespace :redis do
41
+
42
+ desc 'About redis'
43
+ task :about do
44
+ puts "\nSee http://code.google.com/p/redis/ for information about redis.\n\n"
45
+ end
46
+
47
+ desc 'Start redis'
48
+ task :start do
49
+ RedisRunner.start
50
+ end
51
+
52
+ desc 'Stop redis'
53
+ task :stop do
54
+ RedisRunner.stop
55
+ end
56
+
57
+ desc 'Restart redis'
58
+ task :restart do
59
+ RedisRunner.stop
60
+ RedisRunner.start
61
+ end
62
+
63
+ desc 'Attach to redis dtach socket'
64
+ task :attach do
65
+ RedisRunner.attach
66
+ end
67
+
68
+ desc 'Install the lastest verison of Redis from Github (requires git, duh)'
69
+ task :install => [:about, :download, :make] do
70
+ %w(redis-benchmark redis-cli redis-server).each do |bin|
71
+ sh "sudo cp /tmp/redis/#{bin} /usr/bin/"
72
+ end
73
+
74
+ puts "Installed redis-benchmark, redis-cli and redis-server to /usr/bin/"
75
+
76
+ unless File.exists?('/etc/redis.conf')
77
+ sh 'sudo cp /tmp/redis/redis.conf /etc/'
78
+ puts "Installed redis.conf to /etc/ \n You should look at this file!"
79
+ end
80
+ end
81
+
82
+ task :make do
83
+ sh "cd #{RedisRunner.redisdir} && make clean"
84
+ sh "cd #{RedisRunner.redisdir} && make"
85
+ end
86
+
87
+ desc "Download package"
88
+ task :download do
89
+ sh 'rm -rf /tmp/redis/' if File.exists?("#{RedisRunner.redisdir}/.svn")
90
+ sh 'git clone git://github.com/antirez/redis.git /tmp/redis' unless File.exists?(RedisRunner.redisdir)
91
+ sh "cd #{RedisRunner.redisdir} && git pull" if File.exists?("#{RedisRunner.redisdir}/.git")
92
+ end
93
+
94
+ end
95
+
96
+ namespace :dtach do
97
+
98
+ desc 'About dtach'
99
+ task :about do
100
+ puts "\nSee http://dtach.sourceforge.net/ for information about dtach.\n\n"
101
+ end
102
+
103
+ desc 'Install dtach 0.8 from source'
104
+ task :install => [:about] do
105
+
106
+ Dir.chdir('/tmp/')
107
+ unless File.exists?('/tmp/dtach-0.8.tar.gz')
108
+ require 'net/http'
109
+
110
+ url = 'http://downloads.sourceforge.net/project/dtach/dtach/0.8/dtach-0.8.tar.gz'
111
+ open('/tmp/dtach-0.8.tar.gz', 'wb') do |file| file.write(open(url).read) end
112
+ end
113
+
114
+ unless File.directory?('/tmp/dtach-0.8')
115
+ system('tar xzf dtach-0.8.tar.gz')
116
+ end
117
+
118
+ Dir.chdir('/tmp/dtach-0.8/')
119
+ sh 'cd /tmp/dtach-0.8/ && ./configure && make'
120
+ sh 'sudo cp /tmp/dtach-0.8/dtach /usr/bin/'
121
+
122
+ puts 'Dtach successfully installed to /usr/bin.'
123
+ end
124
+ end
125
+
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'sinatra'
3
+ require 'application.rb'
4
+ require 'rack'
5
+ require 'rack/contrib'
6
+
7
+ FileUtils.mkdir_p 'log' unless File.exists?('log')
8
+ log = File.new("log/#{ENV["RACK_ENV"]}.log", "a")
9
+ $stdout.reopen(log)
10
+ $stderr.reopen(log)
11
+
12
+ if ENV['RACK_ENV'] == 'production'
13
+ use Rack::MailExceptions do |mail|
14
+ mail.to 'helma@in-silico.ch'
15
+ mail.subject '[ERROR] %s'
16
+ end
17
+ elsif ENV['RACK_ENV'] == 'development'
18
+ use Rack::Reloader
19
+ use Rack::ShowExceptions
20
+ end
21
+
22
+ run Sinatra::Application
23
+