制作一个text协议的worker来消费新增节点的任务
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
use Workerman\Worker;
|
use Workerman\Worker;
|
||||||
|
use \Workerman\Connection\AsyncTcpConnection;
|
||||||
use Workerman\Connection\TcpConnection;
|
use Workerman\Connection\TcpConnection;
|
||||||
use Workerman\Protocols\Http\Request;
|
use Workerman\Protocols\Http\Request;
|
||||||
use Workerman\Protocols\Http\Response;
|
use Workerman\Protocols\Http\Response;
|
||||||
@@ -12,14 +13,16 @@ $options = getopt('h::', ['server_name:', 'user:', 'password:', 'help::']);
|
|||||||
init_db($options['server_name'], $options['user'], $options['password']);
|
init_db($options['server_name'], $options['user'], $options['password']);
|
||||||
|
|
||||||
$worker = new Worker('http://0.0.0.0:8888');
|
$worker = new Worker('http://0.0.0.0:8888');
|
||||||
$worker -> name = 'CaptureWorker';
|
$worker -> name = 'EntryPoint';
|
||||||
$worker -> count = 20;
|
$worker -> count = 20;
|
||||||
|
|
||||||
$worker -> onWorkerStart = function(Worker $worker) {
|
$worker -> onWorkerStart = function(Worker $worker) {
|
||||||
global $options, $dbconn;
|
global $options, $dbconn;
|
||||||
|
|
||||||
$dbconn = pg_connect(sprintf( "host=%s dbname=scada user=%s password=%s",
|
$dbconn = pg_connect(sprintf(
|
||||||
$options['server_name'], $options['user'], $options['password']));
|
"host=%s dbname=scada user=%s password=%s",
|
||||||
|
$options['server_name'], $options['user'], $options['password']
|
||||||
|
));
|
||||||
};
|
};
|
||||||
|
|
||||||
$worker -> onMessage = function(TcpConnection $connection, Request $request) {
|
$worker -> onMessage = function(TcpConnection $connection, Request $request) {
|
||||||
@@ -61,24 +64,36 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) {
|
|||||||
// 因为pg_insert()不需要action字段
|
// 因为pg_insert()不需要action字段
|
||||||
$action = $post -> action;
|
$action = $post -> action;
|
||||||
unset($post -> action);
|
unset($post -> action);
|
||||||
|
|
||||||
|
if ($action === 'add_node') {
|
||||||
|
$task_connection = new AsyncTcpConnection('Text://127.0.0.1:1888');
|
||||||
|
$task_data = array(
|
||||||
|
'action' => 'add_node',
|
||||||
|
'data' => $post,
|
||||||
|
);
|
||||||
|
$task_connection -> send(json_encode($task_data));
|
||||||
|
$task_connection -> onMessage = function(AsyncTcpConnection $task_connection, $task_result) use ($connection) {
|
||||||
|
$task_connection -> close();
|
||||||
|
$connection -> send($task_result);
|
||||||
|
};
|
||||||
|
// 执行异步连接
|
||||||
|
$task_connection->connect();
|
||||||
|
} else {
|
||||||
$enode_configure = new ENodeConfigure($dbconn, post: $post);
|
$enode_configure = new ENodeConfigure($dbconn, post: $post);
|
||||||
$res = $enode_configure -> $action();
|
$res = $enode_configure -> $action();
|
||||||
|
|
||||||
if ($res === true)
|
if ($res === true)
|
||||||
$connection -> send(json_encode(array(
|
$connection -> send(json_encode(array(
|
||||||
'code' => 0,
|
'code' => 0,
|
||||||
'msg' => 'Success'
|
'msg' => 'Success'
|
||||||
)));
|
)));
|
||||||
else if ($res === "REPLICATED")
|
|
||||||
$connection -> send(json_encode(array(
|
|
||||||
'code' => 1,
|
|
||||||
'msg' => '同一工序单元内的节点编码不可重复!'
|
|
||||||
)));
|
|
||||||
else if ($res === false) {
|
else if ($res === false) {
|
||||||
$connection -> send(json_encode(array(
|
$connection -> send(json_encode(array(
|
||||||
'code' => 1,
|
'code' => 1,
|
||||||
'msg' => '服务器内部逻辑错误,请联系开发者!'
|
'msg' => '服务器内部逻辑错误,请联系开发者!'
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else if ($post -> action === 'set_node_data') {
|
} else if ($post -> action === 'set_node_data') {
|
||||||
$data_capture = new EDataCapture($dbconn, post: $post);
|
$data_capture = new EDataCapture($dbconn, post: $post);
|
||||||
if ($data_capture -> check_res === 'WRONG_WORKING_SUBCLASS') {
|
if ($data_capture -> check_res === 'WRONG_WORKING_SUBCLASS') {
|
||||||
@@ -206,4 +221,44 @@ $worker -> onMessage = function(TcpConnection $connection, Request $request) {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
$consumer = new Worker('Text://0.0.0.0:1888');
|
||||||
|
$consumer -> name = 'Consumer';
|
||||||
|
|
||||||
|
$consumer -> onWorkerStart = function(Worker $consumer) {
|
||||||
|
global $options, $consumer_dbconn;
|
||||||
|
|
||||||
|
$consumer_dbconn = pg_connect(sprintf(
|
||||||
|
"host=%s dbname=scada user=%s password=%s",
|
||||||
|
$options['server_name'], $options['user'], $options['password']
|
||||||
|
));
|
||||||
|
};
|
||||||
|
|
||||||
|
$consumer -> onMessage = function(TcpConnection $connection, $task_data) {
|
||||||
|
global $consumer_dbconn;
|
||||||
|
|
||||||
|
$task_data = json_decode($task_data);
|
||||||
|
|
||||||
|
if ($task_data -> action === 'add_node') {
|
||||||
|
$enode_configure = new ENodeConfigure($consumer_dbconn, post: $task_data -> data);
|
||||||
|
$res = $enode_configure -> add_node();
|
||||||
|
|
||||||
|
if ($res === true)
|
||||||
|
$connection -> send(json_encode(array(
|
||||||
|
'code' => 0,
|
||||||
|
'msg' => 'Success'
|
||||||
|
)));
|
||||||
|
else if ($res === "REPLICATED")
|
||||||
|
$connection -> send(json_encode(array(
|
||||||
|
'code' => 1,
|
||||||
|
'msg' => '同一工序单元内的节点编码不可重复!'
|
||||||
|
)));
|
||||||
|
else if ($res === false) {
|
||||||
|
$connection -> send(json_encode(array(
|
||||||
|
'code' => 1,
|
||||||
|
'msg' => '服务器内部逻辑错误,请联系开发者!'
|
||||||
|
)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
Worker::runAll();
|
Worker::runAll();
|
||||||
|
|||||||
Reference in New Issue
Block a user