亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

強制 Gitlab 在 Go 失敗時重試 webhook

強制 Gitlab 在 Go 失敗時重試 webhook

Go
米琪卡哇伊 2022-06-01 09:56:02
我想監視 Gitlab 項目中的每個事件并將它們存儲在外部服務中。為此,我使用 Gitlab Webhooks。我在 Go 中創建了一個小型本地 HTTP 服務器,它監聽 Gitlab 的 POST 并將它們轉發到外部服務。Hooks 包含我需要的所有信息,所以看起來這個架構很好:Gitlab > HTTPServer > External Service.我的問題是當外部服務關閉時,我無法讓 Gitlab 重試失敗的請求。正如文檔所說:GitLab 忽略端點返回的 HTTP 狀態代碼。您的端點應始終返回有效的 HTTP 響應。如果你不這樣做,GitLab 會認為鉤子失敗并重試。令人驚訝的是,Gitlab 沒有正確的方法來請求 webhook 重試。我必須明確返回無效的 http 響應。此外,我找不到一個 API 端點來列出所有失敗的 webhook 并要求重新發送。問題:如何使用標準的“net/http”庫顯式返回無效的 HTTP 響應,以強制 Gitlab 重試 Webhooks?
查看完整描述

1 回答

?
ABOUTYOU

TA貢獻1812條經驗 獲得超5個贊

正如評論中所寫,webhook 只是一個事件發生的通知,并且可能會發送一些數據,通常作為 JSON 數據。


您有責任保留事件本身以及您想要/需要處理的隨它發送的數據。您將在下面找到一個注釋示例。請注意,這不包括增量退避,但這應該很容易添加:


package main


import (

    "encoding/json"

    "flag"

    "io"

    "log"

    "net/http"

    "os"

    "path/filepath"


    "github.com/joncrlsn/dque"

)


var (

    bind        string

    queueDir    string

    segmentSize int

)


// You might want to add request headers and stuff

type webhookContent struct {

    Foo string

    Bar int

}


func init() {

    flag.StringVar(&bind, "bind", ":8080", "The address to bind to")

    flag.StringVar(&queueDir, "path", "./queue", "path to store the queue in")

    flag.IntVar(&segmentSize, "size", 50, "number of entries for the queue")

}


// The "webserver" component

func runserver(q *dque.DQue) {


    http.HandleFunc("/webhook", func(w http.ResponseWriter, r *http.Request) {

        // A new decoder for each call, as we want to have a new LimitReader

        // for each call. This is a simple, albeit a bit crude method to prevent

        // accidental or malicious overload of your server.

        dec := json.NewDecoder(io.LimitReader(r.Body, 4096))


        defer r.Body.Close()


        c := &webhookContent{}

        if err := dec.Decode(c); err != nil {

            log.Printf("reading body: %s", err)

            http.Error(w, "internal error", http.StatusInternalServerError)

            return

        }


        // When the content is successfully decoded, we can persist it into

        // our queue.

        if err := q.Enqueue(c); err != nil {

            log.Printf("enqueueing webhook data: %s", err)

            // PROPER ERROR HANDLING IS MISSING HERE

        }

    })


    http.ListenAndServe(bind, nil)

}


func main() {

    flag.Parse()


    var (

        q   *dque.DQue

        err error

    )


    if !dirExists(queueDir) {

        if err = os.MkdirAll(queueDir, 0750); err != nil {

            log.Fatalf("creating queue dir: %s", err)

        }

    }


    if !dirExists(filepath.Join(queueDir, "webhooks")) {

        q, err = dque.New("webhooks", queueDir, segmentSize, func() interface{} { return &webhookContent{} })

    } else {

        q, err = dque.Open("webhooks", queueDir, segmentSize, func() interface{} { return &webhookContent{} })

    }


    if err != nil {

        log.Fatalf("setting up queue: %s", err)

    }


    defer q.Close()


    go runserver(q)


    var (

        // Placeholder during event loop

        i interface{}

        // Payload

        w *webhookContent

        // Did the type assertion succeed

        ok bool

    )


    for {

        // We peek only. The semantic of this is that

        // you can already access the next item in the queue

        // without removing it from the queue and "mark" it as read.

        // We use PeekBlock since we want to wait for an item in the

        // queue to be available.

        if i, err = q.PeekBlock(); err != nil {

            // If we can not peek, something is SERIOUSLY wrong.

            log.Fatalf("reading from queue: %s", err)

        }


        if w, ok = i.(*webhookContent); !ok {

            // If the type assertion fails, something is seriously wrong, too.

            log.Fatalf("reading from queue: %s", err)

        }


        if err = doSomethingUseful(w); err != nil {

            log.Printf("Something went wrong: %s", err)

            log.Println("I strongly suggest entering an incremental backoff!")

            continue

        }


        // We did something useful, so we can dequeue the item we just processed from the queue.

        q.Dequeue()

    }


}


func doSomethingUseful(w *webhookContent) error {

    log.Printf("Instead of this log message, you can do something useful with: %#v", w)

    return nil

}


func dirExists(path string) bool {

    fileInfo, err := os.Stat(path)

    if err == nil {

        return fileInfo.IsDir()

    }

    return false

}

現在,當您執行以下操作時:


$ curl -X POST --data '{"Foo":"Baz","Bar":42}' http://localhost:8080/webhook

你應該得到一個日志條目


2020/04/18 11:34:23 Instead of this log message, you can do something useful with: &main.webhookContent{Foo:"Baz", Bar:42}



查看完整回答
反對 回復 2022-06-01
  • 1 回答
  • 0 關注
  • 205 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號