Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Worker Init/Final Log on Datapoint #482

Merged
merged 3 commits into from
Apr 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
.git
.bundle
.idea
vagrant
/doc
/gems
/server/gems
/server/.bundle/
/server/.bundle
/server/Gemfile.lock
/server/log
/server/public/assets
Expand Down
153 changes: 153 additions & 0 deletions doc/initialization_scripts/set_gem_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
#!/bin/bash -e

# This script will enable a user to change a single gem in the list of accessible gems. The script will create the
# NEW_GEMFILE_DIR if it does not already exist.

# This script only works with OpenStudio 1.8.0 or newer.
echo "Calling $0 with arguments: $@"

if [[ (-z $1) || (-z $2) || (-z $3) ]]; then
echo "Expecting script to have 3 parameters:"
echo " 1: Name of the exiting gem to replace, e.g. openstudio-standards"
echo " 2: Argument of the new gem GitHub repo, e.g. NREL/openstudio-standards"
echo " 3: Name of the GitHub branch to install, e.g. master"
echo " -- example use: ./set_standards_version.sh /usr/local/openstudio-2.7.1/Ruby openstudio-standards NREL/openstudio-standards master"
exit 1
fi

# Functions
function replace_gem_in_files (){
# This method replaces the information needed in `filename`.
# Args:
# 1. filepath: path to the location of where the new Gemfile will exist, typically /var/oscli
# 2. gem_name: name of the gem to replace in the Gemfile, e.g., openstudio-standards
# 3. gem_repo: name of the new gem that will from github to install, e.g., NREL/openstudio-standards
# 4. branch: name of the new gem's branch to checkout, e.g., develop
# Example:
# Replace "gem 'openstudio-standards', '= 0.1.15'" with "gem 'openstudio-standards', path: '/var/oscli/clones/openstudio-standards'"

local filepath=$1
local gem_name=$2
local gem_repo=$3
local branch=$4

# Clone the new gem with single branch. This can save a lot of time (e.g., openstudio-standards 5m5.393s to 0m51.276s)
# If the gem has already been checked out, then do a pull.
mkdir -p $filepath/clones
if [ -d "$filepath/clones/$gem_name" ]; then
cd $filepath/clones/$gem_name && git pull
else
git clone https://github.com/$gem_repo.git --branch $branch --single-branch $filepath/clones/$gem_name
fi

echo "***Replacing gem: $gem_name with version on github under $gem_repo"
local OLDSTRING="gem '$gem_name'"
local NEWSTRING="gem '$gem_name', path: '$filepath/clones/$gem_name'"
if [ ! -f "$filepath/Gemfile" ]; then
echo "New Gemfile does not exist: ${filepath}/Gemfile"
exit 1
fi
sed -i -e "s|$OLDSTRING.*|$NEWSTRING|g" ${filepath}/Gemfile

# now replace the openstudio-gems.gemspec version constraint
if [ ! -f "$filepath/openstudio-gems.gemspec" ]; then
echo "openstudio-gems.gemspec does not exist: ${filepath}/openstudio-gems.gemspec"
exit 1
fi
local UPDATE_STRING="spec.add_dependency '$gem_name'"
sed -i -e "s|$UPDATE_STRING.*|$UPDATE_STRING, '>= 0'|g" ${filepath}/openstudio-gems.gemspec
}

# Main

# Find the location of the existing Gemfile based on the contents of the RUBYLIB env variable
echo $(which openstudio)
# You can't call openstudio here since it will load the Server's Gemfile
# echo $(openstudio openstudio_version)
for x in $(printenv RUBYLIB | tr ":" "\n")
do
if [[ $x =~ .*[Oo]pen[Ss]tudio-[0-9]*\.[0-9]*\.[0-9]*/Ruby ]]; then
GEMFILE_DIR=$x
continue
fi
done

echo "GEMFILE_DIR is set to $GEMFILE_DIR"
NEW_GEMFILE_DIR=/var/oscli
EXISTING_GEM=$1
NEW_GEM_REPO=$2
NEW_GEM_BRANCH=$3
GEMFILEUPDATE=$NEW_GEMFILE_DIR/analysis_$SCRIPT_ANALYSIS_ID.lock

# Determine the version of Bundler and make sure it is installed
if [ -e ${GEMFILE_DIR}/Gemfile.lock ]; then
LOCAL_BUNDLER_VERSION=$(tail -n 1 ${GEMFILE_DIR}/Gemfile.lock | tr -dc '[0-9.]')
echo "Installing Bundler version $LOCAL_BUNDLER_VERSION"
if [ -z $LOCAL_BUNDLER_VERSION ]; then
echo "Could not determine version of Bundler to use from Gemfile.lock"
fi
gem install bundler -v $LOCAL_BUNDLER_VERSION
else
echo "Could not find Gemfile.lock file in $GEMFILE_DIR"
exit 1
fi

# Verify the path of the required files
if [ ! -d "$GEMFILE_DIR" ]; then
echo "Directory for Gemfile does not exist: ${GEMFILE_DIR}"
exit 1
fi

if [ ! -f "$GEMFILE_DIR/Gemfile" ]; then
echo "Gemfile does not exist in: ${GEMFILE_DIR}"
exit 1
fi

if [ ! -f "$GEMFILE_DIR/openstudio-gems.gemspec" ]; then
echo "openstudio-gems.gemspec does not exist in: ${GEMFILE_DIR}"
echo "!!! This script only works with OpenStudio 2.8.0 and newer !!!"
exit 1
fi

# First check if there is a file that indicates the gem has already been updated.
# We only need to update the bundle once / worker, not every time a data point is initialized.
echo "Checking if Gemfile has been updated in ${GEMFILEUPDATE}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we do this at the start?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that would make more sense.

if [ -e $GEMFILEUPDATE ]; then
echo "***The gem bundle has already been updated"
exit 0
fi

# Modify the reference Gemfile and gemspec in place
mkdir -p $NEW_GEMFILE_DIR
cp $GEMFILE_DIR/Gemfile $NEW_GEMFILE_DIR
cp $GEMFILE_DIR/openstudio-gems.gemspec $NEW_GEMFILE_DIR

replace_gem_in_files $NEW_GEMFILE_DIR $EXISTING_GEM $NEW_GEM_REPO $NEW_GEM_BRANCH

# Pull the workflow gem from develop otherwise `require 'openstudio-workflow'` fails, supposedly
replace_gem_in_files $NEW_GEMFILE_DIR 'openstudio-workflow' 'NREL/openstudio-workflow-gem' 'develop'

# Show the modified Gemfile contents in the log
cd $NEW_GEMFILE_DIR
dos2unix $NEW_GEMFILE_DIR/Gemfile
dos2unix $NEW_GEMFILE_DIR/openstudio-gems.gemspec
echo "***Here are the modified Gemfile and openstudio-gems.gemspec files:"
cat $NEW_GEMFILE_DIR/Gemfile
cat $NEW_GEMFILE_DIR/openstudio-gems.gemspec

# Unset all BUNDLE, GEM, and RUBY environment variables before calling bundle install. These
# are required before re-bundling!
for evar in $(env | cut -d '=' -f 1 | grep ^BUNDLE); do unset $evar; done
for evar in $(env | cut -d '=' -f 1 | grep ^GEM); do unset $evar; done
for evar in $(env | cut -d '=' -f 1 | grep ^RUBY); do unset $evar; done
export RUBYLIB=$GEMFILE_DIR:/usr/Ruby:$RUBYLIB

# Update the specified gem in the bundle
echo "***Running bundle install with version $LOCAL_BUNDLER_VERSION in $(which bundle)"
if [ -f Gemfile.lock ]; then
rm Gemfile.lock
fi
bundle '_'$LOCAL_BUNDLER_VERSION'_' install --path gems

# Note that the bundle has been updated
echo >> $GEMFILEUPDATE
2 changes: 1 addition & 1 deletion server/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ group :development, :test do
gem 'rspec-rails'
gem 'rspec-retry'
gem 'ruby-prof', '~> 0.15'
gem 'selenium-webdriver'
gem 'selenium-webdriver', '3.141.0'

gem 'psych', '3.0.3'
gem 'rubocop', '0.64.0'
Expand Down
4 changes: 2 additions & 2 deletions server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ bin/delayed_job -i server stop && bin/delayed_job -i server --queue=analyses,bac
```bash
# Foreground - one terminal for each command
QUEUES=background,analyses bundle exec rake environment resque:work
COUNT=4 QUEUES=simulations bundle exec rake environment resque:workers
QUEUES=analysis_wrappers bundle exec rake environment resque:work
COUNT=2 QUEUES=simulations bundle exec rake environment resque:workers
```

### Running Simulations for development in the foreground
Expand All @@ -66,7 +66,7 @@ libraries already installed.

```bash
# from OpenStudio-server root directory
mkdir -p worker-nodes
mkdir -p worker-nodes/server/R
cd worker-nodes
docker run -it -v $(pwd):$(pwd) -p 6311:6311 nrel/openstudio-rserve
```
Expand Down
9 changes: 7 additions & 2 deletions server/app/jobs/dj_jobs/run_simulate_data_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@ def initialize(data_point_id, options = {})
# this is also run from resque job, which leverages perform code below.
# only queue data_point on initialize for delayed_jobs
@data_point.set_queued_state if Rails.application.config.job_manager == :delayed_job
end

def perform
# Create the analysis, simulation, and run directory
FileUtils.mkdir_p analysis_dir unless Dir.exist? analysis_dir
FileUtils.mkdir_p simulation_dir unless Dir.exist? simulation_dir
Expand All @@ -59,7 +57,9 @@ def perform

# Logger for the simulate datapoint
@sim_logger = Logger.new("#{simulation_dir}/#{@data_point.id}.log")
end

def perform
# Error if @datapoint doesn't exist
if @data_point.nil?
@sim_logger = 'Could not find datapoint; @datapoint was nil'
Expand Down Expand Up @@ -540,6 +540,11 @@ def run_script_with_args(script_name)
msg = "Error #{e.message} running #{script_name}: #{e.backtrace.join("\n")}"
@sim_logger.error msg
raise msg
ensure
# save the log information to the datapoint if it exists
if File.exist? log_path
@data_point.worker_logs[script_name] = File.read(log_path).lines
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i like this update. the logs are also downloaded w/data point zip file.

end
end
end
end
7 changes: 4 additions & 3 deletions server/app/lib/utility/oss.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ def self.oscli_bundle
#
# Why are these all class methods?
def self.resolve_env_vars(env_vars)
# List of items to keep as regex
keep_starts_with = [/^RUBY/, /^BUNDLE/, /^GEM/, /^RAILS_ENV/, /PATH/]
# List of items to keep as regex.
# 4/19/2019 Keep rbenv related env vars for when running locally
keep_starts_with = [/^RUBY/, /^BUNDLE/, /^GEM/, /^RAILS_ENV/, /PATH/, /^RBENV/]

new_env_vars = {}
ENV.each do |var, value|
Expand All @@ -43,7 +44,7 @@ def self.resolve_env_vars(env_vars)
end
end

# overwrite any custom env vars
# add and/or overwrite any custom env vars
env_vars.each do |var, value|
new_env_vars[var] = value
end
Expand Down
5 changes: 5 additions & 0 deletions server/app/models/analysis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class Analysis
field :status_message, type: String, default: '' # the resulting message from the analysis
field :output_variables, type: Array, default: [] # list of variable that are needed for output including objective functions
field :os_metadata # don't define type, keep this flexible
field :analysis_logs, type: Hash, default: {} # store the logs from the analysis init and finalize

# Temp location for these vas
field :samples, type: Integer
Expand Down Expand Up @@ -461,5 +462,9 @@ def run_script_with_args(script_name)
# RAILS_ROOT - location of rails
Utility::Oss.run_script(script_path, 4.hours, { 'SCRIPT_PATH' => dir_path, 'ANALYSIS_ID' => id, 'HOST_URL' => APP_CONFIG['os_server_host_url'], 'RAILS_ROOT' => Rails.root.to_s, 'ANALYSIS_DIRECTORY' => shared_directory_path }, args, logger, log_path)
end
ensure
if File.exist? log_path
analysis_logs[script_name] = File.read(log_path).lines
end
end
end
1 change: 1 addition & 0 deletions server/app/models/data_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class DataPoint
field :run_start_time, type: DateTime, default: nil
field :run_end_time, type: DateTime, default: nil
field :sdp_log_file, type: Array, default: []
field :worker_logs, type: Hash, default: {}

# Run location information
field :ip_address, type: String
Expand Down
2 changes: 0 additions & 2 deletions server/doc/README_FOR_APP

This file was deleted.

49 changes: 47 additions & 2 deletions server/spec/features/dj_run_simulation_data_point_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@

before do
# Look at DatabaseCleaner gem in the future to deal with this.
Project.destroy_all
Delayed::Job.destroy_all
begin
Project.destroy_all
Delayed::Job.destroy_all
rescue Errno::EACCES => e
puts "Cannot unlink files, will try and continue"
end

# I am no longer using this factory for this purpose. It doesn't
# link up everything, so just post the test using the Analysis Gem.
Expand Down Expand Up @@ -242,3 +246,44 @@
expect(a[3]).to eq '11_Job11'
end
end

RSpec.describe DjJobs::RunSimulateDataPoint, type: :feature, depends_resque: true do
before :each do
begin
Project.destroy_all
rescue Errno::EACCES => e
puts "Cannot unlink files, will try and continue"
end
FactoryBot.create(:project_with_analyses).analyses

@project = Project.first
@analysis = @project.analyses.first
@data_point = @analysis.data_points.first
end

it 'launches a script successfully' do
job = DjJobs::RunSimulateDataPoint.new(@data_point.id)

# copy over the test script to the directory
FileUtils.mkdir_p "#{job.send :analysis_dir}/scripts/data_point"
FileUtils.cp('spec/files/worker_init_test.sh', "#{job.send :analysis_dir}/scripts/data_point")
FileUtils.cp('spec/files/worker_init_test.args', "#{job.send :analysis_dir}/scripts/data_point")

# call the private method for testing purposes
job.send :run_script_with_args, 'worker_init_test'

# verify that a log file was created

log_file = "#{job.send :analysis_dir}/data_point_#{@data_point.id}/worker_init_test.log"
expect(File.exist? log_file).to eq true
if File.exist? log_file
file_contents = File.read(log_file)
expect(file_contents.include? 'argument number 1')
end

# verify that the init log is attached to the datapoint
# For some reason the worker_logs aren't working within the testing framework. They work in
# actual deployment. # TODO: Figure out why worker_logs don't show up for tests
puts @data_point.worker_logs.inspect
end
end
8 changes: 6 additions & 2 deletions server/spec/features/resque_run_batch_datapoints_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@

before do
# Look at DatabaseCleaner gem in the future to deal with this.
Project.destroy_all
Delayed::Job.destroy_all
begin
Project.destroy_all
Delayed::Job.destroy_all
rescue Errno::EACCES => e
puts "Cannot unlink files, will try and continue"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice!

end

Resque.workers.each(&:unregister_worker)
Resque.queues.each { |q| Resque.redis.del "queue:#{q}" }
Expand Down
Loading