From 4d343f7c584ce09638005665b4ed2de718ff17a6 Mon Sep 17 00:00:00 2001 From: Christoph Helma Date: Tue, 25 Aug 2009 20:58:26 +0200 Subject: rake tasks to (re)start webservices added --- lib/environment.rb | 40 ++++--------------------- lib/opentox-ruby-api-wrapper.rb | 31 +++++++++---------- lib/tasks/opentox.rb | 56 ++++++++++++++++++++++++++++++++--- lib/templates/config.yaml | 11 ++----- opentox-ruby-api-wrapper.gemspec | 4 ++- test/opentox-ruby-api-wrapper_test.rb | 3 +- 6 files changed, 81 insertions(+), 64 deletions(-) diff --git a/lib/environment.rb b/lib/environment.rb index b22e8e5..793fef9 100644 --- a/lib/environment.rb +++ b/lib/environment.rb @@ -1,8 +1,10 @@ -require 'yaml' -config_file = File.join(ENV['HOME'], '.opentox/config.yaml') +# load configuration +ENV['RACK_ENV'] = 'development' unless ENV['RACK_ENV'] + +config_file = File.join(ENV['HOME'], ".opentox/config/#{ENV['RACK_ENV']}.yaml") if File.exist?(config_file) - config = YAML.load_file(config_file) + @@config = YAML.load_file(config_file) else FileUtils.mkdir_p File.dirname(config_file) FileUtils.cp(File.join(File.dirname(__FILE__), 'templates/config.yaml'), config_file) @@ -10,35 +12,3 @@ else exit end -puts config - -@environment = "development" unless @environment = ENV['OPENTOX'] -@config = config[@environment] - -port = 5000 -@services = {} -begin - `killall thin` if @environment == "development" -rescue -end -config["services"].each do |service| - dir = File.join(@config["base_dir"], service) - case @environment - when "development|test" - @services[dir] = "http://localhost:#{port}/" - Dir.chdir dir - `thin --debug --rackup config.ru start -p #{port} -e #{@environment} &` - #pid = fork {`urxvt -title #{service} -e thin --debug --rackup config.ru start -p #{port} -e development`} - #Process.detach(pid) - port += 1 - when "production" - @services[dir] = "http://#{@config['base_uri']}/#{service}/v#{major_version}/" - `touch #{File.join(dir,"tmp/restart.txt")}` - else - "Puts environment #{ENV['OPENTOX']} not supported." - end -end - -def major_version - File.open(File.join(File.dirname(__FILE__), '../VERSION')).each_line.first.split(/\./)[0] -end diff --git a/lib/opentox-ruby-api-wrapper.rb b/lib/opentox-ruby-api-wrapper.rb index 4ee6fd3..d74b412 100644 --- a/lib/opentox-ruby-api-wrapper.rb +++ b/lib/opentox-ruby-api-wrapper.rb @@ -1,4 +1,4 @@ -['rubygems', 'rest_client', 'spork', 'environment'].each do |lib| +['rubygems', 'sinatra', 'sinatra/respond_to', 'sinatra/url_for', 'builder', 'rest_client', 'yaml', 'spork', 'environment'].each do |lib| require lib end @@ -9,12 +9,8 @@ module OpenTox # Escape all nonword characters def uri_escape(string) - URI.escape(string, /[^\w]/) - end - - # Returns true if object creation has finished (for asynchronous processes) - def finished? - YAML.load(RestClient.get(@uri))[:finished] + #URI.escape(string, /[^\w]/) + URI.escape(string, /[^#{URI::PATTERN::UNRESERVED}]/) end # Get the object name @@ -36,9 +32,9 @@ module OpenTox if params[:uri] @uri = params[:uri].to_s elsif params[:smiles] - @uri = RestClient.post @services['opentox-compound'] ,:smiles => uri_escape(params[:smiles]) + @uri = RestClient.post @@config[:services]["opentox-compound"] ,:smiles => uri_escape(params[:smiles]) elsif params[:name] - @uri = RestClient.post @services['opentox-compound'] ,:name => uri_escape(params[:name]) + @uri = RestClient.post @@config[:services]["opentox-compound"] ,:name => uri_escape(params[:name]) end end @@ -47,6 +43,11 @@ module OpenTox RestClient.get @uri end + # Get the unique id (URI encoded canonical smiles) + def uid + RestClient.get @uri + '.uid' + end + # Matchs a smarts string def match?(smarts) if RestClient.get(@uri + '/match/' + uri_escape(smarts)) == 'true' @@ -70,7 +71,7 @@ module OpenTox if params[:uri] @uri = params[:uri].to_s else - @uri = @services['opentox-feature']+ uri_escape(params[:name]) + @uri = @@config[:services]["opentox-feature"] + uri_escape(params[:name]) params[:values].each do |k,v| @uri += '/' + k.to_s + '/' + v.to_s end @@ -91,9 +92,9 @@ module OpenTox if params[:uri] @uri = params[:uri].to_s elsif params[:name] and params[:filename] - @uri = `curl -X POST -F file=@#{params[:filename]} -F name="#{params[:name]}" #{@services['opentox-dataset']}` + @uri = `curl -X POST -F file=@#{params[:filename]} -F name="#{params[:name]}" #{@@config[:services]["opentox-dataset"]}` elsif params[:name] - @uri = RestClient.post @services['opentox-dataset'], :name => params[:name] + @uri = RestClient.post @@config[:services]["opentox-dataset"], :name => params[:name] end end @@ -104,7 +105,7 @@ module OpenTox # Get all compounds and features from a dataset, returns a hash with compound_uris as keys and arrays of feature_uris as values def all_compounds_and_features_uris - YAML.load(RestClient.get(@uri + '/compounds/features')) + YAML.load(RestClient.get(@uri + '/compounds/features.yaml')) end # Get all features from a dataset @@ -133,7 +134,7 @@ module OpenTox # Create a new dataset with BBRC features def initialize(training_dataset) - @dataset_uri = RestClient.post @services['opentox-fminer'], :dataset_uri => training_dataset.uri + @dataset_uri = RestClient.post @@config[:services]["opentox-fminer"], :dataset_uri => training_dataset.uri end def dataset @@ -149,7 +150,7 @@ module OpenTox if params[:uri] @uri = params[:uri] elsif params[:dataset_uri] - @uri = RestClient.post @services['opentox-lazar']+ 'models' , :dataset_uri => params[:dataset_uri] + @uri = RestClient.post @@config[:services]["opentox-lazar"] + 'models' , :dataset_uri => params[:dataset_uri] end end diff --git a/lib/tasks/opentox.rb b/lib/tasks/opentox.rb index 59fc16c..6f1284f 100644 --- a/lib/tasks/opentox.rb +++ b/lib/tasks/opentox.rb @@ -1,4 +1,4 @@ -require File.join(File.dirname(__FILE__), '../..', 'test/opentox-ruby-api-wrapper_test.rb') +require "environment" namespace :opentox do @@ -12,9 +12,57 @@ namespace :opentox do puts `sudo gem update #{@gems}` end - desc "Run tests" - task :test do - load 'test.rb' + namespace :services do + + desc "Run opentox services" + task :start do + @@config[:services].each do |service,uri| + dir = File.join(@@config[:base_dir], service) + case @@config[:webserver] + when 'thin' + port = uri.sub(/^.*:/,'').sub(/\/$/,'') + Dir.chdir dir + begin + `thin --trace --rackup config.ru start -p #{port} -e #{ENV['RACK_ENV']} &` + puts "#{service} started on port #{port}." + rescue + puts "Cannot start #{service} on port #{port}." + end + when 'passenger' + puts "not yet implemented" + else + puts "not yet implemented" + end + end + end + + desc "Stop opentox services" + task :stop do + @@config[:services].each do |service,uri| + port = uri.sub(/^.*:/,'').sub(/\/$/,'') + `echo "SHUTDOWN" | nc localhost #{port}` if port + end + end + + desc "Restart opentox services" + task :restart => [:stop, :start] + + end + + namespace :test do + + ENV['RACK_ENV'] = 'test' + test = "#{Dir.pwd}/test/test.rb" + + desc "Run local tests" + task :local => "opentox:services:restart" do + load test + end + + task :remote do + #load 'test.rb' + end + end end diff --git a/lib/templates/config.yaml b/lib/templates/config.yaml index bae9dc6..768fa01 100644 --- a/lib/templates/config.yaml +++ b/lib/templates/config.yaml @@ -1,11 +1,6 @@ -development: - base_dir: ~/webservices - -production: - base_uri: webservices.in-silico.ch - base_dir: /var/www/webservices - -services: +:base_dir: /home/ch/webservices +:webserver: thin +:services: - opentox-feature - opentox-compound - opentox-dataset diff --git a/opentox-ruby-api-wrapper.gemspec b/opentox-ruby-api-wrapper.gemspec index 9d30d93..5c72422 100644 --- a/opentox-ruby-api-wrapper.gemspec +++ b/opentox-ruby-api-wrapper.gemspec @@ -9,7 +9,7 @@ Gem::Specification.new do |s| s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= s.authors = ["Christoph Helma"] - s.date = %q{2009-08-21} + s.date = %q{2009-08-25} s.description = %q{Ruby wrapper for the OpenTox REST API (http://www.opentox.org)} s.email = %q{helma@in-silico.ch} s.extra_rdoc_files = [ @@ -23,11 +23,13 @@ Gem::Specification.new do |s| "README.rdoc", "Rakefile", "VERSION", + "lib/environment.rb", "lib/helper.rb", "lib/opentox-ruby-api-wrapper.rb", "lib/spork.rb", "lib/tasks/opentox.rb", "lib/templates/config.yaml", + "lib/templates/config.yaml", "opentox-ruby-api-wrapper.gemspec", "test/hamster_carcinogenicity.csv", "test/opentox-ruby-api-wrapper_test.rb", diff --git a/test/opentox-ruby-api-wrapper_test.rb b/test/opentox-ruby-api-wrapper_test.rb index 6b09c4a..0a394e2 100644 --- a/test/opentox-ruby-api-wrapper_test.rb +++ b/test/opentox-ruby-api-wrapper_test.rb @@ -1,4 +1,4 @@ -require 'test_helper' +require File.join(File.dirname(__FILE__), 'test_helper.rb') class OpentoxRubyApiWrapperTest < Test::Unit::TestCase @@ -10,6 +10,7 @@ class OpentoxRubyApiWrapperTest < Test::Unit::TestCase port += 1 end end + ENV['OPENTOX'] = "test" end def test_create_dataset_and_model_and_make_a_prediction -- cgit v1.2.3