Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prune hook_task table #11416

Closed
wants to merge 49 commits into from
Closed

Conversation

bhalbright
Copy link
Contributor

Ref issue: #10741

At a high level, added code to do regular pruning of delivered webhooks in the hook_task table. It can be turned on/off and the schedule controlled globally via app.ini. Also can be controlled per repository via UI. By default webhooks will be on and delete all but the most recent 10 deliveres.

  • Added a db migration to add 2 fields to repository table: is_hook_task_purge_enabled and number_webhook_deliveries_to_keep. Defaults to true and 10.
  • Added fields to repository settings page so that the behavior can be controlled per repository:
    image
  • Added a cron job to perform the deletions on the hook_task table. Very similar to the setup of other cron jobs, it can be turned off and the schedule controlled via app.ini. Defaults to enabled, not run on start, and every 24hours.

@techknowlogick techknowlogick added this to the 1.13.0 milestone May 16, 2020
@bhalbright bhalbright changed the title [WIP] Prune hook_task table Prune hook_task table May 17, 2020
@bhalbright bhalbright marked this pull request as ready for review May 17, 2020 00:05
@bhalbright bhalbright changed the title Prune hook_task table WIP Prune hook_task table May 17, 2020
@bhalbright bhalbright changed the title WIP Prune hook_task table Prune hook_task table May 17, 2020
@bhalbright
Copy link
Contributor Author

This is ready to review

@lunny
Copy link
Member

lunny commented Sep 2, 2020

Please resolve the conflicts.

1 similar comment
@lunny
Copy link
Member

lunny commented Sep 2, 2020

Please resolve the conflicts.

@zeripath
Copy link
Contributor

zeripath commented Sep 2, 2020

I've resolved the conflicts.

@bhalbright
Copy link
Contributor Author

I've resolved the conflicts.

Thanks @zeripath

@codecov-commenter
Copy link

codecov-commenter commented Sep 2, 2020

Codecov Report

Merging #11416 into master will decrease coverage by 0.00%.
The diff coverage is 32.87%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #11416      +/-   ##
==========================================
- Coverage   43.08%   43.07%   -0.01%     
==========================================
  Files         658      660       +2     
  Lines       72448    72519      +71     
==========================================
+ Hits        31211    31237      +26     
- Misses      36183    36220      +37     
- Partials     5054     5062       +8     
Impacted Files Coverage Δ
models/migrations/migrations.go 2.46% <ø> (ø)
models/migrations/v153.go 0.00% <0.00%> (ø)
models/repo.go 55.61% <ø> (+0.22%) ⬆️
modules/auth/repo_form.go 42.34% <ø> (ø)
modules/repository/prune_hook_task.go 0.00% <0.00%> (ø)
modules/setting/repository.go 56.36% <ø> (ø)
routers/repo/setting.go 14.86% <0.00%> (-0.10%) ⬇️
services/repository/push.go 57.92% <0.00%> (-0.65%) ⬇️
modules/cron/tasks_basic.go 85.41% <66.66%> (-1.94%) ⬇️
models/webhook.go 68.78% <80.00%> (+0.62%) ⬆️
... and 14 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e7ffc67...cffae2e. Read the comment docs.

@zeripath
Copy link
Contributor

zeripath commented Sep 2, 2020

no problem @bhalbright - sorry this has taken so long to merge.

repo := bean.(*models.Repository)
repoPath := repo.RepoPath()
log.Trace("Running prune hook_task table on repository %s", repoPath)
if err := models.DeleteDeliveredHookTasks(repo.ID, repo.NumberWebhookDeliveriesToKeep); err != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may fail on sqlite. I think use a Find but not Iterate is better.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I totally understand, can you elaborate? I saw Iterate usage other places like for example (if I'm understanding):
https://github.com/go-gitea/gitea/blob/master/modules/repository/check.go#L25
https://github.com/go-gitea/gitea/blob/master/modules/repository/hooks.go#L159

But I'm happy to refactor it if it is a problem.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When did some database operations on an iterate maybe affect a lock on sqlite. If other places also have the problems, they also needs to be changed.

@@ -245,3 +245,39 @@ func TestUpdateHookTask(t *testing.T) {
assert.NoError(t, UpdateHookTask(hook))
AssertExistsAndLoadBean(t, hook)
}

func TestDeleteDeliveredHookTasks_DeletesDelivered(t *testing.T) {
assert.NoError(t, PrepareTestDatabase())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you insert 2 and limit to 1
and check if only the right one has been deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I added a test where I created a new webhook that was delievered "now" and then called the delete method where 1 is kept...and then verified that the newly created webhook is still present.

@@ -39,6 +39,8 @@ func CreateRepository(doer, u *models.User, opts models.CreateRepoOptions) (_ *m
IsPrivate: opts.IsPrivate,
IsFsckEnabled: !opts.IsMirror,
CloseIssuesViaCommitInAnyBranch: setting.Repository.DefaultCloseIssuesViaCommitsInAnyBranch,
IsHookTaskPurgeEnabled: setting.Repository.DefaultIsHookTaskPurgeEnabled,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we real have to set it on repo creation?
instead of use global default until user change it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I was implementing the way "CloseIssuesViaCommitInAnyBranch" is done...but I think you are suggesting that the columns added to repository would be null by default, and would only have values if the user overrides the settings?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

…-hook-task

# Conflicts:
#	models/migrations/migrations.go
#	models/migrations/v150.go
… will leave the most recent based if 1 delivery is to be kept
@6543
Copy link
Member

6543 commented Sep 15, 2020

@bhalbright pleace resolve conflicts :)

…-hook-task

# Conflicts:
#	models/migrations/migrations.go
#	models/migrations/v151.go
@bhalbright
Copy link
Contributor Author

@bhalbright pleace resolve conflicts :)

sure, done. I started working on the changes you suggested (the columns added to repository would be null by default, and would only have values if the user overrides the settings), but I don't know that I'll have enough free time to finish it this week.

@6543
Copy link
Member

6543 commented Sep 16, 2020

@bhalbright thanks!!!

…-hook-task

# Conflicts:
#	models/migrations/migrations.go
#	models/migrations/v152.go
#	modules/repository/generate.go
@6543
Copy link
Member

6543 commented Sep 23, 2020

@bhalbright I think bhalbright#1 should it be

@bhalbright
Copy link
Contributor Author

@6543 my apologies, I'm going to close this PR. I realized after chatting with @jag3773 that I misinterpreted the original ask, I should be deleting per webhook, not per repo. Also, I think I made this too complicated, the ability to override the number to keep per repo was likely overkill. I'm going to re-submit a PR that will allow you to set the values via the ini file and you will be able to cleanup via a simple age of the record or by delete all but a certain # per webhook. Also I think I'll put in an option to delete orphaned records in hook_task.

I'm sorry for the time everyone spent reviewing.

@bhalbright bhalbright closed this Sep 24, 2020
@6543 6543 removed this from the 1.13.0 milestone Sep 24, 2020
@go-gitea go-gitea locked and limited conversation to collaborators Nov 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lgtm/need 1 This PR needs approval from one additional maintainer to be merged. type/enhancement An improvement of existing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants