Skip to content

Commit eaba090

Browse files
committed
INIT
1 parent 266c513 commit eaba090

File tree

4 files changed

+143
-44
lines changed

4 files changed

+143
-44
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- [Redis Insight] (http://localhost:8001/)
1111
- [WebSocket Server] (http://localhost:3000)
1212
- [TaskQueue Page] (http://localhost:8888)
13+
- [PhpMyAdmin] (http://localhost:8082)
1314

1415
## INSTALLATION
1516
```sh

docker-compose.yml

+50-26
Original file line numberDiff line numberDiff line change
@@ -56,31 +56,31 @@ services:
5656
- app
5757
- redis
5858

59-
# celery_worker_02:
60-
# container_name: celery_worker_02
61-
# build: .
62-
# command: celery -A celery_worker.celery worker --loglevel=info
63-
# volumes:
64-
# - .:/app
65-
# environment:
66-
# - CELERY_BROKER_URL=${CELERY_BROKER_URL}
67-
# - CELERY_RESULT_BACKEND=${CELERY_RESULT_BACKEND}
68-
# depends_on:
69-
# - app
70-
# - redis
59+
celery_worker_02:
60+
container_name: celery_worker_02
61+
build: .
62+
command: celery -A celery_worker.celery worker --loglevel=info
63+
volumes:
64+
- .:/app
65+
environment:
66+
- CELERY_BROKER_URL=${CELERY_BROKER_URL}
67+
- CELERY_RESULT_BACKEND=${CELERY_RESULT_BACKEND}
68+
depends_on:
69+
- app
70+
- redis
7171

72-
# celery_worker_03:
73-
# container_name: celery_worker_03
74-
# build: .
75-
# command: celery -A celery_worker.celery worker --loglevel=info
76-
# volumes:
77-
# - .:/app
78-
# environment:
79-
# - CELERY_BROKER_URL=${CELERY_BROKER_URL}
80-
# - CELERY_RESULT_BACKEND=${CELERY_RESULT_BACKEND}
81-
# depends_on:
82-
# - app
83-
# - redis
72+
celery_worker_03:
73+
container_name: celery_worker_03
74+
build: .
75+
command: celery -A celery_worker.celery worker --loglevel=info
76+
volumes:
77+
- .:/app
78+
environment:
79+
- CELERY_BROKER_URL=${CELERY_BROKER_URL}
80+
- CELERY_RESULT_BACKEND=${CELERY_RESULT_BACKEND}
81+
depends_on:
82+
- app
83+
- redis
8484

8585
flower:
8686
container_name: flower
@@ -95,8 +95,8 @@ services:
9595
- app
9696
- redis
9797
- celery_worker_01
98-
# - celery_worker_02
99-
# - celery_worker_03
98+
- celery_worker_02
99+
- celery_worker_03
100100

101101
redisinsight:
102102
container_name: redisinsight
@@ -106,6 +106,30 @@ services:
106106
volumes:
107107
- redisinsight:/db
108108

109+
mysql:
110+
container_name: mysql
111+
image: mariadb:10.3
112+
restart: always
113+
environment:
114+
MYSQL_ROOT_PASSWORD: notSecureChangeMe
115+
MYSQL_DATABASE: test
116+
ports:
117+
- "3306:3306"
118+
119+
phpmyadmin:
120+
container_name: phpmyadmin
121+
image: phpmyadmin
122+
restart: always
123+
ports:
124+
- 8082:80
125+
environment:
126+
- PMA_HOST=mysql
127+
- PMA_USER=root
128+
- PMA_PASSWORD=notSecureChangeMe
129+
- PMA_DATABASE=test
130+
depends_on:
131+
- mysql
132+
109133
volumes:
110134
redis: null
111135
redisinsight: null

src/api/api.php

+59
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
<?php
2+
try {
3+
$db_host = "mysql";
4+
$db_db = "test";
5+
$db_user = "root";
6+
$db_pass = "notSecureChangeMe";
7+
$dbh = new PDO("mysql:host=$db_host;port=3306;dbname=$db_db", $db_user, $db_pass, array(
8+
PDO::ATTR_EMULATE_PREPARES => true,
9+
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
10+
PDO::ATTR_PERSISTENT => true
11+
));
12+
} catch (PDOException $e) {
13+
die('DB Connection Error');
14+
};
15+
216
switch($_REQUEST['action']) {
317
case 'login':
418
// DO LOGIN
@@ -58,6 +72,51 @@
5872
};
5973
header('Content-Type: application/json; charset=utf-8');
6074
echo json_encode($ret);
75+
break;
76+
case 'tq_log':
77+
$headers = getallheaders();
78+
$json = file_get_contents('php://input');
79+
$data = json_decode($json);
80+
81+
if($headers['Authorization'] == 'Bearer THISIASASUPERSECRETKEY') {
82+
83+
$sql = "
84+
CREATE TABLE IF NOT EXISTS `taskgroups` (
85+
`id` bigint(0) NOT NULL AUTO_INCREMENT,
86+
`transdate` datetime(0) NOT NULL,
87+
`taskgroup` varchar(255) NULL,
88+
`channel_id` varchar(255) NULL,
89+
`task_id` varchar(255) NULL,
90+
`taskname` varchar(255) NULL,
91+
`success` tinyint(1) NULL DEFAULT 0,
92+
`step` int(11) NULL,
93+
`completed` tinyint(1) NULL DEFAULT 0,
94+
`url` varchar(255) NULL,
95+
`data` json NULL,
96+
PRIMARY KEY (`id`) ,
97+
UNIQUE INDEX `channel_id`(`taskgroup`, `channel_id`)
98+
);
99+
";
100+
$dbh->prepare($sql)->execute();
101+
102+
$dbh->prepare("
103+
INSERT INTO taskgroups (transdate, taskgroup, channel_id, task_id, taskname, success, step, completed, url, data)
104+
VALUES (NOW(), :taskgroup, :channel_id, :task_id, :taskname, :success, :step, :completed, :url, :data)
105+
ON DUPLICATE KEY UPDATE transdate = NOW(), taskgroup = :taskgroup, channel_id = :channel_id, task_id = :task_id, taskname = :taskname, success = :success, step = :step, completed = :completed, url = :url, data = :data;
106+
")->execute(array(
107+
':taskgroup' => $data->taskgroup,
108+
':channel_id' => $data->channel,
109+
':task_id' => $data->task_id,
110+
':taskname' => $data->taskname,
111+
':success' => $data->success,
112+
':step' => $data->step,
113+
':completed' => $data->completed,
114+
':url' => $data->url,
115+
':data' => json_encode($data->data)
116+
));
117+
118+
};
119+
61120
break;
62121
default:
63122
$data = array(

src/api/callback.php

+33-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
$redis = new \Redis;
33
$redis->connect('redis', 6379);
44

5+
$tq_url = 'http://app:8000';
56
$taskgroup = $_REQUEST['action'];
67

78
switch($taskgroup) {
@@ -11,7 +12,7 @@
1112
$response = $data->response->body;
1213
$request = $data->request->body;
1314
$channel_id = ($data->channel_id)?$data->channel_id:$data->task_id;
14-
15+
1516
// $redis->publish($channel_id,json_encode($data));
1617
// error_log('################# LOGIN BEGIN #################');
1718
// error_log(print_r($data, TRUE));
@@ -22,8 +23,7 @@
2223
case 'test-login':
2324
if($response->status=='success'){
2425
// if login is VALID redirect to SET SESSION at /test-login
25-
$url = 'http://app:8000';
26-
$ret = curlPost($url, array(
26+
$ret = curlPost($tq_url, array(
2727
"taskname" => "test-login01",
2828
"url" => "http://api/api.php?action=login01",
2929
"http_method" => "POST",
@@ -37,16 +37,20 @@
3737
));
3838
$response->status = 'pending';
3939
$redis->publish($channel_id, json_encode($response));
40+
tq_log($taskgroup,$data,1,1,0);
4041
}else{
4142
// if login is INVALID return FAILURE
4243
$redis->publish($channel_id, json_encode($response));
44+
tq_log($taskgroup,$data,1,1,1);
4345
};
4446
break;
4547
case 'test-login01':
4648
$redis->publish($channel_id,json_encode($response));
49+
tq_log($taskgroup,$data,1,1,1);
4750
break;
4851
};
4952
} else {
53+
tq_log($taskgroup,$data,0,0,0);
5054
// LOG FAILED
5155
// INSERT ALL FAILURES TO MYSQL DB
5256
};
@@ -70,6 +74,8 @@ function curlPost($url, $data) {
7074
curl_setopt($ch, CURLOPT_POST, 1);
7175
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
7276
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
77+
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
78+
curl_setopt($ch, CURLOPT_TCP_FASTOPEN, 1);
7379
$ret = curl_exec($ch);
7480
curl_close ($ch);
7581
$ret = json_decode($ret, true);
@@ -79,22 +85,31 @@ function curlPost($url, $data) {
7985
return $ret;
8086
}
8187

82-
function tq($taskgroup,$message,$data,$completed=1,$step=1,$url_next=''){
83-
$response = $data->response;
88+
function tq_log($taskgroup,$data,$success,$step=1,$completed=1){
89+
global $tq_url;
90+
$response = $data->response->body;
8491
$request = $data->request;
85-
$ret = array(
86-
'message' => $message,
87-
'transdate' => date('Y-m-d H:i:s'),
88-
'channel' => $data->channel,
89-
'task_id' => $data->task_id,
90-
'taskgroup' => $taskgroup,
91-
'taskname' => $request->taskname,
92-
'completed' => $completed,
93-
'step' => $step,
94-
'url' => $request->url,
95-
'url_next' => $url_next,
96-
'data' => $data
97-
);
92+
93+
curlPost($tq_url, array(
94+
"taskname" => 'tq_log',
95+
"url" => 'http://api/api.php?action=tq_log',
96+
"http_method" => "POST",
97+
"headers" => array(
98+
"Content-Type" => "application/json",
99+
"Authorization" => "Bearer THISIASASUPERSECRETKEY"
100+
),
101+
"body" => array(
102+
'taskgroup' => $taskgroup,
103+
'channel' => $data->channel_id,
104+
'task_id' => $data->task_id,
105+
'taskname' => $request->taskname,
106+
'success' => $success,
107+
'step' => $step,
108+
'completed' => $completed,
109+
'data' => $data
110+
)
111+
));
112+
98113
return $ret;
99114
};
100115
?>

0 commit comments

Comments
 (0)