Skip to content

Commit

Permalink
fix(scripting): use bg context for task execution
Browse files Browse the repository at this point in the history
  • Loading branch information
ncarlier committed May 31, 2023
1 parent 5dcd1ff commit 1e80f59
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
20 changes: 20 additions & 0 deletions pkg/helper/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package helper

import (
"context"
"time"

"github.com/ncarlier/readflow/pkg/constant"
)

// NewBackgroundContextWithValues create new context with same values than another context
func NewBackgroundContextWithValues(src context.Context, timeout time.Duration) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
ctx = context.WithValue(ctx, constant.ContextDownloader, src.Value(constant.ContextDownloader))
ctx = context.WithValue(ctx, constant.ContextIncomingWebhook, src.Value(constant.ContextIncomingWebhook))
ctx = context.WithValue(ctx, constant.ContextIsAdmin, src.Value(constant.ContextIsAdmin))
ctx = context.WithValue(ctx, constant.ContextRequestID, src.Value(constant.ContextRequestID))
ctx = context.WithValue(ctx, constant.ContextUser, src.Value(constant.ContextUser))
ctx = context.WithValue(ctx, constant.ContextUserID, src.Value(constant.ContextUserID))
return ctx, cancel
}
7 changes: 5 additions & 2 deletions pkg/service/articles_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/ncarlier/readflow/pkg/constant"
"github.com/ncarlier/readflow/pkg/event"
"github.com/ncarlier/readflow/pkg/helper"
"github.com/ncarlier/readflow/pkg/model"
"github.com/ncarlier/readflow/pkg/scripting"

Expand Down Expand Up @@ -102,8 +103,10 @@ func (reg *Registry) CreateArticle(ctx context.Context, form model.ArticleCreate
logger.Info().Uint("id", article.ID).Msg("article created")
// exec asynchronously other operations
go func() {
if err := reg.execOtherOperations(ctx, ops, article); err != nil {
logger.Info().Err(err).Msg(unableToCreateArticleErrorMsg)
bgCtx, cancel := helper.NewBackgroundContextWithValues(ctx, constant.DefaultTimeout)
defer cancel()
if err := reg.execOtherOperations(bgCtx, ops, article); err != nil {
logger.Info().Err(err).Msg("error while applying script operations")
}
}()
// emit article creation event
Expand Down

0 comments on commit 1e80f59

Please sign in to comment.