Some improvements in multi-server communications (Entity)
This commit is contained in:
parent
93908626a0
commit
600b914715
|
@ -57,6 +57,20 @@ abstract class Entity
|
|||
*/
|
||||
private $enterprise;
|
||||
|
||||
/**
|
||||
* MC Node id.
|
||||
*
|
||||
* @var integer|null
|
||||
*/
|
||||
protected $nodeId = null;
|
||||
|
||||
/**
|
||||
* Connected to external node.
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
private $connected = false;
|
||||
|
||||
|
||||
/**
|
||||
* Instances a new object using array definition.
|
||||
|
@ -193,6 +207,53 @@ abstract class Entity
|
|||
}
|
||||
|
||||
|
||||
/**
|
||||
* Connects to current nodeId target.
|
||||
* If no nodeId is defined, then returns without doing anything.
|
||||
*
|
||||
* @return void
|
||||
* @throws \Exception On error.
|
||||
*/
|
||||
public function connectNode()
|
||||
{
|
||||
if ($this->nodeId === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
\enterprise_include_once('include/functions_metaconsole.php');
|
||||
$r = \enterprise_hook(
|
||||
'metaconsole_connect',
|
||||
[
|
||||
null,
|
||||
$this->nodeId,
|
||||
]
|
||||
);
|
||||
|
||||
if ($r !== NOERR) {
|
||||
throw new \Exception(
|
||||
__('Cannot connect to node %d', $this->nodeId)
|
||||
);
|
||||
}
|
||||
|
||||
$this->connected = true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Restore connection after connectNode.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function restoreConnection()
|
||||
{
|
||||
if ($this->connected === true) {
|
||||
\enterprise_include_once('include/functions_metaconsole.php');
|
||||
\enterprise_hook('metaconsole_restore_db');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Saves current object definition to database.
|
||||
*
|
||||
|
|
|
@ -74,14 +74,29 @@ class Event extends Entity
|
|||
parent::__construct($table);
|
||||
}
|
||||
|
||||
try {
|
||||
if ((bool) \is_metaconsole() === true
|
||||
&& $this->server_id() !== null
|
||||
) {
|
||||
$this->nodeId = $this->server_id();
|
||||
}
|
||||
|
||||
$this->connectNode();
|
||||
|
||||
if ($this->id_agente() !== null) {
|
||||
$this->linkedAgent = new Agent($this->id_agente());
|
||||
$this->linkedAgent = new Agent((int) $this->id_agente());
|
||||
}
|
||||
|
||||
if ($this->id_agentmodule() !== null) {
|
||||
$this->linkedModule = new Module($this->id_agentmodule());
|
||||
$this->linkedModule = new Module((int) $this->id_agentmodule());
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
// Do not link items if failed to find them.
|
||||
$this->restoreConnection();
|
||||
}
|
||||
|
||||
// Restore if needed.
|
||||
$this->restoreConnection();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -91,8 +91,10 @@ class Module extends Entity
|
|||
* @return PandoraFMS\Module found or null if not found.
|
||||
* @throws \Exception On error.
|
||||
*/
|
||||
public static function search(array $params, ?int $limit=0)
|
||||
{
|
||||
public static function search(
|
||||
array $params,
|
||||
?int $limit=0
|
||||
) {
|
||||
if (empty($params) === true) {
|
||||
return null;
|
||||
}
|
||||
|
@ -192,21 +194,40 @@ class Module extends Entity
|
|||
*
|
||||
* @param integer|null $id_agent_module Module id.
|
||||
* @param boolean $link_agent Link agent object.
|
||||
* @param integer|null $nodeId Target node (if metaconsole
|
||||
* environment).
|
||||
*
|
||||
* @throws \Exception On error.
|
||||
*/
|
||||
public function __construct(
|
||||
?int $id_agent_module=null,
|
||||
bool $link_agent=false
|
||||
bool $link_agent=false,
|
||||
?int $nodeId=null
|
||||
) {
|
||||
if (is_numeric($id_agent_module) === true
|
||||
&& $id_agent_module > 0
|
||||
) {
|
||||
if ($nodeId !== null) {
|
||||
$this->nodeId = $nodeId;
|
||||
}
|
||||
|
||||
try {
|
||||
// Connect to node if needed.
|
||||
$this->connectNode();
|
||||
|
||||
parent::__construct(
|
||||
'tagente_modulo',
|
||||
['id_agente_modulo' => $id_agent_module]
|
||||
);
|
||||
|
||||
// Restore.
|
||||
$this->restoreConnection();
|
||||
} catch (\Exception $e) {
|
||||
$this->restoreConnection();
|
||||
// Forward exception.
|
||||
throw $e;
|
||||
}
|
||||
|
||||
if ($this->nombre() === 'delete_pending') {
|
||||
return null;
|
||||
}
|
||||
|
@ -229,14 +250,22 @@ class Module extends Entity
|
|||
}
|
||||
|
||||
try {
|
||||
// Connect to node if needed.
|
||||
$this->connectNode();
|
||||
|
||||
// Customize certain fields.
|
||||
$this->status = new ModuleStatus($this->fields['id_agente_modulo']);
|
||||
|
||||
// Restore.
|
||||
$this->restoreConnection();
|
||||
} catch (\Exception $e) {
|
||||
// Restore.
|
||||
$this->restoreConnection();
|
||||
|
||||
$this->status = new Modulestatus();
|
||||
}
|
||||
|
||||
// Customize certain fields.
|
||||
$this->status = new ModuleStatus($this->fields['id_agente_modulo']);
|
||||
$this->moduleType = new ModuleType($this->id_tipo_modulo());
|
||||
|
||||
// Include some enterprise dependencies.
|
||||
|
@ -267,8 +296,15 @@ class Module extends Entity
|
|||
{
|
||||
if ($this->linkedAgent === null) {
|
||||
try {
|
||||
// Connect to node if needed.
|
||||
$this->connectNode();
|
||||
$this->linkedAgent = new Agent($this->id_agente());
|
||||
// Connect to node if needed.
|
||||
$this->restoreConnection();
|
||||
} catch (\Exception $e) {
|
||||
// Connect to node if needed.
|
||||
$this->restoreConnection();
|
||||
|
||||
// Unexistent agent.
|
||||
return null;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue