Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit 60ee9fd

Browse files
committed
Add a dedicated table for queues and stop using file-based queues
1 parent 5cc4f18 commit 60ee9fd

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

core/src/plugins/mq.sql/SqlMessageExchanger.php

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
use Pydio\Core\Model\ContextInterface;
2424

25+
use Pydio\Core\PluginFramework\SqlTableProvider;
26+
use Pydio\Core\Utils\DBHelper;
2527
use Pydio\Core\Utils\FileHelper;
2628
use Pydio\Core\Utils\Vars\OptionsHelper;
2729

@@ -37,7 +39,7 @@
3739
* @package AjaXplorer_Plugins
3840
* @subpackage Mq
3941
*/
40-
class SqlMessageExchanger extends Plugin implements IMessageExchanger
42+
class SqlMessageExchanger extends Plugin implements IMessageExchanger, SqlTableProvider
4143
{
4244

4345
/**
@@ -77,9 +79,11 @@ public function loadChannel($channelName, $create = false)
7779
if (isSet(self::$channels) && is_array(self::$channels[$channelName])) {
7880
return;
7981
}
80-
if (is_file($this->getPluginWorkDir()."/queues/channel-$channelName")) {
82+
$res = dibi::query('SELECT [content] FROM [ajxp_mq_queues] WHERE [channel_name] = %s', $channelName);
83+
if($res->count()){
8184
if(!isset(self::$channels)) self::$channels = array();
82-
$data = FileHelper::loadSerialFile($this->getPluginWorkDir() . "/queues/channel-$channelName");
85+
$single = $res->fetchSingle();
86+
$data = unserialize(base64_decode($single));
8387
if (is_array($data)) {
8488
if(!is_array($data["MESSAGES"])) $data["MESSAGES"] = array();
8589
if(!is_array($data["CLIENTS"])) $data["CLIENTS"] = array();
@@ -97,9 +101,36 @@ public function loadChannel($channelName, $create = false)
97101
public function __destruct()
98102
{
99103
if (isSet(self::$channels) && is_array(self::$channels)) {
104+
$inserts = [];
105+
$deletes = [];
106+
$driver = $this->sqlDriver["driver"];
100107
foreach (self::$channels as $channelName => $data) {
101108
if (is_array($data)) {
102-
FileHelper::saveSerialFile($this->getPluginWorkDir() . "/queues/channel-$channelName", $data);
109+
if(isSet($data["CLIENTS"]) && count($data["CLIENTS"])) {
110+
$serialized = base64_encode(serialize($data));
111+
if($driver === "postgre"){
112+
dibi::query("DELETE FROM [ajxp_mq_queues] WHERE [channel_name] = %s", $channelName);
113+
dibi::query('INSERT INTO [ajxp_mq_queues]', ["channel_name" => $channelName, "content" => $serialized]);
114+
}else{
115+
$inserts[] = "('$channelName', '".$serialized."')";
116+
}
117+
}else{
118+
$deletes[] = $channelName;
119+
}
120+
}
121+
}
122+
if(count($inserts)){
123+
try{
124+
dibi::query('REPLACE INTO [ajxp_mq_queues] ([channel_name],[content]) VALUES '.implode(",", $inserts));
125+
}catch(\DibiException $dE){
126+
$this->logError(__CLASS__, $dE->getMessage());
127+
}
128+
}
129+
if(count($deletes)){
130+
try{
131+
dibi::query('DELETE FROM [ajxp_mq_queues] WHERE [channel_name] IN %s', $deletes);
132+
}catch(\DibiException $dE){
133+
$this->logError(__CLASS__, $dE->getMessage());
103134
}
104135
}
105136
}
@@ -337,4 +368,14 @@ public function publishInstantMessage(ContextInterface $ctx, $channel, $message)
337368
self::$channels[$channel]["MESSAGES"] = $clean;
338369
}
339370

371+
/**
372+
* @param array $param
373+
* @return string
374+
* @throws \Exception
375+
*/
376+
public function installSQLTables($param)
377+
{
378+
$p = OptionsHelper::cleanDibiDriverParameters(isSet($param) && isSet($param["SQL_DRIVER"]) ? $param["SQL_DRIVER"] : $this->sqlDriver);
379+
return DBHelper::runCreateTablesQuery($p, $this->getBaseDir() . "/create.sql");
380+
}
340381
}

core/src/plugins/mq.sql/create.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE IF NOT EXISTS `ajxp_mq_queues` (
2+
`channel_name` varchar(255) NOT NULL,
3+
`content` text NOT NULL,
4+
PRIMARY KEY (`channel_name`)
5+
) DEFAULT CHARSET=utf8;

0 commit comments

Comments
 (0)