Skip to content

Commit 0290451

Browse files
authored
Merge pull request #515 from ProgressPlanner/filip/v16/lock-adding-tasks
Add lock while task is being added
2 parents 255115d + ac328d8 commit 0290451

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

classes/class-suggested-tasks-db.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,17 @@ public function add( $data ) {
3636
return 0;
3737
}
3838

39+
// Set lock transient.
40+
$transient_key = 'prpl_task_lock_' . $data['task_id'];
41+
42+
// Check if the task is already being processed.
43+
if ( \get_transient( $transient_key ) ) {
44+
return 0;
45+
}
46+
47+
// Set lock transient.
48+
\set_transient( $transient_key, true, 5 );
49+
3950
// Check if we have an existing task with the same title.
4051
$posts = $this->get_tasks_by(
4152
[
@@ -53,6 +64,7 @@ public function add( $data ) {
5364

5465
// If we have an existing task, skip.
5566
if ( ! empty( $posts ) ) {
67+
\delete_transient( $transient_key );
5668
return $posts[0]->ID;
5769
}
5870

@@ -137,6 +149,8 @@ public function add( $data ) {
137149
\update_post_meta( $post_id, "prpl_$key", $value );
138150
}
139151

152+
\delete_transient( $transient_key );
153+
140154
return $post_id;
141155
}
142156

0 commit comments

Comments
 (0)