Skip to content

Commit

Permalink
Pm job management (#3899)
Browse files Browse the repository at this point in the history
Add the ability to delete jobs from the job log as well as the ability to
stop a running/queued job.
  • Loading branch information
ashton22305 authored Nov 15, 2024
1 parent 918ae09 commit 6fa9a84
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 9 deletions.
49 changes: 48 additions & 1 deletion apps/dashboard/app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,54 @@ def job_details

hpc_job = project.job(job_details_params[:jobid].to_s, cluster_str)

render(partial: 'job_details', locals: { job: hpc_job })
@project = project

render(partial: 'job_details', locals: { job: hpc_job, project: @project })
end

# DELETE /projects/:project_id/jobs/:cluster/:jobid
def delete_job
@project = Project.find(job_details_params[:project_id])

cluster_str = job_details_params[:cluster].to_s

jobid = job_details_params[:jobid]

if @project.remove_logged_job(jobid.to_s, cluster_str)
redirect_to(
project_path(job_details_params[:project_id]),
notice: I18n.t('dashboard.jobs_project_job_deleted', job_id: jobid)
)
else
redirect_to(
project_path(job_details_params[:project_id]),
alert: I18n.t('dashboard.jobs_project_job_not_deleted', jobid: jobid)
)
end
end

# POST /projects/:project_id/jobs/:cluster/:jobid/stop
def stop_job
@project = Project.find(job_details_params[:project_id])
cluster_str = job_details_params[:cluster].to_s
cluster = OodAppkit.clusters[cluster_str.to_sym]

jobid = job_details_params[:jobid]

hpc_job = @project.job(jobid.to_s, cluster_str)

begin
cluster.job_adapter.delete(jobid.to_s) unless hpc_job.status.to_s == 'completed'
redirect_to(
project_path(job_details_params[:project_id]),
notice: I18n.t('dashboard.jobs_project_job_stopped', job_id: jobid)
)
rescue StandardError => e
redirect_to(
project_path(job_details_params[:project_id]),
alert: I18n.t('dashboard.jobs_project_generic_error', error: e.message.to_s)
)
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion apps/dashboard/app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def status_text(status)
'Running'
when 'queued'
'Queued'
when 'qued_held'
when 'queued_held'
'Hold'
when 'suspended'
'Suspend'
Expand Down
19 changes: 18 additions & 1 deletion apps/dashboard/app/helpers/projects_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,21 @@ def render_readme(readme_location)
simple_format(file_content)
end
end
end

def job_details_buttons(status, job, project)
locals = { project_id: project.id, id: job.id, cluster: job.cluster }
button_partial = button_category(status)
render(partial: "projects/buttons/#{button_category(status)}_buttons", locals: locals) unless button_partial.nil?
end

def button_category(status)
case status
when 'queued_held'
'held'
when 'suspended'
'held'
else
status
end
end
end
13 changes: 13 additions & 0 deletions apps/dashboard/app/models/concerns/job_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ def upsert_job!(directory, job)
JobLoggerHelper.write_log(directory, new_jobs)
end

def delete_job!(directory, job)
existing_jobs = jobs(directory)
stored_job = existing_jobs.detect { |j| j.id == job.id && j.cluster == job.cluster }

return if stored_job.nil?

new_jobs = existing_jobs.map(&:to_h)
idx = existing_jobs.index(stored_job)
new_jobs.delete_at(idx)

JobLoggerHelper.write_log(directory, new_jobs)
end

# def write_job_log!(directory, jobs)
# JobLoggerHelper.write_log!(directory, jobs)
# endd
Expand Down
9 changes: 8 additions & 1 deletion apps/dashboard/app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,13 @@ def job(job_id, cluster)
job
end

def remove_logged_job(job_id, cluster)
old_job = jobs.detect { |j| j.id == job_id && j.cluster == cluster }
Project.delete_job!(directory, old_job)

jobs.none? { |j| j.id == job_id && j.cluster == cluster }
end

def adapter(cluster_id)
cluster = OodAppkit.clusters[cluster_id] || raise(StandardError, "Job specifies nonexistent '#{cluster_id}' cluster id.")
cluster.job_adapter
Expand All @@ -230,7 +237,7 @@ def readme_path
end

private

def update_attrs(attributes)
[:name, :description, :icon].each do |attribute|
instance_variable_set("@#{attribute}".to_sym, attributes.fetch(attribute, ''))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@

<turbo-stream action="replace" target="<%= id %>" data-job-status="<%= job.status %>">
<template>
<%= render(partial: 'job_details_content', locals: { job: job }, :formats=>[:html]) %>
<%= render(partial: 'job_details_content', locals: { job: job, project: project }, :formats=>[:html]) %>
</template>
</turbo-stream>
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
</tr>
<% end %>
</table>
<div class="d-flex flex-row-reverse mt-2 mx-2">
<%= job_details_buttons(job.status.to_s, job, project) %>
</div>
</div>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= button_to(
'Delete',
project_delete_job_path(project_id: project_id, cluster: cluster, jobid: id),
method: :delete,
data: { confirm: t('dashboard.batch_connect_sessions_delete_confirm') },
class: 'btn btn-danger'
) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= button_to(
'Stop',
project_stop_job_path(project_id: project_id, cluster: cluster, jobid: id),
method: :post,
data: { confirm: t('dashboard.batch_connect_sessions_delete_confirm') },
class: 'btn btn-danger'
) %>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<%= button_to(
'Stop',
project_stop_job_path(project_id: project_id, cluster: cluster, jobid: id),
method: :post,
data: { confirm: t('dashboard.batch_connect_sessions_delete_confirm') },
class: 'btn btn-danger'
) %>
6 changes: 3 additions & 3 deletions apps/dashboard/app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,16 @@

<div class="col-md-8">
<div class="row">
<h2 class="d-flex justify-content-center">Active Jobs</h2>
<%= render(partial: 'job_details', collection: @project.active_jobs, as: :job) %>
<h2 class="h3 d-flex justify-content-center">Active Jobs</h2>
<%= render(partial: 'job_details', collection: @project.active_jobs, as: :job, locals: { project: @project }) %>
</div>

<div class="row">
<h2 class="h3 d-flex justify-content-center">Completed Jobs</h2>
<div id="completed_jobs" class="row">
<%- @project.completed_jobs.each do |job| -%>
<div class="col-md-4" id="<%= "job_#{job.cluster}_#{job.id}" %>">
<%= render(partial: 'job_details_content', locals: { job: job }) %>
<%= render(partial: 'job_details_content', locals: { job: job, project: @project }) %>
</div>
<%- end -%>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<!-- Modal -->
<div
class="modal fade h-75"
class="modal fade"
id="<%= path_selector_id %>" tabindex="-1" role="dialog"
aria-labelledby="modal-path-selector" aria-hidden="true"
data-show-files="<%= show_files %>"
Expand Down
4 changes: 4 additions & 0 deletions apps/dashboard/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,10 @@ en:
jobs_project_invalid_configuration_clusters: "An HPC cluster is required. Contact your administrator to add one to the system."
jobs_project_invalid_configuration_scripts: "An executable script is required for your project. Upload a script using the file application."

jobs_project_job_stopped: "Successfully stopped job %{job_id}"
jobs_project_job_deleted: "Successfully deleted job %{job_id}"
jobs_project_job_not_deleted: "Cannot delete job %{job_id}"

jobs_scripts_created: "Script successfully created!"
jobs_scripts_default_created: "A 'hello_world.sh' was also added to this project."
jobs_scripts_updated: "Script manifest updated!"
Expand Down
2 changes: 2 additions & 0 deletions apps/dashboard/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
resources :projects do
root 'projects#index'
get '/jobs/:cluster/:jobid' => 'projects#job_details', :defaults => { :format => 'turbo_stream' }, :as => 'job_details'
delete '/jobs/:cluster/:jobid' => 'projects#delete_job', :as => 'delete_job'
post '/jobs/:cluster/:jobid/stop' => 'projects#stop_job', :as => 'stop_job'

resources :launchers do
post 'submit', on: :member
Expand Down

0 comments on commit 6fa9a84

Please sign in to comment.