Skip to content

Commit 42c07d2

Browse files
authored
fix: Less flaky kanban_helper::set_entry() (#98)
this should be less susceptible to race conditions during high load
1 parent 819d567 commit 42c07d2

1 file changed

Lines changed: 10 additions & 14 deletions

File tree

lbplanner/classes/helpers/kanban_helper.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,18 @@ public static function get_entry(int $userid, int $cmid): ?kanbanentry {
6969
* @param kanbanentry $entry the entry to set
7070
*/
7171
public static function set_entry(kanbanentry $entry): void {
72-
global $DB, $CFG;
72+
global $DB;
7373

74-
try {
74+
if ($entry->column === KANBANCOL_TYPE_NUMERIC::BACKLOG) {
7575
$DB->delete_records(self::TABLE, ['userid' => $entry->userid, 'cmid' => $entry->cmid]);
76-
} catch (\dml_exception $e) {
77-
// Needed for low-reporting contexts such as a prod server.
78-
echo 'error while trying to delete preexisting kanban entries: '
79-
. $e->getMessage()
80-
. "\nFurther info:\n"
81-
. $e->debuginfo;
82-
var_dump($entry);
83-
throw $e;
84-
}
85-
if ($entry->column !== KANBANCOL_TYPE_NUMERIC::BACKLOG) {
86-
$newid = $DB->insert_record(self::TABLE, $entry->prepare_for_db(), true);
87-
$entry->set_fresh($newid);
76+
} else {
77+
$id = $DB->get_field(self::TABLE, 'id', ['userid' => $entry->userid, 'cmid' => $entry->cmid], IGNORE_MISSING);
78+
if ($id === false) {
79+
$id = $DB->insert_record(self::TABLE, $entry->prepare_for_db(), true);
80+
} else {
81+
$DB->set_field(self::TABLE, 'selectedcolumn', $entry->column, ['id' => $id]);
82+
}
83+
$entry->set_fresh($id);
8884
}
8985
}
9086
}

0 commit comments

Comments
 (0)