FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ScheduleAgent.php
1 <?php
2 /***********************************************************
3  * Copyright (C) 2014 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\UI\Ajax;
20 
26 
28 {
29  const NAME = "scheduleAgentAjax";
30 
32  private $uploadDao;
33 
34  function __construct()
35  {
36  parent::__construct(self::NAME, array(
37  self::TITLE => _("Private: schedule a agent scan from post"),
38  self::PERMISSION => Auth::PERM_WRITE,
39  ));
40 
41  $this->uploadDao = $this->getObject('dao.upload');
42  }
43 
48  protected function handle(Request $request)
49  {
50  $errorMessage = "";
51  $jobqueueId = -1;
52 
53  $userId = $_SESSION['UserId'];
54  $groupId = $_SESSION['GroupId'];
55  $uploadId = intval($_POST['uploadId']);
56  $agentName = $_POST['agentName'];
57 
58  if ($uploadId > 0) {
59  $upload = $this->uploadDao->getUpload($uploadId);
60  $uploadName = $upload->getFilename();
61  $ourPlugin = plugin_find($agentName);
62 
63  $jobqueueId = isAlreadyRunning($ourPlugin->AgentName, $uploadId);
64  if ($jobqueueId == 0) {
65  $jobId = JobAddJob($userId, $groupId, $uploadName, $uploadId);
66  $jobqueueId = $ourPlugin->AgentAdd($jobId, $uploadId, $errorMessage, array());
67  }
68  } else {
69  $errorMessage = "bad request";
70  }
71 
73 
74  $headers = array('Content-type' => 'text/json');
75  if (empty($errorMessage) && ($jobqueueId > 0)) {
76  return new Response(json_encode(array("jqid" => $jobqueueId)), Response::HTTP_OK, $headers);
77  } else {
78  return new Response(json_encode(array("error" => $errorMessage)), Response::HTTP_INTERNAL_SERVER_ERROR, $headers);
79  }
80  }
81 }
82 
83 register_plugin(new ScheduleAgent());
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.
ReportCachePurgeAll()
Purge all records from the report cache.