Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
retrieve force start button in task (only for debug mode) (#2057)
Browse files Browse the repository at this point in the history
  • Loading branch information
orthagh authored and trasher committed Feb 27, 2017
1 parent 8c699ba commit a9211b6
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 59 deletions.
21 changes: 20 additions & 1 deletion inc/task.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ function getTaskjobstatesForAgent($agent_id, $methods = array(), $options=array(
* @param array $methods
* @return true
*/
function prepareTaskjobs($methods = array()) {
function prepareTaskjobs($methods = array(), $tasks_id = false) {
global $DB;

$now = new DateTime();
Expand All @@ -454,6 +454,12 @@ function prepareTaskjobs($methods = array()) {
//transform methods array into string for database query
$methods = "'" . implode("','", $methods) . "'";

// limit preparation to a specific tasks_id
$sql_task_id = "";
if ($tasks_id) {
$sql_task_id = "AND `task`.`id` = $tasks_id";
}

$query = implode( " \n", array(
"SELECT",
" task.`id`, task.`name`, task.`reprepare_if_successful`, ",
Expand All @@ -463,6 +469,7 @@ function prepareTaskjobs($methods = array()) {
"LEFT JOIN `glpi_plugin_fusioninventory_tasks` task",
" ON task.`id` = job.`plugin_fusioninventory_tasks_id`",
"WHERE task.`is_active` = 1",
$sql_task_id,
"AND (",
/**
* Filter jobs by the schedule and timeslots
Expand Down Expand Up @@ -783,6 +790,18 @@ static function formatChrono($chrono) {
}


/**
* Force running the current task
**/
function forceRunning() {
$methods = array();
foreach( PluginFusioninventoryStaticmisc::getmethods() as $method) {
$methods[] = $method['method'];
}
$this->prepareTaskjobs($methods, $this->getID());
}



/**
* Get logs of job
Expand Down
44 changes: 39 additions & 5 deletions inc/task.view.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,42 @@ function showForm($id, $options=array()) {
return true;
}

function showFormButtons($options = array()) {
if (isset($this->fields['id'])) {
$ID = $this->fields['id'];
}

echo "<tr>";
echo "<td colspan='2'>";
if ($this->isNewID($ID)) {
echo Html::submit(_x('button','Add'), array('name' => 'add'));
} else {
echo Html::hidden('id', array('value' => $ID));
echo Html::submit(_x('button','Save'), array('name' => 'update'));
}
echo "</td>";

if ($this->fields['is_active']
&& $_SESSION['glpi_use_mode'] == Session::DEBUG_MODE) {
echo "<td>";
echo Html::submit(__('Force start', 'fusioninventory'), array('name' => 'forcestart'));
echo "</td>";
}

echo "<td>";
if ($this->can($ID, PURGE)) {
echo Html::submit(_x('button','Delete permanently'),
array('name' => 'purge',
'confirm' => __('Confirm the final deletion?')));
}
echo "</td>";
echo "</tr>";

// Close for Form
echo "</table></div>";
Html::closeForm();
}



/**
Expand All @@ -534,11 +570,9 @@ public function submitForm($postvars) {
if (isset($postvars['forcestart'])) {
Session::checkRight('plugin_fusioninventory_task', UPDATE);

/**
* TODO: forcing the task execution should be done in the task object
*/
$pfTaskjob = new PluginFusioninventoryTaskjob();
$pfTaskjob->forceRunningTask($postvars['id']);
$this->getFromDB($postvars['id']);
$this->forceRunning();

Html::back();
} else if (isset ($postvars["add"])) {
Session::checkRight('plugin_fusioninventory_task', CREATE);
Expand Down
49 changes: 0 additions & 49 deletions inc/taskjob.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -673,55 +673,6 @@ function reinitializeTaskjobs($tasks_id, $disableTimeVerification = 0) {



/**
* Force running a task
*
* @global object $DB
* @param integer $tasks_id id of the task
* @return string unique id
*/
function forceRunningTask($tasks_id) {
global $DB;

$uniqid = '';

if ($this->reinitializeTaskjobs($tasks_id, 1)) {

$pfTaskjob = new PluginFusioninventoryTaskjob();
$_SESSION['glpi_plugin_fusioninventory']['agents'] = array();

$query = "SELECT `".$pfTaskjob->getTable()."`.*,
`glpi_plugin_fusioninventory_tasks`.`communication`,
UNIX_TIMESTAMP(datetime_start) as date_scheduled_timestamp
FROM ".$pfTaskjob->getTable()."
LEFT JOIN `glpi_plugin_fusioninventory_tasks`
ON `plugin_fusioninventory_tasks_id`=`glpi_plugin_fusioninventory_tasks`.`id`
WHERE `is_active`='1'
AND `status` = '0'
AND `glpi_plugin_fusioninventory_tasks`.`id`='".$tasks_id."'
AND `".$pfTaskjob->getTable()."`.`plugins_id` != '0'
AND `".$pfTaskjob->getTable()."`.`method` IS NOT NULL
AND `".$pfTaskjob->getTable()."`.`method` != ''
ORDER BY `id`";
$result = $DB->query($query);
while ($data=$DB->fetch_array($result)) {
$uniqid = $pfTaskjob->prepareRunTaskjob($data);
}
foreach (array_keys($_SESSION['glpi_plugin_fusioninventory']['agents']) as $agents_id) {
$pfTaskjob->startAgentRemotly($agents_id);
}
unset($_SESSION['glpi_plugin_fusioninventory']['agents']);
} else {
Session::addMessageAfterRedirect(
__('Unable to run task because some jobs is running yet!', 'fusioninventory'),
ERROR
);
}
return $uniqid;
}



/**
* Get period in secondes by type and count time
*
Expand Down
6 changes: 3 additions & 3 deletions inc/taskjob.view.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -810,12 +810,12 @@ public function submitForm($postvars) {
} else if (isset($postvars['taskjobstoforcerun'])) {
// * Force running many tasks (wizard)
Session::checkRight('plugin_fusioninventory_task', UPDATE);
$pfTask = new PluginFusioninventoryTask();
$pfTaskjob = new PluginFusioninventoryTaskjob();
$_SESSION["plugin_fusioninventory_forcerun"] = array();
foreach ($postvars['taskjobstoforcerun'] as $taskjobs_id) {
$pfTaskjob->getFromDB($taskjobs_id);
$uniqid = $pfTaskjob->forceRunningTask($pfTaskjob->fields['plugin_fusioninventory_tasks_id']);
$_SESSION["plugin_fusioninventory_forcerun"][$taskjobs_id] = $uniqid;
$pfTask->getFromDB($pfTaskjob->fields['plugin_fusioninventory_tasks_id']);
$pfTask->forceRunning();
}
} else if (isset($postvars['add']) || isset($postvars['update'])) {
// * Add and update taskjob
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function TaskWithPC() {
WHERE `id`="1"');

// Force task prepation
$pfTaskJob->forceRunningTask(1);
$pfTask->forceRunning();

$a_jobstates = getAllDatasFromTable("glpi_plugin_fusioninventory_taskjobstates");
foreach ($a_jobstates as $num=>$data) {
Expand Down

0 comments on commit a9211b6

Please sign in to comment.