Skip to content

Commit 94a3f47

Browse files
authored
Merge pull request #602 from ProgressPlanner/filip/alternative-lock
Alternative (better) lock when adding new tasks
2 parents 1905b20 + 4ea34b6 commit 94a3f47

1 file changed

Lines changed: 61 additions & 53 deletions

File tree

classes/class-suggested-tasks-db.php

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,22 @@ 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;
39+
$lock_key = 'prpl_task_lock_' . $data['task_id'];
40+
$lock_value = \time();
41+
42+
// add_option will return false if the option is already there.
43+
if ( ! \add_option( $lock_key, $lock_value, '', false ) ) {
44+
$current = \get_option( $lock_key );
45+
46+
// If lock is stale (older than 30s), take over.
47+
if ( $current && ( $current < \time() - 30 ) ) {
48+
\update_option( $lock_key, $lock_value );
49+
} else {
50+
error_log( 'Lock for: ' . $data['task_id'] ); // TODO: remove this after testing.
51+
return 0; // Other process is using it.
52+
}
4553
}
4654

47-
// Set lock transient.
48-
\set_transient( $transient_key, true, 5 );
49-
5055
// Check if we have an existing task with the same title.
5156
$posts = $this->get_tasks_by(
5257
[
@@ -64,7 +69,7 @@ public function add( $data ) {
6469

6570
// If we have an existing task, skip.
6671
if ( ! empty( $posts ) ) {
67-
\delete_transient( $transient_key );
72+
\delete_option( $lock_key );
6873
return $posts[0]->ID;
6974
}
7075

@@ -100,57 +105,60 @@ public function add( $data ) {
100105
break;
101106
}
102107

103-
$post_id = \wp_insert_post( $args );
108+
try {
109+
$post_id = \wp_insert_post( $args );
104110

105-
// Add terms if they don't exist.
106-
foreach ( [ 'category', 'provider_id' ] as $context ) {
107-
$taxonomy_name = \str_replace( '_id', '', $context );
108-
$term = \get_term_by( 'name', $data[ $context ], "prpl_recommendations_$taxonomy_name" );
109-
if ( ! $term ) {
110-
\wp_insert_term( $data[ $context ], "prpl_recommendations_$taxonomy_name" );
111+
// Add terms if they don't exist.
112+
foreach ( [ 'category', 'provider_id' ] as $context ) {
113+
$taxonomy_name = \str_replace( '_id', '', $context );
114+
$term = \get_term_by( 'name', $data[ $context ], "prpl_recommendations_$taxonomy_name" );
115+
if ( ! $term ) {
116+
\wp_insert_term( $data[ $context ], "prpl_recommendations_$taxonomy_name" );
117+
}
111118
}
112-
}
113-
114-
// Set the task category.
115-
\wp_set_post_terms( $post_id, $data['category'], 'prpl_recommendations_category' );
116-
117-
// Set the task provider.
118-
\wp_set_post_terms( $post_id, $data['provider_id'], 'prpl_recommendations_provider' );
119119

120-
// Set the task parent.
121-
if ( ! empty( $data['parent'] ) ) {
122-
$parent = \get_post( $data['parent'] );
123-
if ( $parent ) {
124-
\wp_update_post(
125-
[
126-
'ID' => $post_id,
127-
'post_parent' => $parent->ID,
128-
]
129-
);
120+
// Set the task category.
121+
\wp_set_post_terms( $post_id, $data['category'], 'prpl_recommendations_category' );
122+
123+
// Set the task provider.
124+
\wp_set_post_terms( $post_id, $data['provider_id'], 'prpl_recommendations_provider' );
125+
126+
// Set the task parent.
127+
if ( ! empty( $data['parent'] ) ) {
128+
$parent = \get_post( $data['parent'] );
129+
if ( $parent ) {
130+
\wp_update_post(
131+
[
132+
'ID' => $post_id,
133+
'post_parent' => $parent->ID,
134+
]
135+
);
136+
}
130137
}
131-
}
132138

133-
// Set other meta.
134-
$default_keys = [
135-
'title',
136-
'description',
137-
'status',
138-
'category',
139-
'provider_id',
140-
'parent',
141-
'order',
142-
'post_status',
143-
];
144-
foreach ( $data as $key => $value ) {
145-
if ( \in_array( $key, $default_keys, true ) ) {
146-
continue;
139+
// Set other meta.
140+
$default_keys = [
141+
'title',
142+
'description',
143+
'status',
144+
'category',
145+
'provider_id',
146+
'parent',
147+
'order',
148+
'post_status',
149+
];
150+
foreach ( $data as $key => $value ) {
151+
if ( \in_array( $key, $default_keys, true ) ) {
152+
continue;
153+
}
154+
155+
\update_post_meta( $post_id, "prpl_$key", $value );
147156
}
148-
149-
\update_post_meta( $post_id, "prpl_$key", $value );
157+
} finally {
158+
// Delete the lock. This executes always.
159+
\delete_option( $lock_key );
150160
}
151161

152-
\delete_transient( $transient_key );
153-
154162
return $post_id;
155163
}
156164

0 commit comments

Comments
 (0)