Skip to content

Commit 0667634

Browse files
committed
Added lock and unlock
1 parent a531735 commit 0667634

File tree

5 files changed

+133
-66
lines changed

5 files changed

+133
-66
lines changed

.idea/workspace.xml

Lines changed: 101 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,8 @@
1616
}
1717
},
1818
"minimum-stability": "dev",
19-
"require": {}
19+
"require": {
20+
"ext-pgsql": "*",
21+
"ext-json": "*"
22+
}
2023
}

src/WorkerJS/PHPClient/HTTPClientTask.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ public function sendTask() {
4141

4242
$payload['webhook'] = $webhook;
4343

44-
$taskResponse = $this->sendRequest($url, $payload);
44+
$this->client->getTaskStore()->lock();
4545

46+
$taskResponse = $this->sendRequest($url, $payload);
4647
$this->client->getTaskStore()->setTask($taskResponse["taskID"], $this);
48+
49+
$this->client->getTaskStore()->unlock();
50+
4751
return $taskResponse["taskID"];
4852
}
4953

src/WorkerJS/PHPClient/PostgresTaskStore.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,13 @@ public function getTask($taskID) {
3737
public function setTask($taskID, Task $task) {
3838
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()))."')");
3939
}
40+
41+
public function lock() {
42+
pg_query($this->connection,"BEGIN WORK; LOCK TABLE tasks IN ACCESS EXCLUSIVE MODE;");
43+
}
44+
45+
public function unlock() {
46+
pg_query($this->connection,"COMMIT WORK;");
47+
}
4048
}
4149

src/WorkerJS/PHPClient/TaskStore.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ public function __construct($client) {
1717
$this->client = $client;
1818
}
1919

20+
/**
21+
* @param $taskID
22+
* @param Task $task
23+
* @return mixed
24+
*/
2025
abstract public function setTask($taskID, Task $task);
2126

2227
/**
@@ -25,5 +30,15 @@ abstract public function setTask($taskID, Task $task);
2530
* @throws TaskNotFoundException
2631
*/
2732
abstract public function getTask($taskID);
33+
34+
/**
35+
* @return mixed
36+
*/
37+
abstract public function lock();
38+
39+
/**
40+
* @return mixed
41+
*/
42+
abstract public function unlock();
2843
}
2944

0 commit comments

Comments
 (0)