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

feat: make hook processing asynchronous #22

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions server/api/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
package api

import (
"context"
"errors"
"fmt"
"net/http"
"strconv"
"time"

"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -93,6 +95,8 @@ func BlockTilQueueHasRunningItem(c *gin.Context) {
c.Status(http.StatusNoContent)
}

const pipelineCreationTimeout = 2 * time.Minute

// PostHook
//
// @Summary Incoming webhook from forge
Expand Down Expand Up @@ -236,15 +240,17 @@ func PostHook(c *gin.Context) {
}

//
// 6. Finally create a pipeline
// 6. Finally create a pipeline. Do it asynchronously to avoid blocking the webhook
//
go func() {
ctx, cancel := context.WithTimeout(c.Request.Context(), pipelineCreationTimeout)
defer cancel()

pl, err := pipeline.Create(c, _store, repo, pipelineFromForge)
if err != nil {
handlePipelineErr(c, err)
} else {
c.JSON(http.StatusOK, pl)
}
_, err := pipeline.Create(ctx, _store, repo, pipelineFromForge)
log.Err(err).Msg("error creating pipeline")
}()

c.JSON(http.StatusAccepted, nil)
}

func getRepoFromToken(store store.Store, t *token.Token) (*model.Repo, error) {
Expand Down
27 changes: 24 additions & 3 deletions server/api/hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"sync"
"testing"
"time"

"github.com/franela/goblin"
"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -78,6 +80,11 @@ func TestHook(t *testing.T) {
},
}

// Since the hook calls a go routine to create the pipeline, we need to use a wait
// group to wait for the go routine to finish before we can assert the results.
var wg sync.WaitGroup
wg.Add(1)

_manager.On("ForgeFromRepo", repo).Return(_forge, nil)
_forge.On("Hook", mock.Anything, mock.Anything).Return(repo, pipeline, nil)
_store.On("GetRepo", repo.ID).Return(repo, nil)
Expand All @@ -93,12 +100,26 @@ func TestHook(t *testing.T) {
_manager.On("RegistryServiceFromRepo", repo).Return(_registryService)
_registryService.On("RegistryListPipeline", repo, mock.Anything).Return(nil, nil)
_manager.On("EnvironmentService").Return(nil)
_store.On("DeletePipeline", mock.Anything).Return(nil)

_store.On("DeletePipeline", mock.Anything).Run(func(_ mock.Arguments) {
wg.Done()
}).Return(nil)

api.PostHook(c)

assert.Equal(g, http.StatusNoContent, c.Writer.Status())
assert.Equal(g, "true", w.Header().Get("Pipeline-Filtered"))
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()

select {
case <-time.After(5 * time.Second):
g.Fail("timeout")
case <-done:
}

assert.Equal(g, http.StatusAccepted, c.Writer.Status())
})
})
}