FOSSology  4.4.0
Open Source License Compliance by Open Source Software
AgentPlugin.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Plugin;
9 
10 abstract class AgentPlugin implements Plugin
11 {
12  const PRE_JOB_QUEUE = 'preJq';
13 
14  public $AgentName;
15  public $Name = "agent_abstract";
16  public $Dependency = array();
17  public $Title = 'how to show checkbox';
18  public $PluginLevel = 10;
19  public $State = PLUGIN_STATE_READY;
20  public $DBaccess = PLUGIN_DB_WRITE;
21 
22  function __construct()
23  {
24  }
25  function execute()
26  {
27  }
28  function postInstall()
29  {
30  }
31 
32  function preInstall()
33  {
34  menu_insert("Agents::" . $this->Title, 0, $this->Name);
35  }
36 
37  function unInstall()
38  {
39  }
40 
44  function getName()
45  {
46  return $this->Name;
47  }
48 
56  public function AgentHasResults($uploadId=0)
57  {
58  return 0;
59  }
60 
72  public function AgentAdd($jobId, $uploadId, &$errorMsg, $dependencies=array(), $arguments=null)
73  {
74  $dependencies[] = "agent_adj2nest";
75  if ($this->AgentHasResults($uploadId) == 1) {
76  return 0;
77  }
78 
79  $jobQueueId = \IsAlreadyScheduled($jobId, $this->AgentName, $uploadId);
80  if ($jobQueueId != 0) {
81  return $jobQueueId;
82  }
83 
84  $args = is_array($arguments) ? '' : $arguments;
85  return $this->doAgentAdd($jobId, $uploadId, $errorMsg, $dependencies, $uploadId, $args);
86  }
87 
100  protected function doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies, $jqargs = "", $jq_cmd_args = null)
101  {
102  $deps = array();
103  foreach ($dependencies as $dependency) {
104  $dep = $this->implicitAgentAdd($jobId, $uploadId, $errorMsg, $dependency);
105  if ($dep == - 1) {
106  return -1;
107  }
108  $deps[] = $dep;
109  }
110 
111  if (empty($jqargs)) {
112  $jqargs = $uploadId;
113  }
114  $jobQueueId = \JobQueueAdd($jobId, $this->AgentName, $jqargs, "", $deps, NULL, $jq_cmd_args);
115  if (empty($jobQueueId)) {
116  $errorMsg = "Failed to insert agent $this->AgentName into job queue. jqargs: $jqargs";
117  return -1;
118  }
119  $success = \fo_communicate_with_scheduler("database", $output, $errorMsg);
120  if (! $success) {
121  $errorMsg .= "\n" . $output;
122  }
123 
124  return $jobQueueId;
125  }
126 
134  protected function implicitAgentAdd($jobId, $uploadId, &$errorMsg, $dependency)
135  {
136  if (is_array($dependency)) {
137  $pluginName = $dependency['name'];
138  $depArgs = array_key_exists('args', $dependency) ? $dependency['args'] : null;
139  $preJq = array_key_exists(self::PRE_JOB_QUEUE, $dependency) ? $dependency[self::PRE_JOB_QUEUE] : array();
140  } else {
141  $pluginName = $dependency;
142  $depArgs = null;
143  $preJq = array();
144  }
145  $depPlugin = plugin_find($pluginName);
146  if (! $depPlugin) {
147  $errorMsg = "Invalid plugin name: $pluginName, (implicitAgentAdd())";
148  return -1;
149  }
150 
151  return $depPlugin->AgentAdd($jobId, $uploadId, $errorMsg, $preJq, $depArgs);
152  }
153 
154  function __toString()
155  {
156  return getStringRepresentation(get_object_vars($this), get_class($this));
157  }
158 }
doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies, $jqargs="", $jq_cmd_args=null)
AgentAdd($jobId, $uploadId, &$errorMsg, $dependencies=array(), $arguments=null)
Definition: AgentPlugin.php:72
implicitAgentAdd($jobId, $uploadId, &$errorMsg, $dependency)
JobQueueAdd($job_pk, $jq_type, $jq_args, $jq_runonpfile, $Depends, $host=NULL, $jq_cmd_args=NULL)
Insert a jobqueue + jobdepends records.
Definition: common-job.php:157
IsAlreadyScheduled($job_pk, $AgentName, $upload_pk)
Check if an agent is already scheduled in a job.
Definition: common-job.php:378
menu_insert($Path, $LastOrder=0, $URI=NULL, $Title=NULL, $Target=NULL, $HTML=NULL)
Given a Path, order level for the last item, and optional plugin name, insert the menu item.
getStringRepresentation($vars, $classname)
plugin_find($pluginName)
Given the official name of a plugin, return the $Plugins object.
fo_communicate_with_scheduler($input, &$output, &$error_msg)
Communicate with scheduler, send commands to the scheduler, then get the output.
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:38