aboutsummaryrefslogtreecommitdiffstats
path: root/models/webhook
diff options
context:
space:
mode:
authorzeripath <art27@cantab.net>2022-11-23 14:10:04 +0000
committerGitHub <noreply@github.com>2022-11-23 22:10:04 +0800
commit787f6c32278f1fb814ce405e960dd7c50a7d2b3b (patch)
tree48879d3b6eecebc20286e32e6f22800b826ffc68 /models/webhook
parent4c00d8f916f4a347d04001418b766f1849214181 (diff)
downloadgitea-787f6c32278f1fb814ce405e960dd7c50a7d2b3b.tar.gz
gitea-787f6c32278f1fb814ce405e960dd7c50a7d2b3b.zip
Ensure that Webhook tasks are not double delivered (#21558)
When re-retrieving hook tasks from the DB double check if they have not been delivered in the meantime. Further ensure that tasks are marked as delivered when they are being delivered. In addition: * Improve the error reporting and make sure that the webhook task population script runs in a separate goroutine. * Only get hook task IDs out of the DB instead of the whole task when repopulating the queue * When repopulating the queue make the DB request paged Ref #17940 Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: delvh <dev.lh@web.de> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
Diffstat (limited to 'models/webhook')
-rw-r--r--models/webhook/hooktask.go22
1 files changed, 19 insertions, 3 deletions
diff --git a/models/webhook/hooktask.go b/models/webhook/hooktask.go
index 246484aea9..92d9e97383 100644
--- a/models/webhook/hooktask.go
+++ b/models/webhook/hooktask.go
@@ -233,14 +233,30 @@ func ReplayHookTask(ctx context.Context, hookID int64, uuid string) (*HookTask,
return newTask, db.Insert(ctx, newTask)
}
-// FindUndeliveredHookTasks represents find the undelivered hook tasks
-func FindUndeliveredHookTasks(ctx context.Context) ([]*HookTask, error) {
- tasks := make([]*HookTask, 0, 10)
+// FindUndeliveredHookTaskIDs will find the next 100 undelivered hook tasks with ID greater than the provided lowerID
+func FindUndeliveredHookTaskIDs(ctx context.Context, lowerID int64) ([]int64, error) {
+ const batchSize = 100
+
+ tasks := make([]int64, 0, batchSize)
return tasks, db.GetEngine(ctx).
+ Select("id").
+ Table(new(HookTask)).
Where("is_delivered=?", false).
+ And("id > ?", lowerID).
+ Asc("id").
+ Limit(batchSize).
Find(&tasks)
}
+func MarkTaskDelivered(ctx context.Context, task *HookTask) (bool, error) {
+ count, err := db.GetEngine(ctx).ID(task.ID).Where("is_delivered = ?", false).Cols("is_delivered").Update(&HookTask{
+ ID: task.ID,
+ IsDelivered: true,
+ })
+
+ return count != 0, err
+}
+
// CleanupHookTaskTable deletes rows from hook_task as needed.
func CleanupHookTaskTable(ctx context.Context, cleanupType HookTaskCleanupType, olderThan time.Duration, numberToKeep int) error {
log.Trace("Doing: CleanupHookTaskTable")