~bigbes/core-go

468752564125e79b5efdfff091d8886dc23e373a — Drew DeVault 4 years ago d74ae98
webhooks/legacy: fetch subscriptions upfront

This fixes a race condition when delivering webhooks for resource
deletion events, in which the subscriptions would be removed from the
delete cascade before the task to fetch them executes. This requires the
downstream code to call Queue before committing the delete transaction,
and updates the API to include a context argument for the connection
pool.
1 files changed, 9 insertions(+), 9 deletions(-)

M webhooks/legacy.go
M webhooks/legacy.go => webhooks/legacy.go +9 -9
@@ 49,7 49,7 @@ func NewLegacyQueue() *LegacyQueue {
//
// Name shall be the prefix of the webhook tables, e.g. "user" for
// "user_webhook_{delivery,subscription}".
func (lq *LegacyQueue) Schedule(q sq.SelectBuilder,
func (lq *LegacyQueue) Schedule(ctx context.Context, q sq.SelectBuilder,
	name, event string, payload []byte) {
	// The following tasks are done during this process:
	//


@@ 59,16 59,16 @@ func (lq *LegacyQueue) Schedule(q sq.SelectBuilder,
	//
	// The first two steps are done in this task, then N tasks are created for
	// step 3 where N = number of subscriptions.
	task := work.NewTask(func(ctx context.Context) error {
		subs, err := fetchSubscriptions(ctx, q, event)
		if err != nil {
			return err
		}
	subs, err := fetchSubscriptions(ctx, q, event)
	if err != nil {
		panic(err)
	}

		if len(subs) == 0 {
			return nil
		}
	if len(subs) == 0 {
		return
	}

	task := work.NewTask(func(ctx context.Context) error {
		tasks := make([]*work.Task, len(subs))
		if err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
			var err error