function prepareRun($taskjobs_id)
 {
     $pfTask = new PluginFusioninventoryTask();
     $pfTaskjob = new PluginFusioninventoryTaskjob();
     $pfTaskjoblog = new PluginFusioninventoryTaskjoblog();
     $pfTaskjobstate = new PluginFusioninventoryTaskjobstate();
     $pfAgentmodule = new PluginFusioninventoryAgentmodule();
     $pfIPRange = new PluginFusioninventoryIPRange();
     $pfAgent = new PluginFusioninventoryAgent();
     $uniqid = uniqid();
     $pfTaskjob->getFromDB($taskjobs_id);
     $pfTask->getFromDB($pfTaskjob->fields['plugin_fusioninventory_tasks_id']);
     $communication = $pfTask->fields['communication'];
     //list all iprange
     $a_iprange = importArrayFromDB($pfTaskjob->fields['definition']);
     $count_ip = 0;
     $a_iprangelist = array();
     $a_subnet_nbip = array();
     foreach ($a_iprange as $iprange) {
         $iprange_id = current($iprange);
         $a_iprangelist[] = $iprange_id;
         $pfIPRange->getFromDB($iprange_id);
         $s = $pfIPRange->getIp2long($pfIPRange->fields['ip_start']);
         $e = $pfIPRange->getIp2long($pfIPRange->fields['ip_end']);
         $a_subnet_nbip[$iprange_id] = $e - $s;
         $count_ip += $e - $s;
     }
     //list all agents
     $a_agent = importArrayFromDB($pfTaskjob->fields['action']);
     $dynagent = 0;
     $a_agentlist = array();
     foreach ($a_agent as $agent) {
         $agent_id = current($agent);
         if ($agent_id == '.1') {
             $dynagent = 1;
         } else {
             if ($agent_id == '.2') {
                 $dynagent = 2;
             } else {
                 // Detect if agent exists
                 if ($pfAgent->getFromDB($agent_id)) {
                     if ($pfTask->fields['communication'] == 'pull') {
                         $a_agentlist[$agent_id] = 1;
                     } else {
                         if ($pfTaskjob->isAgentAlive('1', $agent_id)) {
                             $a_agentlist[$agent_id] = 1;
                         }
                     }
                 }
             }
         }
     }
     if ($dynagent == '1') {
         $a_agents = $pfAgentmodule->getAgentsCanDo('NETWORKDISCOVERY');
         foreach ($a_agents as $data) {
             if ($count_ip / 10 >= count($a_agentlist)) {
                 $pfAgent->getFromDB($data['id']);
                 $a_ip = $pfAgent->getIPs();
                 foreach ($a_ip as $ip) {
                     if ($pfTask->fields['communication'] == 'push') {
                         if ($pfTaskjob->isAgentAlive('1', $data['id'])) {
                             $a_agentlist[$data['id']] = 1;
                         }
                     } else {
                         if ($pfTask->fields['communication'] == 'pull') {
                             $a_agentlist[$data['id']] = 1;
                         }
                     }
                 }
             }
         }
     }
     if ($dynagent == '2') {
         // Dynamic with subnet
         $pfSnmpinventory = new PluginFusioninventoryNetworkinventory();
         $taskvalid = 0;
         foreach ($a_subnet_nbip as $iprange_id => $nbips) {
             //$maxagentpossible = $nbips/10;
             $pfIPRange->getFromDB($iprange_id);
             $a_agentListComplete = array();
             $a_agentList = $pfSnmpinventory->getAgentsSubnet($nbips, "push", "", $pfIPRange->fields['ip_start'], $pfIPRange->fields['ip_end']);
             if (isset($a_agentList)) {
                 $a_agentListComplete = array_merge($a_agentListComplete, $a_agentList);
             }
             if (!isset($a_agentListComplete) or empty($a_agentListComplete)) {
                 $a_input = array();
                 $a_input['plugin_fusioninventory_taskjobs_id'] = $taskjobs_id;
                 $a_input['plugin_fusioninventory_agents_id'] = 0;
                 $a_input['state'] = 1;
                 $a_input['itemtype'] = 'PluginFusioninventoryIPRange';
                 $a_input['items_id'] = $iprange_id;
                 $a_input['uniqid'] = $uniqid;
                 $a_input['execution_id'] = $task->fields['execution_id'];
                 $Taskjobstates_id = $pfTaskjobstate->add($a_input);
                 //Add log of taskjob
                 $a_input['plugin_fusioninventory_taskjobstates_id'] = $Taskjobstates_id;
                 $a_input['state'] = 7;
                 $a_input['date'] = date("Y-m-d H:i:s");
                 $pfTaskjoblog->add($a_input);
                 $pfTaskjobstate->changeStatusFinish($Taskjobstates_id, 0, 'PluginFusioninventoryIPRange', 1, "Unable to find agent to run this job");
                 $input_taskjob = array();
                 $input_taskjob['id'] = $pfTaskjob->fields['id'];
                 //$input_taskjob['status'] = 1;
                 $pfTaskjob->update($input_taskjob);
             } else {
                 $s = $pfIPRange->getIp2long($pfIPRange->fields['ip_start']);
                 $e = $pfIPRange->getIp2long($pfIPRange->fields['ip_end']);
                 $nbIpAgent = ceil(($e - $s) / count($a_agentListComplete));
                 $iptimes = 0;
                 foreach ($a_agentListComplete as $agent_id) {
                     $_SESSION['glpi_plugin_fusioninventory']['agents'][$agent_id] = 1;
                     //Add jobstate and put status (waiting on server = 0)
                     $a_input = array();
                     $a_input['plugin_fusioninventory_taskjobs_id'] = $taskjobs_id;
                     $a_input['state'] = 0;
                     $a_input['plugin_fusioninventory_agents_id'] = $agent_id;
                     $a_input['itemtype'] = 'PluginFusioninventoryIPRange';
                     $a_input['uniqid'] = $uniqid;
                     $a_input['execution_id'] = $task->fields['execution_id'];
                     $a_input['items_id'] = $iprange_id;
                     if ($iptimes + $nbIpAgent > $e - $s) {
                         $a_input['specificity'] = $iptimes . "-" . ($e - $s);
                     } else {
                         $a_input['specificity'] = $iptimes . "-" . ($iptimes + $nbIpAgent);
                     }
                     $taskvalid++;
                     $Taskjobstates_id = $pfTaskjobstate->add($a_input);
                     //Add log of taskjob
                     $a_input['plugin_fusioninventory_taskjobstates_id'] = $Taskjobstates_id;
                     $a_input['state'] = 7;
                     $a_input['date'] = date("Y-m-d H:i:s");
                     $pfTaskjoblog->add($a_input);
                     unset($a_input['state']);
                     $iptimes += $nbIpAgent + 1;
                     if ($iptimes >= $e - $s + 1) {
                         break;
                     }
                     $input_taskjob = array();
                     $input_taskjob['id'] = $pfTaskjob->fields['id'];
                     $input_taskjob['status'] = 1;
                     $pfTaskjob->update($input_taskjob);
                 }
             }
         }
         if ($taskvalid == "0") {
             $pfTaskjob->reinitializeTaskjobs($pfTaskjob->fields['plugin_fusioninventory_tasks_id']);
         }
         // *** Add jobstate
     } else {
         if (count($a_agentlist) == 0) {
             $a_input = array();
             $a_input['plugin_fusioninventory_taskjobs_id'] = $taskjobs_id;
             $a_input['state'] = 1;
             $a_input['plugin_fusioninventory_agents_id'] = 0;
             $a_input['itemtype'] = 'PluginFusioninventoryIPRange';
             $a_input['items_id'] = 0;
             $a_input['uniqid'] = $uniqid;
             $a_input['execution_id'] = $task->fields['execution_id'];
             $Taskjobstates_id = $pfTaskjobstate->add($a_input);
             //Add log of taskjob
             $a_input['plugin_fusioninventory_taskjobstates_id'] = $Taskjobstates_id;
             $a_input['state'] = 7;
             $a_input['date'] = date("Y-m-d H:i:s");
             $pfTaskjoblog->add($a_input);
             $pfTaskjobstate->changeStatusFinish($Taskjobstates_id, 0, 'PluginFusioninventoryIPRange', 1, "Unable to find agent to run this job");
             $input_taskjob = array();
             $input_taskjob['id'] = $pfTaskjob->fields['id'];
             //$input_taskjob['status'] = 1;
             $pfTaskjob->update($input_taskjob);
         } else {
             $iptimes = 0;
             $nbIpadded = 0;
             $break = 0;
             $numberIpByAgent = ceil($count_ip / count($a_agentlist));
             $a_iprangelistTmp = $a_iprangelist;
             $ip_id = array_shift($a_iprangelistTmp);
             foreach ($a_agentlist as $agent_id => $ip) {
                 //Add jobstate and put status (waiting on server = 0)
                 $a_input = array();
                 $a_input['plugin_fusioninventory_taskjobs_id'] = $taskjobs_id;
                 $a_input['state'] = 0;
                 $a_input['plugin_fusioninventory_agents_id'] = $agent_id;
                 $a_input['itemtype'] = 'PluginFusioninventoryIPRange';
                 $a_input['uniqid'] = $uniqid;
                 $a_input['execution_id'] = $task->fields['execution_id'];
                 //            $nbIpAgent = $numberIpByAgent;
                 $nbIpadded = 0;
                 foreach ($a_iprangelist as $iprange_id) {
                     if ($ip_id == $iprange_id) {
                         $pfIPRange->getFromDB($iprange_id);
                         $s = $pfIPRange->getIp2long($pfIPRange->fields['ip_start']);
                         $e = $pfIPRange->getIp2long($pfIPRange->fields['ip_end']);
                         if ($communication == "push") {
                             $_SESSION['glpi_plugin_fusioninventory']['agents'][$agent_id] = 1;
                         }
                         $a_input['items_id'] = $iprange_id;
                         $nbIpAgent = $numberIpByAgent - $nbIpadded;
                         if ($iptimes + $nbIpAgent > $e - $s) {
                             $a_input['specificity'] = $iptimes . "-" . ($e - $s);
                             $nbIpadded = $e - $s - $iptimes;
                             $ip_id = array_shift($a_iprangelistTmp);
                             $iptimes = 0;
                         } else {
                             $a_input['specificity'] = $iptimes . "-" . ($iptimes + $nbIpAgent);
                             $iptimes += $nbIpAgent + 1;
                             $nbIpadded = 0;
                             $break = 1;
                         }
                         $Taskjobstates_id = $pfTaskjobstate->add($a_input);
                         //Add log of taskjob
                         $a_input['plugin_fusioninventory_taskjobstates_id'] = $Taskjobstates_id;
                         $a_input['state'] = 7;
                         $a_input['date'] = date("Y-m-d H:i:s");
                         $pfTaskjoblog->add($a_input);
                         unset($a_input['state']);
                     }
                 }
                 $input_taskjob = array();
                 $input_taskjob['id'] = $pfTaskjob->fields['id'];
                 $input_taskjob['status'] = 1;
                 $pfTaskjob->update($input_taskjob);
             }
         }
     }
     return $uniqid;
 }