FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
common-job.php
Go to the documentation of this file.
1 <?php
2 
6 /***********************************************************
7  Copyright (C) 2008-2015 Hewlett-Packard Development Company, L.P.
8  Copyright (C) 2015,2018 Siemens AG
9 
10  This library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU Lesser General Public
12  License version 2.1 as published by the Free Software Foundation.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with this library; if not, write to the Free Software Foundation, Inc.0
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  ***********************************************************/
66 function JobAddUpload($userId, $groupId, $job_name, $filename, $desc, $UploadMode, $folder_pk, $public_perm=Auth::PERM_NONE)
67 {
68  global $container;
69 
70  $dbManager = $container->get('db.manager');
71  /* check all required inputs */
72  if (empty($userId) || empty($job_name) || empty($filename) ||
73  empty($UploadMode) || empty($folder_pk)) {
74  return;
75  }
76 
77  $row = $dbManager->getSingleRow("INSERT INTO upload
78  (upload_desc,upload_filename,user_fk,upload_mode,upload_origin,public_perm) VALUES ($1,$2,$3,$4,$5,$6) RETURNING upload_pk",
79  array($desc,$job_name,$userId,$UploadMode,$filename, $public_perm),__METHOD__.'.insert.upload');
80  $uploadId = $row['upload_pk'];
81 
82  $dbManager->getSingleRow("INSERT INTO foldercontents (parent_fk,foldercontents_mode,child_id) VALUES ($1,$2,$3)",
83  array($folder_pk,FolderDao::MODE_UPLOAD,$uploadId),'insert.foldercontents');
84 
88  if (empty($groupId)) {
89  $usersRow = $dbManager->getSingleRow('SELECT * FROM users WHERE user_pk=$1',
90  array($userId), __METHOD__.'.select.user');
91  $groupId = $usersRow['group_fk'];
92  }
93  $perm_admin = Auth::PERM_ADMIN;
94 
95  $dbManager->getSingleRow("INSERT INTO perm_upload (perm, upload_fk, group_fk) VALUES ($1,$2,$3)",
96  array($perm_admin, $uploadId, $groupId),'insert.perm_upload');
97 
98  return ($uploadId);
99 }
100 
101 
113 function JobAddJob($userId, $groupId, $job_name, $upload_pk=0, $priority=0)
114 {
115  global $container;
116 
118  $dbManager = $container->get('db.manager');
119 
120  $upload_pk_val = empty($upload_pk) ? null : $upload_pk;
121 
122  $params = array($userId, $priority, $job_name, $upload_pk_val);
123  $stmtName = __METHOD__;
124  if (empty($groupId)) {
125  $stmtName .= "noGrp";
126  $groupPkVal = "(SELECT group_fk FROM users WHERE user_pk = $1)";
127  } else {
128  $params[] = $groupId;
129  $groupPkVal = "$". count($params);
130  }
131 
132  $row = $dbManager->getSingleRow(
133  "INSERT INTO job
134  (job_user_fk,job_group_fk,job_queued,job_priority,job_name,job_upload_fk) VALUES
135  ($1,$groupPkVal,now(),$2,$3,$4) RETURNING job_pk",
136  $params,
137  $stmtName
138  );
139 
140  return intval($row['job_pk']);
141 } // JobAddJob()
142 
143 
159 function JobQueueAdd($job_pk, $jq_type, $jq_args, $jq_runonpfile, $Depends, $host = NULL, $jq_cmd_args=NULL)
160 {
161  global $PG_CONN;
162  $jq_args = pg_escape_string($jq_args);
163  $jq_cmd_args = pg_escape_string($jq_cmd_args);
164 
165  /* Make sure all dependencies exist */
166  if (is_array($Depends)) {
167  foreach ($Depends as $Dependency) {
168  if (empty($Dependency)) {
169  continue;
170  }
171 
172  $sql = "SELECT jq_pk FROM jobqueue WHERE jq_pk = '$Dependency'";
173  $result = pg_query($PG_CONN, $sql);
174  DBCheckResult($result, $sql, __FILE__, __LINE__);
175  $MissingDep = (pg_num_rows($result) == 0);
176  pg_free_result($result);
177 
178  if ($MissingDep) {
179  return;
180  }
181  }
182  }
183 
184  $sqlBegin = "BEGIN";
185  $result = pg_query($PG_CONN, $sqlBegin);
186  DBCheckResult($result, $sqlBegin, __FILE__, __LINE__);
187  pg_free_result($result);
188 
189  /* Add the job */
190  $sql = "INSERT INTO jobqueue ";
191  $sql.= "(jq_job_fk,jq_type,jq_args,jq_runonpfile,jq_starttime,jq_endtime,jq_end_bits,jq_host,jq_cmd_args) VALUES ";
192  $sql.= "('$job_pk','$jq_type','$jq_args',";
193  $sql .= (empty($jq_runonpfile)) ? "NULL" : "'$jq_runonpfile'";
194  $sql.= ",NULL,NULL,0,";
195  $sql .= $host ? "'$host'," : "NULL,";
196  $sql .= $jq_cmd_args ? "'$jq_cmd_args')" : "NULL)";
197 
198  $result = pg_query($PG_CONN, $sql);
199  DBCheckResult($result, $sql, __FILE__, __LINE__);
200  pg_free_result($result);
201 
202  /* Find the jobqueue that was just added */
203  $jq_pk = GetLastSeq("jobqueue_jq_pk_seq", "jobqueue");
204  if (empty($jq_pk)) {
205  $sql = "ROLLBACK";
206  $result = pg_query($PG_CONN, $sql);
207  DBCheckResult($result, $sql, __FILE__, __LINE__);
208  pg_free_result($result);
209  return;
210  }
211 
212  /* Add dependencies */
213  if (is_array($Depends)) {
214  foreach ($Depends as $Dependency) {
215  if (empty($Dependency)) {
216  continue;
217  }
218  $sql = "INSERT INTO jobdepends (jdep_jq_fk,jdep_jq_depends_fk) VALUES ('$jq_pk','$Dependency')";
219  $result = pg_query($PG_CONN, $sql);
220  DBCheckResult($result, $sql, __FILE__, __LINE__);
221  pg_free_result($result);
222  }
223  }
224 
225  /* Commit the jobqueue and jobdepends changes */
226  $sql = "COMMIT";
227  $result = pg_query($PG_CONN, $sql);
228  DBCheckResult($result, $sql, __FILE__, __LINE__);
229  pg_free_result($result);
230 
231  return $jq_pk;
232 } // JobQueueAdd()
233 
234 
246 function GetJobList($status)
247 {
248  /* Gets the list of jobqueue records with the requested $status */
249  global $PG_CONN;
250  if (empty($status)) {
251  return;
252  }
253  $sql = "SELECT jq_pk FROM jobqueue WHERE jq_endtext like '%$status%' order by jq_pk;";
254  $result = pg_query($PG_CONN, $sql);
255  DBCheckResult($result, $sql, __FILE__, __LINE__);
256  $job_array = pg_fetch_all_columns($result, 0);
257  pg_free_result($result);
258  return $job_array;
259 }
260 
270 function QueueUploadsOnAgents($upload_pk_list, $agent_list, $Verbose)
271 {
272  global $Plugins;
273  global $PG_CONN;
274 
275  /* Get the users.default_bucketpool_fk */
276  $user_pk = Auth::getUserId();
277  $group_pk = Auth::getGroupId();
278 
279  if (empty($upload_pk_list)) {
280  return;
281  }
282  // Schedule them
283  $agent_count = count($agent_list);
284  foreach (explode(",", $upload_pk_list) as $upload_pk) {
285  if (empty($upload_pk)) {
286  continue;
287  }
288 
289  // Create a job for the upload
290  $where = "where upload_pk ='$upload_pk'";
291  $UploadRec = GetSingleRec("upload", $where);
292  if (empty($UploadRec)) {
293  echo "ERROR: unknown upload_pk: $upload_pk\n";
294  continue;
295  }
296 
297  $ShortName = $UploadRec['upload_filename'];
298 
299  /* Create Job */
300  $job_pk = JobAddJob($user_pk, $group_pk, $ShortName, $upload_pk);
301 
302  // don't exit on AgentAdd failure, or all the agents requested will
303  // not get scheduled.
304  for ($ac = 0; $ac < $agent_count; $ac ++) {
305  $agentname = $agent_list[$ac]->URI;
306  if (! empty($agentname)) {
307  $Agent = & $Plugins[plugin_find_id($agentname)];
308  $Dependencies = "";
309  $ErrorMsg = "already queued!";
310  $agent_jq_pk = $Agent->AgentAdd($job_pk, $upload_pk, $ErrorMsg,
311  $Dependencies);
312  if ($agent_jq_pk <= 0) {
313  echo "WARNING: Scheduling failed for Agent $agentname, upload_pk is: $upload_pk, job_pk is:$job_pk\n";
314  echo "WARNING message: $ErrorMsg\n";
315  } else if ($Verbose) {
316  $SQL = "SELECT upload_filename FROM upload where upload_pk = $upload_pk";
317  $result = pg_query($PG_CONN, $SQL);
318  DBCheckResult($result, $SQL, __FILE__, __LINE__);
319  $row = pg_fetch_assoc($result);
320  pg_free_result($result);
321  print
322  "$agentname is queued to run on $upload_pk:$row[upload_filename].\n";
323  }
324  }
325  } /* for $ac */
326  } /* for each $upload_pk */
327 } /* QueueUploadsOnAgents() */
328 
335 function QueueUploadsOnDelagents($upload_pk_list)
336 {
337  /* Get the users.default_bucketpool_fk */
338  $user_pk = Auth::getUserId();
339  $group_pk = Auth::getGroupId();
340 
341  if (! empty($upload_pk_list)) {
342  foreach (explode(",", $upload_pk_list) as $upload_pk) {
343  if (empty($upload_pk)) {
344  continue;
345  }
346 
347  // Create a job for the upload
348  $jobpk = JobAddJob($user_pk, $group_pk, "Delete", $upload_pk);
349  if (empty($jobpk) || ($jobpk < 0)) {
350  echo "WARNING: Failed to schedule Delagent for Upload $upload_pk";
351  }
352  $jqargs = "DELETE UPLOAD $upload_pk";
353  $jobqueuepk = JobQueueAdd($jobpk, "delagent", $jqargs, NULL, NULL);
354  if (empty($jobqueuepk)) {
355  echo "WARNING: Failed to schedule Delagent for Upload $upload_pk";
356  }
357  print "Delagent is queued to run on Upload: $upload_pk.\n";
358  } /* for each $upload_pk */
359  } // if $upload_pk is defined
360  /* Tell the scheduler to check the queue. */
361  $success = fo_communicate_with_scheduler("database", $output, $error_msg);
362  if (!$success) {
363  echo $error_msg . "\n" . $output;
364  }
365 }
366 
380 function IsAlreadyScheduled($job_pk, $AgentName, $upload_pk)
381 {
382  global $PG_CONN;
383 
384  $jq_pk = 0;
385 
386  /*
387  * check if the upload_pk is currently in the job queue being processed when
388  * agent name is ununpack or adj2nest
389  */
390  /*
391  * it is unneccessary to reschedule ununpack and adj2nest, one time is enough
392  */
393  if ($AgentName == "ununpack" || $AgentName == "adj2nest") {
394  $sql = "SELECT jq_pk FROM jobqueue, job where job_pk=jq_job_fk " .
395  "AND jq_type='$AgentName' and job_upload_fk = $upload_pk";
396  } else {
397  /* check if the upload_pk is currently in the job queue being processed */
398  $sql = "SELECT jq_pk FROM jobqueue, job where job_pk=jq_job_fk AND jq_type='$AgentName' and job_pk=$job_pk";
399  }
400  $result = pg_query($PG_CONN, $sql);
401  DBCheckResult($result, $sql, __FILE__, __LINE__);
402  if (pg_num_rows($result) > 0) {
403  $row = pg_fetch_assoc($result);
404  $jq_pk = $row["jq_pk"];
405  }
406  pg_free_result($result);
407  return $jq_pk;
408 } // IsAlreadyScheduled()
409 
410 
435 function CommonAgentAdd($plugin, $job_pk, $upload_pk, &$ErrorMsg, $Dependencies, $jqargs = "", $jq_cmd_args = NULL)
436 {
437  global $Plugins;
438  $Deps = array();
439  $DependsEmpty = array();
440 
441  /* check if the latest agent has already been run */
442  if ($plugin->AgentHasResults($upload_pk) == 1) {
443  return 0;
444  }
445 
446  /* if it is already scheduled, then return success */
447  if (($jq_pk = IsAlreadyScheduled($job_pk, $plugin->AgentName, $upload_pk)) != 0) {
448  return $jq_pk;
449  }
450 
451  /* queue up dependencies */
452  foreach ($Dependencies as $Dependency) {
453  if (is_array($Dependency)) {
454  $PluginName = $Dependency['name'];
455  $DepArgs = $Dependency['args'];
456  } else {
457  $PluginName = $Dependency;
458  $DepArgs = null;
459  }
460  $DepPlugin = plugin_find($PluginName);
461  if ($DepPlugin === null) {
462  $ErrorMsg = "Invalid plugin name: $PluginName, (CommonAgentAdd())";
463  return - 1;
464  }
465  if (($Deps[] = $DepPlugin->AgentAdd($job_pk, $upload_pk, $ErrorMsg, $DependsEmpty, $DepArgs)) == - 1) {
466  return - 1;
467  }
468  }
469  /* schedule AgentName */
470  if (empty($jqargs)) {
471  $jqargs = $upload_pk;
472  }
473  $jq_pk = JobQueueAdd($job_pk, $plugin->AgentName, $jqargs, "", $Deps, NULL,
474  $jq_cmd_args);
475  if (empty($jq_pk)) {
476  $ErrorMsg = _(
477  "Failed to insert agent $plugin->AgentName into job queue. jqargs: $jqargs");
478  return (-1);
479  }
480  /* Tell the scheduler to check the queue. */
481  $success = fo_communicate_with_scheduler("database", $output, $error_msg);
482  if (!$success) {
483  $ErrorMsg = $error_msg . "\n" . $output;
484  }
485 
486  return ($jq_pk);
487 }
488 
500 function isAlreadyRunning($agentName, $upload_pk)
501 {
502  global $PG_CONN;
503 
504  $jq_pk = 0;
505 
506  $sql = "SELECT jq_pk FROM jobqueue INNER JOIN job ON job_pk = jq_job_fk "
507  . "WHERE jq_type='$agentName' AND job_upload_fk = $upload_pk "
508  . "AND jq_end_bits = 0";
509  $result = pg_query($PG_CONN, $sql);
510  DBCheckResult($result, $sql, __FILE__, __LINE__);
511  if (pg_num_rows($result) > 0) {
512  $row = pg_fetch_assoc($result);
513  $jq_pk = $row["jq_pk"];
514  }
515  pg_free_result($result);
516  return intval($jq_pk);
517 } // isAlreadyRunning()
IsAlreadyScheduled($job_pk, $AgentName, $upload_pk)
Check if an agent is already scheduled in a job.
Definition: common-job.php:380
CommonAgentAdd($plugin, $job_pk, $upload_pk, &$ErrorMsg, $Dependencies, $jqargs="", $jq_cmd_args=NULL)
Queue an agent. This is a simple version of AgentAdd() that can be used by multiple plugins that only...
Definition: common-job.php:435
QueueUploadsOnAgents($upload_pk_list, $agent_list, $Verbose)
Schedule agent tasks on upload ids.
Definition: common-job.php:270
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
#define PERM_NONE
User has no permission (not logged in)
Definition: libfossology.h:43
GetLastSeq($seqname, $tablename)
Get last sequence number.
Definition: common-db.php:304
GetSingleRec($Table, $Where="")
Retrieve a single database record.
Definition: common-db.php:102
GetJobList($status)
Gets the list of jobqueue records with the requested $status.
Definition: common-job.php:246
isAlreadyRunning($agentName, $upload_pk)
Check if an agent is already running in a job.
Definition: common-job.php:500
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.
foreach($Options as $Option=> $OptVal) if(0==$reference_flag &&0==$nomos_flag) $PG_CONN
DBCheckResult($result, $sql, $filenm, $lineno)
Check the postgres result for unexpected errors. If found, treat them as fatal.
Definition: common-db.php:198
QueueUploadsOnDelagents($upload_pk_list)
Schedule delagent on upload ids.
Definition: common-job.php:335
JobAddUpload($userId, $groupId, $job_name, $filename, $desc, $UploadMode, $folder_pk, $public_perm=Auth::PERM_NONE)
Insert a new upload record, and update the foldercontents table.
Definition: common-job.php:66
#define PERM_ADMIN
Administrator.
Definition: libfossology.h:46