From 1d861356d8a724b5faf1c231e9345b4d1502724e Mon Sep 17 00:00:00 2001 From: Drew DeVault Date: Wed, 20 Nov 2019 11:55:00 -0500 Subject: [PATCH] update-hook: improve stage-3 --- gitsrht-update-hook/post-update.go | 29 +++++++++++++++++++++++------ gitsrht-update-hook/stage-3.go | 2 +- gitsrht-update-hook/webhooks.go | 6 +++++- 3 files changed, 29 insertions(+), 8 deletions(-) diff --git a/gitsrht-update-hook/post-update.go b/gitsrht-update-hook/post-update.go index 10fabae..91a66d1 100644 --- a/gitsrht-update-hook/post-update.go +++ b/gitsrht-update-hook/post-update.go @@ -6,8 +6,8 @@ import ( "fmt" "log" "os" - "os/exec" "strings" + "syscall" goredis "github.com/go-redis/redis" _ "github.com/lib/pq" @@ -272,10 +272,27 @@ func postUpdate() { // Run stage 3 asyncronously - the last few tasks can be done without // blocking the pusher's terminal. - stage3 := exec.Command(hook, string(deliveriesJson), string(payloadBytes)) - stage3.Args[0] = "stage-3" + wd, err := os.Getwd() + if err != nil { + log.Fatalf("Failed to execute stage 3: %v", err) + } + + procAttr := syscall.ProcAttr{ + Dir: wd, + Files: []uintptr{os.Stdin.Fd(), os.Stdout.Fd(), os.Stderr.Fd()}, + Env: os.Environ(), + Sys: &syscall.SysProcAttr{ + Foreground: false, + }, + } + pid, err := syscall.ForkExec(hook, []string{ + "hooks/stage-3", string(deliveriesJson), string(payloadBytes), + }, &procAttr) + if err != nil { + log.Fatalf("Failed to execute stage 3: %v", err) + } + logger.Printf("Executing stage 3 to record %d sync deliveries and make " + - "%d async deliveries", len(deliveries), len(dbinfo.AsyncWebhooks)) - stage3.Start() - stage3.Process.Release() + "%d async deliveries (pid %d)", len(deliveries), + len(dbinfo.AsyncWebhooks), pid) } diff --git a/gitsrht-update-hook/stage-3.go b/gitsrht-update-hook/stage-3.go index 34ef916..f453236 100644 --- a/gitsrht-update-hook/stage-3.go +++ b/gitsrht-update-hook/stage-3.go @@ -73,7 +73,7 @@ func stage3() { subscription_id ) VALUES ( $1, NOW() AT TIME ZONE 'UTC', 'repo:post-update', - $2, $3, $4, $5, $6, $7 + $2, $3, $4, $5, $6, $7, $8 ); `, delivery.UUID, delivery.Url, delivery.Payload, delivery.Headers, diff --git a/gitsrht-update-hook/webhooks.go b/gitsrht-update-hook/webhooks.go index 2f5ddec..faba0de 100644 --- a/gitsrht-update-hook/webhooks.go +++ b/gitsrht-update-hook/webhooks.go @@ -145,7 +145,11 @@ func deliverWebhooks(subs []WebhookSubscription, delivery.ResponseStatus = resp.StatusCode delivery.ResponseHeaders = responseHeaders.String() - delivery.Response = string(respBody)[:65535] + if len(respBody) > 65535 { + delivery.Response = string(respBody)[:65535] + } else { + delivery.Response = string(respBody) + } deliveries = append(deliveries, delivery) } -- 2.38.4