mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-06 09:51:21 +00:00
fix(): fix panic due to writing into closed chan
This commit is contained in:
parent
82346b08da
commit
43820cd9f0
|
@ -64,13 +64,8 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
params = append(params, tools.HTTPHeadersToShellVars(r.Header)...)
|
||||
|
||||
// Create work
|
||||
work := new(worker.WorkRequest)
|
||||
work.Name = p
|
||||
work.Script = script
|
||||
work.Payload = string(body)
|
||||
work.Args = params
|
||||
work.MessageChan = make(chan []byte)
|
||||
work.Timeout = atoiFallback(r.Header.Get("X-Hook-Timeout"), defaultTimeout)
|
||||
timeout := atoiFallback(r.Header.Get("X-Hook-Timeout"), defaultTimeout)
|
||||
work := worker.NewWorkRequest(p, script, string(body), params, timeout)
|
||||
|
||||
// Put work in queue
|
||||
worker.WorkQueue <- *work
|
||||
|
@ -81,7 +76,7 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
|
|||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
|
||||
logger.Debug.Println("Work request queued:", script)
|
||||
fmt.Fprintf(w, "data: Hook work request \"%s\" queued...\n\n", work.Name)
|
||||
// fmt.Fprintf(w, "data: Running \"%s\" ...\n\n", work.Name)
|
||||
|
||||
for {
|
||||
msg, open := <-work.MessageChan
|
||||
|
|
|
@ -68,6 +68,10 @@ func runScript(work *WorkRequest) (string, error) {
|
|||
go func(reader io.Reader) {
|
||||
scanner := bufio.NewScanner(reader)
|
||||
for scanner.Scan() {
|
||||
if work.Closed {
|
||||
logger.Error.Println("Unable to write into the work channel. Work request closed.")
|
||||
return
|
||||
}
|
||||
// writing to the work channel
|
||||
line := scanner.Text()
|
||||
work.MessageChan <- []byte(line)
|
||||
|
@ -95,6 +99,6 @@ func runScript(work *WorkRequest) (string, error) {
|
|||
return logFilename, err
|
||||
}
|
||||
timer.Stop()
|
||||
logger.Info.Println("Script", work.Script, "executed wit SUCCESS")
|
||||
logger.Info.Println("Script", work.Script, "executed with SUCCESS")
|
||||
return logFilename, nil
|
||||
}
|
||||
|
|
|
@ -8,4 +8,18 @@ type WorkRequest struct {
|
|||
Args []string
|
||||
MessageChan chan []byte
|
||||
Timeout int
|
||||
Closed bool
|
||||
}
|
||||
|
||||
// NewWorkRequest creats new work request
|
||||
func NewWorkRequest(name, script, payload string, args []string, timeout int) *WorkRequest {
|
||||
return &WorkRequest{
|
||||
Name: name,
|
||||
Script: script,
|
||||
Payload: payload,
|
||||
Args: args,
|
||||
Timeout: timeout,
|
||||
MessageChan: make(chan []byte),
|
||||
Closed: false,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -52,6 +52,7 @@ func (w Worker) Start() {
|
|||
work.MessageChan <- []byte("done")
|
||||
notify(subject, "See attachment.", filename)
|
||||
}
|
||||
work.Closed = true
|
||||
close(work.MessageChan)
|
||||
case <-w.QuitChan:
|
||||
logger.Debug.Printf("Stopping worker%d...\n", w.ID)
|
||||
|
@ -72,7 +73,7 @@ func (w Worker) Stop() {
|
|||
func notify(subject string, text string, outfilename string) {
|
||||
var notifier, err = notification.NotifierFactory()
|
||||
if err != nil {
|
||||
logger.Info.Println("Unable to get the notifier. Notification skipped:", err)
|
||||
logger.Debug.Println("Unable to get the notifier. Notification skipped:", err)
|
||||
return
|
||||
}
|
||||
if notifier == nil {
|
||||
|
|
Loading…
Reference in New Issue
Block a user