我试图找到一篇文章,显示这个或类似的东西,但我不能。一切都是基于前端。我想要实现的是能够检查oracle中数据库表的实时更改,并且对于插入到此表中的每个新记录,我想为此运行一个进程。我遇到了https://dzone.com/articles/real-time-data-with-nodejs-socketio-and-oracle-dat,但它处理前端并不完全确定这是否可行。
目前我正在使用cronjob使用php来运行我的工作,但它会影响应用程序,TNS listener
正在通过连接请求收到垃圾邮件。我需要最小化我的整体应用程序连接。
下面是我到目前为止的情况。查询只运行一次。我想要它做的是持续运行,直到找到记录。一旦它找到了运行另一个脚本的记录
var http = require('http');
var express = require('express');
var bodyParser = require('body-parser');
var logger = require('morgan');
var serveStatic = require('serve-static');
var socketio = require('socket.io');
var oracledb = require('oracledb');
var dbConfig = require('./dbconfig.js');
var app;
var httpServer;
var io;
initWebServer();
function initWebServer() {
app = express();
httpServer = http.Server(app);
io = socketio(httpServer);
app.use(logger('combined'));
app.use(bodyParser.urlencoded({ extended: false }));
//Create 2 static file end points, 1 for public and 1 for bower_components
//app.use('/', serveStatic(__dirname + '/public'));
//app.use('/vendor', serveStatic(__dirname + '/bower_components'));
//Create end points for the application
//app.get('/api/super-cities', function(req, res, next) {
//getFavoriteCities(req, res, next, 'API');
//});
//app.post('/api/vote', postVote);
//Create an end point for the database
//app.get('/db', function(req, res, next) {
//getFavoriteCities(req, res, next, 'SOCKET_IO');
//});
httpServer.listen(3000, function() {
console.log('Webserver listening on localhost:3000');
getNewCommsMatrix();
io.on('change', function() {
getNewCommsMatrix();
});
});
}
function getNewCommsMatrix(){
oracledb.getConnection(
dbConfig,
function(err, connection) {
if (err) return next(err);
connection.execute(
'select * ' +
'from ( ' +
'SELECT wj.*, min(ts_created) over () as min_created ' +
'FROM CCP.worker_jobs wj WHERE flag IN (\'Q\',\'F\') AND (job = \'processCommsMatrixUpload\') ' +
'ORDER BY ts_created ' +
') ' +
'where ts_created = min_created',
{},//no binds
{
maxRows: 1,
outFormat: oracledb.OBJECT
},
function(err, results) {
if (err) {
return connection.release(function() {
next(err);
});
}
console.log(results.metaData);
console.log(results.rows);
connection.release(function(err) {
if (err) return next(err);
/*
if (context === 'API') {
res.send(results.rows);
} else if (context === 'SOCKET_IO') {
io.emit('change', results.rows);
res.send();
}
*/
});
}
);
}
);
}
我的cron每分钟运行一个bash脚本,它运行一个进程,其间为2-5秒,并最多生成10个。
的cron
CMD=$(ps aux | grep "cli-worker-processQueueCommsMatrixUpload.sh -e $ENV_NAME >>" | grep -v grep | wc -l)
if [[ "$CMD" -lt 10 ]]; then
COUNTER=1
if [[ $ENV_NAME == test ]]; then
while true ; do
if [[ "$COUNTER" -eq 180 ]]; then
exit 0
fi
/opt/SP/php7/bin/php /var/httpd/ccp-test/ccp-test.domain.com/scripts/cli-worker-manager.php processQueueCommsMatrixUpload >> /var/httpd/ccp-test/ccp-test.domain.com/scripts/logs/cli-worker-processQueueCommsMatrixUpload.log 2>&1 &
sleep 3;
COUNTER=$[$COUNTER +1]
done
fi
fi
CLI-工人manager.php
for($i=0;$i<100;$i++){
$arg = $argv[1];
$workerManager = new WorkerManager($arg);
$workerManager->{$arg}();
}
在功能
下运行public function processQueueCommsMatrixUpload() {
$workerJobs = new WorkerJobs;
$dbh_ro = new DatabaseConnection;
$queue = $workerJobs->getOldestJob('processCommsMatrixUpload');
if(count($queue) > 0) {
$job = $queue[0];
$filecache = CCPSHARED . "/locks/".$job['id'];
$condition = [
'ID' => $job['id'],
'FLAG' => ['IN', [ 'Q', 'F' ]]
];
if(($job['flag'] == 'F') && ($job['attempts'] = 3)) {
$this->log->info('job #' . $job['id'] . ' Failed attempts 3 reached ');
$update = [
'SERVER' => gethostname(),
'FLAG' => 'T', // T = terminated
'ATTEMPTS' => 3,
'TS_UPDATED' => 'systimestamp',
];
$result = $workerJobs->updateTable($update, $condition);
\unlink($filecache);
return true;
}
//recheck to see other worker has not processed this file
$nextjob = $workerJobs->recheckNextJob((int) $job['id'],'processCommsMatrixUpload');
if(count($nextjob) > 0){
//check filecache or create file cache with
if(\file_exists($filecache)){
$this->log->warn('file cache exists, processCommsMatrixUpload has been picked up by other server #' . $job['id'] );
return true;
}
\touch($filecache);
$attempts = (empty($nextjob[0]['attempts'])) ? 1 : $nextjob[0]['attempts'];
$update = [
'SERVER' => gethostname(),
'FLAG' => 'P',
'ATTEMPTS' => $attempts,
'TS_UPDATED' => 'systimestamp',
];
$result = $workerJobs->updateTable($update, $condition);
if(is_string($result)) {
$this->log->error('worker job #' . $job['id'] . ' update failure.');
$notification = new Notification();
$body = 'worker job #' . $job['id'] . ' update failure.';
$notification->cliJobs($notification->getSystemEmail(), ['user@domain.com',], $body);
} else {
sleep(rand(2 , 10)); //add random delay between each job from 1-3 secs
$ts_start = strtoupper(date('d-M-y h.i.s') ) . substr((string)microtime(), 1, 8);
$this->log->info('starting job #' . $job['id'] . ' at ' . $ts_start);
$this->{$job['job']}($job);
if(file_exists($filecache)){
\unlink($filecache);
}
$ts_end = strtoupper(date('d-M-y h.i.s')) . substr((string)microtime(), 1, 8);
$this->log->info('finishing job #' . $job['id'] . ' at ' . $ts_end);
$this->log->info('job #' . $job['id'] . ' processing time : '. (strtotime($ts_end) - strtotime($ts_start)) . ' seconds');
}
}else{
$this->log->warn('Job (processCommsMatrixUpload) has been picked up by other server #' . $job['id'] );
//$notification = new Notification();
//$body = 'Job (processCommsMatrixUpload) has been picked up by other server #' . $job['id'];
//$notification->cliJobs($notification->getSystemEmail(), ['user@domain.com'], $body);
}
}
$workerJobs->ccpRO->disconnect();
return true;
}
我不能以db触发器运行,因为我需要它来处理php中的查询结果集,除非有办法从db调用php脚本?
问题不在于数据库连接,它与TNS listener
一起引发错误,因为它将其视为DDOS
攻击。
我以为我可以打开一个连接而不是每秒打开一个新连接。