FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
AgentPlugin.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2015, Siemens AG
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * version 2 as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  ***********************************************************/
18 
19 namespace Fossology\Lib\Plugin;
20 
21 abstract class AgentPlugin implements Plugin
22 {
23  const PRE_JOB_QUEUE = 'preJq';
24 
25  public $AgentName;
26  public $Name = "agent_abstract";
27  public $Dependency = array();
28  public $Title = 'how to show checkbox';
29  public $PluginLevel = 10;
30  public $State = PLUGIN_STATE_READY;
31  public $DBaccess = PLUGIN_DB_WRITE;
32 
33  function __construct()
34  {
35  }
36  function execute()
37  {
38  }
39  function postInstall()
40  {
41  }
42 
43  function preInstall()
44  {
45  menu_insert("Agents::" . $this->Title, 0, $this->Name);
46  }
47 
48  function unInstall()
49  {
50  }
51 
55  function getName()
56  {
57  return $this->Name;
58  }
59 
67  public function AgentHasResults($uploadId=0)
68  {
69  return 0;
70  }
71 
83  public function AgentAdd($jobId, $uploadId, &$errorMsg, $dependencies=array(), $arguments=null)
84  {
85  $dependencies[] = "agent_adj2nest";
86  if ($this->AgentHasResults($uploadId) == 1) {
87  return 0;
88  }
89 
90  $jobQueueId = \IsAlreadyScheduled($jobId, $this->AgentName, $uploadId);
91  if ($jobQueueId != 0) {
92  return $jobQueueId;
93  }
94 
95  $args = is_array($arguments) ? '' : $arguments;
96  return $this->doAgentAdd($jobId, $uploadId, $errorMsg, $dependencies, $uploadId, $args);
97  }
98 
110  protected function doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies, $jqargs = "", $jq_cmd_args = null)
111  {
112  $deps = array();
113  foreach ($dependencies as $dependency) {
114  $dep = $this->implicitAgentAdd($jobId, $uploadId, $errorMsg, $dependency);
115  if ($dep == - 1) {
116  return -1;
117  }
118  $deps[] = $dep;
119  }
120 
121  if (empty($jqargs)) {
122  $jqargs = $uploadId;
123  }
124  $jobQueueId = \JobQueueAdd($jobId, $this->AgentName, $jqargs, "", $deps, NULL, $jq_cmd_args);
125  if (empty($jobQueueId)) {
126  $errorMsg = "Failed to insert agent $this->AgentName into job queue. jqargs: $jqargs";
127  return -1;
128  }
129  $success = \fo_communicate_with_scheduler("database", $output, $errorMsg);
130  if (! $success) {
131  $errorMsg .= "\n" . $output;
132  }
133 
134  return $jobQueueId;
135  }
136 
144  protected function implicitAgentAdd($jobId, $uploadId, &$errorMsg, $dependency)
145  {
146  if (is_array($dependency)) {
147  $pluginName = $dependency['name'];
148  $depArgs = array_key_exists('args', $dependency) ? $dependency['args'] : null;
149  $preJq = array_key_exists(self::PRE_JOB_QUEUE, $dependency) ? $dependency[self::PRE_JOB_QUEUE] : array();
150  } else {
151  $pluginName = $dependency;
152  $depArgs = null;
153  $preJq = array();
154  }
155  $depPlugin = plugin_find($pluginName);
156  if (! $depPlugin) {
157  $errorMsg = "Invalid plugin name: $pluginName, (implicitAgentAdd())";
158  return -1;
159  }
160 
161  return $depPlugin->AgentAdd($jobId, $uploadId, $errorMsg, $preJq, $depArgs);
162  }
163 
164  function __toString()
165  {
166  return getStringRepresentation(get_object_vars($this), get_class($this));
167  }
168 }
IsAlreadyScheduled($job_pk, $AgentName, $upload_pk)
Check if an agent is already scheduled in a job.
Definition: common-job.php:380
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:159
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.
AgentAdd($jobId, $uploadId, &$errorMsg, $dependencies=array(), $arguments=null)
Definition: AgentPlugin.php:83
#define PLUGIN_DB_WRITE
Plugin requires write permission on DB.
Definition: libfossology.h:50
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...
doAgentAdd($jobId, $uploadId, &$errorMsg, $dependencies, $jqargs="", $jq_cmd_args=null)