Skip to content

Commit af06e5a

Browse files
committed
Ajmo se pomoliti
1 parent a9074f1 commit af06e5a

File tree

7 files changed

+22
-17
lines changed

7 files changed

+22
-17
lines changed

src/WorkerJS/PHPClient/HTTPClientTask.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ class HTTPClientTask extends Task{
1616

1717
public function __construct($client, $name)
1818
{
19+
parent::__construct($client, $name);
1920
$this->client = $client;
2021
$this->name = $name;
2122
}
@@ -27,16 +28,18 @@ public function sendTask(){
2728

2829
$taskResponse = $this->sendRequest($url, $payload);
2930

30-
$this->client->getTaskStore()->setTask($taskResponse["taskID"], $this->getTask());
31+
$this->client->getTaskStore()->setTask($taskResponse["taskID"], $this);
3132
return $taskResponse;
3233
}
3334

3435
public function sendMessage($payload){
3536
$url = $this->client->getSetting("api_base")."/message";
37+
$request = [
38+
"taskID" => $this->task["taskID"],
39+
"message" => $payload,
40+
];
3641

37-
$payload->taskID = $this->task->taskID;
38-
39-
return $this->sendRequest($url, $payload);
42+
return $this->sendRequest($url, $request);
4043
}
4144

4245
public function preProcess(){
@@ -49,7 +52,7 @@ private function sendRequest($url, $payload){
4952
curl_setopt($ch, CURLOPT_URL, $url);
5053
curl_setopt($ch, CURLOPT_POST, true);
5154
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($payload));
52-
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
55+
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
5356
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
5457

5558
$result = curl_exec($ch);

src/WorkerJS/PHPClient/MySQLTaskStore.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ public function getTask($taskID){
2525
if(mysqli_num_rows($result) === 0){
2626
throw new \Exception("Task $taskID not found.");
2727
} else {
28-
return mysqli_fetch_assoc($result);
28+
$task = mysqli_fetch_assoc($result)["task"];
29+
return json_decode($task);
2930
}
3031
}
3132

src/WorkerJS/PHPClient/PostgresTaskStore.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ public function __construct($client){
2121
}
2222

2323
public function getTask($taskID){
24-
$result = pg_query($this->connection, "SELECT task FROM tasks WHERE \"taskID\" = ".intval($taskID));
24+
$result = pg_query($this->connection, "SELECT task FROM tasks WHERE \"taskID\" = '".pg_escape_string($this->connection, $taskID)."'");
2525

2626
if(pg_num_rows($result) === 0){
2727
throw new \Exception("Task $taskID not found.");
2828
} else {
29-
return pg_fetch_assoc($result);
29+
$task = pg_fetch_assoc($result)["task"];
30+
31+
return json_decode($task, true);
3032
}
3133
}
3234

3335
public function setTask($taskID, Task $task){
34-
pg_query($this->connection, "INSERT INTO tasks (\"taskID\", task) VALUES (".intval($taskID).", '".pg_escape_string($this->connection, json_encode($task->getTask()))."')");
36+
pg_query($this->connection, "INSERT INTO tasks (\"taskID\", task) VALUES ('".pg_escape_string($this->connection, $taskID)."', '".pg_escape_string($this->connection, json_encode($task->getTask()))."')");
3537
}
3638
}

src/WorkerJS/PHPClient/Task.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,12 @@
1010

1111
abstract class Task
1212
{
13-
private $name;
1413
private $client;
1514
protected $task;
1615

1716
public function __construct($client, $params)
1817
{
19-
if($params instanceof string) {
18+
if( is_string($params) ) {
2019
$name = $params;
2120
$this->task = [
2221
"name" => null,
@@ -48,6 +47,6 @@ private function preProcessParams(){
4847
}
4948

5049
public function getHandlerName() {
51-
return $this->name;
50+
return $this->task["name"];
5251
}
5352
}

src/WorkerJS/PHPClient/TaskMessageRequestHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,18 @@ public function __construct(Client $client){
1717

1818
public function handleRequest(string $body){
1919
$body = json_decode($body);
20-
2120
//TODO: Check protocol
2221

2322
$taskID = $body->taskID;
2423

2524
$taskStore = $this->client->getTaskStore();
2625

2726
$task = $taskStore->getTask($taskID);
27+
$task["taskID"] = $taskID;
2828

2929
$task = $this->client->newTask($task);
3030

31-
$handlerName = $task->getHandlerName();
31+
$handlerName = $body->name;
3232

3333
try {
3434
$this->client->getTaskMessageRouter()->call($handlerName, $task, $body);

src/WorkerJS/PHPClient/TaskMessageRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ private static function getHandlerByName($name)
4141
if(isset($taskMessageRouter->handlers[$name])) {
4242
return $taskMessageRouter->handlers[$name];
4343
} else {
44-
throw \Exception("Handler for $name is not registered. ");
44+
throw new \Exception("Handler for $name is not registered. ");
4545
}
4646
}
4747

src/example.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@
1818
use \WorkerJS\PHPClient;
1919

2020
$client = new PHPClient\Client([]);
21+
$taskMessageRequestHandler = new PHPClient\TaskMessageRequestHandler($client);
2122

2223
// Message receiver
2324

2425
$router->get("/webhook", function($request, $response){
25-
$taskMessageRequestHandler = new PHPClient\TaskMessageRequestHandler($client);
26+
global $taskMessageRequestHandler;
2627

2728
$taskMessageRequestHandler->handleRequest($request->body);
2829
});
@@ -53,4 +54,3 @@ public function handle(\WorkerJS\PHPClient\Task $task, $params)
5354
]);
5455

5556
$task->sendTask();
56-

0 commit comments

Comments
 (0)