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 use Symfony\Component\HttpFoundation\Request;
11 
12 abstract class AgentPlugin implements Plugin
13 {
14  const PRE_JOB_QUEUE = 'preJq';
15 
16  public $AgentName;
17  public $Name = "agent_abstract";
18  public $Dependency = array();
19  public $Title = 'how to show checkbox';
20  public $PluginLevel = 10;
21  public $State = PLUGIN_STATE_READY;
22  public $DBaccess = PLUGIN_DB_WRITE;
23 
24  function __construct()
25  {
26  }
27  function execute()
28  {
29  }
30  function postInstall()
31  {
32  }
33 
34  function preInstall()
35  {
36  menu_insert("Agents::" . $this->Title, 0, $this->Name);
37  }
38 
39  function unInstall()
40  {
41  }
42 
46  function getName()
47  {
48  return $this->Name;
49  }
50 
58  public function AgentHasResults($uploadId=0)
59  {
60  return 0;
61  }
62 
76  public function AgentAdd($jobId, $uploadId, &$errorMsg, $dependencies=[],
77  $arguments=null, $request=null, $unpackArgs=null)
78  {
79  $dependencies[] = "agent_adj2nest";
80  if ($this->AgentHasResults($uploadId) == 1) {
81  return 0;
82  }
83 
84  $jobQueueId = \IsAlreadyScheduled($jobId, $this->AgentName, $uploadId);
85  if ($jobQueueId != 0) {
86  return $jobQueueId;
87  }
88 
89  $args = is_array($arguments) ? '' : $arguments;
90  return $this->doAgentAdd($jobId, $uploadId, $errorMsg, $dependencies,
91  $uploadId, $args, $request);
92  }
93 
107  protected function doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies,
108  $jqargs = "", $jq_cmd_args = null, $request = null)
109  {
110  $deps = array();
111  foreach ($dependencies as $dependency) {
112  $dep = $this->implicitAgentAdd($jobId, $uploadId, $errorMsg,
113  $dependency, $request);
114  if ($dep == - 1) {
115  return -1;
116  }
117  $deps[] = $dep;
118  }
119 
120  if (empty($jqargs)) {
121  $jqargs = $uploadId;
122  }
123  $jobQueueId = \JobQueueAdd($jobId, $this->AgentName, $jqargs, "", $deps, NULL, $jq_cmd_args);
124  if (empty($jobQueueId)) {
125  $errorMsg = "Failed to insert agent $this->AgentName into job queue. jqargs: $jqargs";
126  return -1;
127  }
128  $success = \fo_communicate_with_scheduler("database", $output, $errorMsg);
129  if (! $success) {
130  $errorMsg .= "\n" . $output;
131  }
132 
133  return $jobQueueId;
134  }
135 
144  protected function implicitAgentAdd($jobId, $uploadId, &$errorMsg,
145  $dependency, $request)
146  {
147  if (is_array($dependency)) {
148  $pluginName = $dependency['name'];
149  $depArgs = array_key_exists('args', $dependency) ? $dependency['args'] : null;
150  $preJq = array_key_exists(self::PRE_JOB_QUEUE, $dependency) ? $dependency[self::PRE_JOB_QUEUE] : array();
151  } else {
152  $pluginName = $dependency;
153  $depArgs = null;
154  $preJq = array();
155  }
156  $depPlugin = plugin_find($pluginName);
157  if (! $depPlugin) {
158  $errorMsg = "Invalid plugin name: $pluginName, (implicitAgentAdd())";
159  return -1;
160  }
161 
162  return $depPlugin->AgentAdd($jobId, $uploadId, $errorMsg, $preJq,
163  $depArgs, $request);
164  }
165 
166  function __toString()
167  {
168  return getStringRepresentation(get_object_vars($this), get_class($this));
169  }
170 }
AgentAdd($jobId, $uploadId, &$errorMsg, $dependencies=[], $arguments=null, $request=null, $unpackArgs=null)
Definition: AgentPlugin.php:76
doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies, $jqargs="", $jq_cmd_args=null, $request=null)
implicitAgentAdd($jobId, $uploadId, &$errorMsg, $dependency, $request)
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