Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[arvados.git] / apps / workbench / app / models / job.rb
index e716df75f9105bcecfeb61618465343e33d73019..7c55d9e857cfa89a9c448d3c77ccda829b747449 100644 (file)
@@ -1,2 +1,63 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 class Job < ArvadosBase
+  def self.goes_in_projects?
+    true
+  end
+
+  def content_summary
+    "#{script} job"
+  end
+
+  def editable_attributes
+    %w(description)
+  end
+
+  def default_name
+    if script
+      x = "\"#{script}\" job"
+    else
+      x = super
+    end
+    if finished_at
+      x += " finished #{finished_at.strftime('%b %-d')}"
+    elsif started_at
+      x += " started #{started_at.strftime('%b %-d')}"
+    elsif created_at
+      x += " submitted #{created_at.strftime('%b %-d')}"
+    end
+  end
+
+  def cancel
+    arvados_api_client.api "jobs/#{self.uuid}/", "cancel", {"cascade" => true}
+  end
+
+  def self.queue_size
+    arvados_api_client.api("jobs/", "queue_size", {"_method"=> "GET"})[:queue_size] rescue 0
+  end
+
+  def self.queue
+    arvados_api_client.unpack_api_response arvados_api_client.api("jobs/", "queue", {"_method"=> "GET"})
+  end
+
+  def textile_attributes
+    [ 'description' ]
+  end
+
+  def stderr_log_query(limit=nil)
+    query = Log.where(object_uuid: self.uuid).order("created_at DESC").with_count('none')
+    query = query.limit(limit) if limit
+    query
+  end
+
+  def stderr_log_lines(limit=2000)
+    stderr_log_query(limit).results.reverse.
+      flat_map { |log| log.properties[:text].split("\n") rescue [] }
+  end
+
+  def work_unit(label=nil)
+    JobWorkUnit.new(self, label, self.uuid)
+  end
 end