-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtjqueue.php
More file actions
346 lines (279 loc) · 8.41 KB
/
tjqueue.php
File metadata and controls
346 lines (279 loc) · 8.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* @package Techjoomla.CLI
* @author Techjoomla <extensions@techjoomla.com>
* @copyright Copyright (c) 2009-2019 TechJoomla. All rights reserved.
* @license GNU General Public License version 2 or later.
*/
use TJQueue\Admin\TJQueueConsume;
use Joomla\CMS\Log\Log;
use Joomla\CMS\Application\CliApplication;
use Joomla\CMS\Plugin\PluginHelper;
use Joomla\CMS\Factory;
define('_JEXEC', 1);
define('JPATH_BASE', dirname(__DIR__));
// Load system defines
if (file_exists(JPATH_BASE . '/defines.php'))
{
require_once JPATH_BASE . '/defines.php';
}
if (!defined('_JDEFINES'))
{
require_once JPATH_BASE . '/includes/defines.php';
}
// Check for presence of vendor dependencies not included in the git repository
if (!file_exists(JPATH_LIBRARIES . '/vendor/autoload.php') || !is_dir(JPATH_ROOT . '/media/vendor')) {
echo file_get_contents(JPATH_ROOT . '/templates/system/build_incomplete.html');
exit;
}
require_once JPATH_BASE . '/includes/framework.php';
// Boot the DI container
$container = \Joomla\CMS\Factory::getContainer();
// Alias the session service keys to the web session service as that is the primary session backend for this application
// In addition to aliasing "common" service keys, we also create aliases for the PHP classes to ensure autowiring objects is supported. This includes aliases for aliased class names, and the keys for aliased class names should be considered deprecated to be removed when the class name alias is removed as well.
$container->alias('session.web', 'session.web.site')
->alias('session', 'session.web.site')
->alias('JSession', 'session.web.site')
->alias(\Joomla\CMS\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\Session::class, 'session.web.site')
->alias(\Joomla\Session\SessionInterface::class, 'session.web.site');
// Instantiate the application.
$app = $container->get(\Joomla\CMS\Application\SiteApplication::class);
// Set the application as global app
\Joomla\CMS\Factory::$application = $app;
$app->createExtensionNamespaceMap();
// Get the framework.
require_once JPATH_LIBRARIES . '/bootstrap.php';
// Bootstrap the CMS libraries.
// require_once JPATH_LIBRARIES . '/cms.php';
// Load the configuration
require_once JPATH_CONFIGURATION . '/configuration.php';
jimport('tjqueueconsume', JPATH_SITE . '/administrator/components/com_tjqueue/libraries');
require_once JPATH_BASE . '/includes/defines.php';
require_once JPATH_BASE . '/includes/framework.php';
//~ $app = Factory::getContainer()->get(\Joomla\CMS\Application\SiteApplication::class);
//~ $app->initialise();
/**
* TjQueue
*
* @package Techjoomla.CLI
* @subpackage Tjqueue
* @since 0.0.1
*/
class TJQueue extends JApplicationCli
{
private $message = null;
private $options;
/**
* Class constructor.
*
* @since 0.0.1
*/
public function __construct()
{
parent::__construct();
$shortopts = "";
// Support topic with option -t value
$shortopts .= "t:";
// Support limit with option -n value
$shortopts .= "n:";
// Support set timeout with option -s value
$shortopts .= "s:";
$longopts = array(
"topic:", // Long option to read topic --topic="value"
"n:", // Long option to read count --n="value"
"timeout:" // Long option to read timeout in milliseconds --timeout="value"
);
$argv = getopt($shortopts, $longopts);
$this->options = new stdClass;
$this->options->topic = array_key_exists('t', $argv) ? $argv['t'] : (array_key_exists('topic', $argv) ? $argv['topic'] : null);
$this->options->limit = array_key_exists('n', $argv) ? $argv['n'] : 50;
$this->options->timeout = array_key_exists('s', $argv) ? $argv['s'] : (array_key_exists('timeout', $argv) ? $argv['timeout'] : 2000);
}
/**
* Load an object or array into the application configuration object.
*
* @param mixed $data Either an array or object to be loaded into the configuration object.
*
* @return CliApplication Instance of $this to allow chaining.
*
* @since 1.7.0
*/
public function loadConfiguration($data)
{
// Load the data into the configuration object.
if (is_array($data))
{
$this->config->loadArray($data);
}
elseif (is_object($data))
{
$this->config->loadObject($data);
}
return $this;
}
/**
* Entry point for the script
*
* @return void
*
* @since 3.8.6
*/
// public function doExecute()
// {
// }
public function getName()
{
}
/**
* Method to execute script
*
* @return void.
*
* @since 1.0
*/
public function doExecute()
{
PluginHelper::importPlugin('system');
Joomla\CMS\Factory::getApplication()->triggerEvent('onAfterInitialise');
//PluginHelper::importPlugin('system');
$log['success'] = 1;
$log['message'] = 'Started: Running queue cron';
self::writeLog($log);
// If topic name not set in first argument
if (!$this->options->topic)
{
$log['success'] = 0;
$log['message'] = 'Error-Topic name not found to process.';
self::writeLog($log);
exit;
}
$TJQueueConsume = new TJQueueConsume($this->options->topic, $this->options->timeout);
$i = 0;
while ($i++ < $this->options->limit)
{
$this->message = $TJQueueConsume->receive();
if ($this->message == null)
{
$log['success'] = 1;
$log['message'] = 'Done: No message available in queue to process';
self::writeLog($log);
exit;
}
$client = $this->message->getProperty('client');
if (empty($client))
{
$log['success'] = 0;
$log['message'] = 'Error- Invalid client- client value should not be blank';
self::writeLog($log);
continue;
}
$res = explode('.', $client);
// Get plugin name to call
$plugin = $res[0];
// Get class name
$class = $res[1];
$filePath = JPATH_SITE . '/plugins/tjqueue/' . $plugin . '/consumers/' . $class . '.php';
if (!file_exists($filePath))
{
$log['success'] = 0;
$log['message'] = "Error 404- Consumer class file doesn't exist:" . $filePath;
self::writeLog($log);
continue;
}
$mailcatcherFilePath = JPATH_SITE . '/plugins/system/mailcatcher/mailer.php';
if (!file_exists($mailcatcherFilePath))
{
$log['success'] = 0;
$log['message'] = "Error 404- mailcatcher class file doesn't exist:" . $mailcatcherFilePath;
self::writeLog($log);
continue;
}
try
{
require_once $mailcatcherFilePath;
require_once $filePath;
// Prepare class Name
$className = 'Tjqueue' . ucfirst($plugin) . ucfirst($class);
if (!class_exists($className))
{
$log['success'] = 0;
$log['message'] = $className . ' class not found';
self::writeLog($log);
continue;
}
$obj = new $className;
$result = $obj->consume($this->message);
$TJQueueConsume->acknowledge($result);
if ($result)
{
$log['success'] = 1;
$log['message'] = 'Message consumed successfully';
}
else
{
$log['success'] = 0;
$log['message'] = 'Message consumption failed';
}
self::writeLog($log);
}
catch (Exception $e)
{
$log['success'] = 0;
$log['message'] = $e->getMessage();
self::writeLog($log);
}
}
}
/**
* Plugin method with the same name as the event will be called automatically.
*
* @param array $data Result data
*
* @return array.
*
* @since 0.0.1
*/
public function writeLog($data)
{
$client = $this->message ? $this->message->getProperty('client') : null;
$messageId = $this->message ? $this->message->getMessageId() : null;
$this->out($data['message']);
// Add to log
$logFields = [
"messageId" => $messageId,
"message" => $data['message'],
"topic" => $this->options->topic,
"client" => $client
];
// Convert logFields to string implode by pipe(|)
$logMessage = implode(' | ', array_map(
function ($v, $k)
{
if (is_array($v))
{
return $k . '[]: ' . implode('&' . $k . '[]: ', $v);
}
else
{
return $k . ': ' . $v;
}
},
$logFields,
array_keys($logFields)
)
);
Log::addLogger(
array (
'text_file' => 'tjqueue_log_' . date("j.n.Y") . '.log.php',
'text_entry_format' => '{DATETIME} | {PRIORITY} | {MESSAGE}'
),
Log::ALL,
array($category = 'tjlogs')
);
$priority = $data['success'] == 1 ? Log::INFO : Log::ERROR;
Log::add($logMessage, $priority, $category = 'tjlogs');
}
}
JApplicationCli::getInstance('TJQueue')->loadConfiguration($argv);
JApplicationCli::getInstance('TJQueue')->execute();