Skip to content

Commit

Permalink
Merge pull request #789 from NREL/soft-stop
Browse files Browse the repository at this point in the history
Add Soft Stop Analysis capability
  • Loading branch information
brianlball authored Aug 25, 2024
2 parents ca705ea + 3fc630d commit f518a2d
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 2 deletions.
22 changes: 22 additions & 0 deletions server/app/controllers/analyses_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ def destroy
# stop analysis button action
def stop
@analysis = Analysis.find(params[:id])
if @analysis.nil?
logger.error "Analysis with ID #{params[:id]} not found."
redirect_to analyses_path, alert: 'Analysis not found.' and return
end
res = @analysis.stop_analysis

respond_to do |format|
Expand All @@ -208,6 +212,24 @@ def stop
end
end
end

# stop analysis button action
def soft_stop
@analysis = Analysis.find(params[:id])
if @analysis.nil?
logger.error "Analysis with ID #{params[:id]} not found."
redirect_to analyses_path, alert: 'Analysis not found.' and return
end
res = @analysis.soft_stop_analysis

respond_to do |format|
if res[0]
format.html { redirect_to @analysis, notice: 'Analysis flag changed to stop. Will NOT wait until the last submitted run finishes before killing.' }
else
format.html { redirect_to @analysis, notice: 'Analysis flag did NOT change.' }
end
end
end

# Controller for submitting the action via post. This right now only works with the API
# and will only return a JSON response based on whether or not the analysis has been
Expand Down
20 changes: 20 additions & 0 deletions server/app/models/analysis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,26 @@ def stop_analysis
[save!, errors]
end

def soft_stop_analysis
Rails.logger.info('attempting to stop analysis')

self.run_flag = false

jobs.each do |j|
unless j.status == 'completed'
j.status = 'completed'
j.end_time = Time.new
j.status_message = 'datapoint canceled'
j.save!
end
end

# Remove all the queued background jobs for this analysis
data_points.where(status: 'queued').each(&:set_soft_canceled_state)

[save!, errors]
end

# Method that pulls out the variables from the uploaded problem/analysis JSON.
def pull_out_os_variables
pat_json = false
Expand Down
10 changes: 10 additions & 0 deletions server/app/models/data_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,16 @@ def set_canceled_state
self.status_message = 'datapoint canceled'
save!
end

def set_soft_canceled_state
Rails.logger.debug "data_point.set_soft_canceled_state"
#destroy_background_job # destroy queued job
self.run_start_time ||= Time.now
self.run_end_time = Time.now
self.status = :completed
self.status_message = 'datapoint canceled'
save!
end

def set_queued_state
Rails.logger.debug "data_point.set_queued_state"
Expand Down
5 changes: 3 additions & 2 deletions server/app/views/analyses/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
<th>Status</th>
<td><%= @analysis.status %>
<% if @analysis.status == 'started' %>
| <%= link_to("Stop Analysis", stop_analysis_path(@analysis)) %>
<% end %>
| <%= link_to("Stop Analysis", stop_analysis_path(@analysis), onclick: "return confirm('Are you sure you want to stop the analysis?')") %>
| <%= link_to("Soft Stop", soft_stop_analysis_path(@analysis), onclick: "return confirm('Are you sure you want to stop the analysis?')") %>
<% end %>
</td>
</tr>
<tr>
Expand Down
2 changes: 2 additions & 0 deletions server/app/views/pages/dashboard.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<div class="button-container">
<% if @current.status == 'started' %>
<td><%= link_to 'Stop Analysis', stop_analysis_path(@current), data: { confirm: 'Are you sure?' }, class: "btn btn-mini" %></td>
<td><%= link_to 'Soft Stop Analysis', soft_stop_analysis_path(@current), data: { confirm: 'Are you sure?' }, class: "btn btn-mini" %></td>
<% else %>
<td><%= link_to 'Delete Project', project_path(@current.project), method: :delete, data: {confirm: 'Are you sure?'}, class: "btn btn-mini" %></td>
<% end %>
Expand Down Expand Up @@ -125,6 +126,7 @@
<div class="button-container">
<% if res.status == 'started' %>
<td><%= link_to 'Stop Analysis', stop_analysis_path(res.project), data: { confirm: 'Are you sure?' }, class: "btn btn-mini" %></td>
<td><%= link_to 'Soft Stop Analysis', soft_stop_analysis_path(res.project), data: { confirm: 'Are you sure?' }, class: "btn btn-mini" %></td>
<% else %>
<td><%= link_to 'Delete Project', project_path(res.project), method: :delete, data: {confirm: 'Are you sure?'}, class: "btn btn-mini" %></td>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions server/app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<% logger.warn("WTH - returns '#{project.analyses.first}'") %>
<% if project.analyses.first.status == 'started' %>
<td><%= link_to 'Stop', stop_analysis_path(project.analyses.first), data: { confirm: 'Are you sure?' } %></td>
<td><%= link_to 'Soft Stop', soft_stop_analysis_path(project.analyses.first), data: { confirm: 'Are you sure?' } %></td>
<% else %>
<td><%= link_to 'Delete', project, method: :delete, data: { confirm: 'Are you sure?' } %></td>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions server/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
post :action
post :upload
get :stop
get :soft_stop
get :status
get :page_data
get :analysis_data
Expand Down

0 comments on commit f518a2d

Please sign in to comment.