Skip to content
This repository has been archived by the owner on Aug 9, 2021. It is now read-only.

Commit

Permalink
feat(mqtt): script to rebuild persisted messages in the broker
Browse files Browse the repository at this point in the history
Signed-off-by: Thierry Bugier <tbugier@teclib.com>
  • Loading branch information
btry authored and DIOHz0r committed Feb 22, 2018
1 parent 8837511 commit 16d6946
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 2 deletions.
18 changes: 18 additions & 0 deletions inc/agent.class.php
Expand Up @@ -1839,4 +1839,22 @@ public function hook_computer_purge(CommonDBTM $item) {
$agent = new static();
$agent->deleteByCriteria(['computers_id' => $item->getField('id')], 1);
}

public function refreshPersistedNotifications() {
if ($this->isNewItem()) {
return;
}

if ($this->fields['wipe'] != '0') {
$this->sendWipeQuery();
}

if ($this->fields['lock'] != '0') {
$this->sendLockQuery();
}

if ($this->fields['enroll_status'] != 'unenrolling') {
$this->sendUnenrollQuery();
}
}
}
20 changes: 19 additions & 1 deletion inc/fleet.class.php
Expand Up @@ -519,4 +519,22 @@ public function hook_entity_purge(CommonDBTM $item) {
$fleet->deleteByCriteria(['entities_id' => $item->getField('id')], 1);
}
}
}

public function refreshPersistedNotifications() {
global $DB;

if ($this->isNewItem()) {
return;
}

$task = new PluginFlyvemdmTask();
$request = [
'FROM' => $task::getTable(),
'WHERE' => [$this::getForeignKeyField() => $this->getID()]
];
foreach ($DB->request($request) as $row) {
$task->getFromDB($row['id']);
$task->publishPolicy($this);
}
}
}
6 changes: 6 additions & 0 deletions inc/notifiable.class.php
Expand Up @@ -75,4 +75,10 @@ public function getFiles();
* @param integer $retain
*/
public function notify($topic, $mqttMessage, $qos = 0, $retain = 0);

/**
* Send all persisted messages for the notifiable
* Used to regenerate messages stored by the broker if they are lost
*/
public function refreshPersistedNotifications();
}
1 change: 1 addition & 0 deletions install/upgrade/update_to_dev.php
Expand Up @@ -349,6 +349,7 @@ function plugin_flyvemdm_update_to_dev(Migration $migration) {
$mqttClient->publish('/' . $topic, null, 0, 1);
}

// re-use previous request array
$request['WHERE'] = ['enroll_status' => ['=' => 'unenrolling']];
$mqttMessage = ['unenroll' => 'now'];
$mqttMessage = json_encode($mqttMessage, JSON_UNESCAPED_SLASHES);
Expand Down
70 changes: 70 additions & 0 deletions scripts/mqtt-rebuild-messages.php
@@ -0,0 +1,70 @@
<?php
/**
* LICENSE
*
* Copyright © 2016-2018 Teclib'
* Copyright © 2010-2018 by the FusionInventory Development Team.
*
* This file is part of Flyve MDM Plugin for GLPI.
*
* Flyve MDM Plugin for GLPI is a subproject of Flyve MDM. Flyve MDM is a mobile
* device management software.
*
* Flyve MDM Plugin for GLPI is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* Flyve MDM Plugin for GLPI is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
* You should have received a copy of the GNU Affero General Public License
* along with Flyve MDM Plugin for GLPI. If not, see http://www.gnu.org/licenses/.
* ------------------------------------------------------------------------------
* @author Thierry Bugier
* @copyright Copyright © 2018 Teclib
* @license AGPLv3+ http://www.gnu.org/licenses/agpl.txt
* @link https://github.com/flyve-mdm/glpi-plugin
* @link https://flyve-mdm.com/
* ------------------------------------------------------------------------------
*/

// Ensure current directory when run from crontab
chdir(dirname($_SERVER["SCRIPT_FILENAME"]));

include (__DIR__ . "/../vendor/docopt/docopt/src/docopt.php");

$doc = <<<DOC
mqtt-rebuild-messages.php
Usage:
mqtt-rebuild-messages.php [ --tests ]
Options:
--tests Use GLPI test database
--debug Verbose mode for debug (dumps all messages)
DOC;

$docopt = new \Docopt\Handler();
$args = $docopt->handle($doc);
if (isset($args['--tests']) && $args['--tests'] !== false) {
echo "running in testing environment" . PHP_EOL;
define('GLPI_ROOT', dirname(dirname(dirname(__DIR__))));
define("GLPI_CONFIG_DIR", GLPI_ROOT . "/tests");
}

include (__DIR__ . '/../../../inc/includes.php');

$mqttClient = PluginFlyvemdmMqttClient::getInstance();
$fleetFk = PluginFlyvemdmFleet::getForeignKeyField();
$request = [
'SELECT' => ['id'],
'FROM' => PluginFlyvemdmFleet::getTable(),
'WHERE' => ['is_default' => ['<>' => '0']]
];
$fleet = new PluginFlyvemdmFleet();
foreach($DB->request($request) as $row) {
$fleet->getFromDB($row['id']);
$fleet->refreshPersistedNotifications();
}
2 changes: 1 addition & 1 deletion scripts/mqtt.php
Expand Up @@ -49,7 +49,7 @@
$docopt = new \Docopt\Handler();
$args = $docopt->handle($doc);
if (isset($args['--tests']) && $args['--tests'] !== false) {
echo "running in testing environment" . PHP_EOL;
echo "running in testing environment" . PHP_EOL;
define('GLPI_ROOT', dirname(dirname(dirname(__DIR__))));
define("GLPI_CONFIG_DIR", GLPI_ROOT . "/tests");
}
Expand Down

0 comments on commit 16d6946

Please sign in to comment.