mirror of
https://github.com/ncarlier/webhookd.git
synced 2025-04-07 17:07:09 +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)...)
|
params = append(params, tools.HTTPHeadersToShellVars(r.Header)...)
|
||||||
|
|
||||||
// Create work
|
// Create work
|
||||||
work := new(worker.WorkRequest)
|
timeout := atoiFallback(r.Header.Get("X-Hook-Timeout"), defaultTimeout)
|
||||||
work.Name = p
|
work := worker.NewWorkRequest(p, script, string(body), params, timeout)
|
||||||
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)
|
|
||||||
|
|
||||||
// Put work in queue
|
// Put work in queue
|
||||||
worker.WorkQueue <- *work
|
worker.WorkQueue <- *work
|
||||||
|
@ -81,7 +76,7 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
|
|
||||||
logger.Debug.Println("Work request queued:", script)
|
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 {
|
for {
|
||||||
msg, open := <-work.MessageChan
|
msg, open := <-work.MessageChan
|
||||||
|
|
|
@ -68,6 +68,10 @@ func runScript(work *WorkRequest) (string, error) {
|
||||||
go func(reader io.Reader) {
|
go func(reader io.Reader) {
|
||||||
scanner := bufio.NewScanner(reader)
|
scanner := bufio.NewScanner(reader)
|
||||||
for scanner.Scan() {
|
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
|
// writing to the work channel
|
||||||
line := scanner.Text()
|
line := scanner.Text()
|
||||||
work.MessageChan <- []byte(line)
|
work.MessageChan <- []byte(line)
|
||||||
|
@ -95,6 +99,6 @@ func runScript(work *WorkRequest) (string, error) {
|
||||||
return logFilename, err
|
return logFilename, err
|
||||||
}
|
}
|
||||||
timer.Stop()
|
timer.Stop()
|
||||||
logger.Info.Println("Script", work.Script, "executed wit SUCCESS")
|
logger.Info.Println("Script", work.Script, "executed with SUCCESS")
|
||||||
return logFilename, nil
|
return logFilename, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,4 +8,18 @@ type WorkRequest struct {
|
||||||
Args []string
|
Args []string
|
||||||
MessageChan chan []byte
|
MessageChan chan []byte
|
||||||
Timeout int
|
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")
|
work.MessageChan <- []byte("done")
|
||||||
notify(subject, "See attachment.", filename)
|
notify(subject, "See attachment.", filename)
|
||||||
}
|
}
|
||||||
|
work.Closed = true
|
||||||
close(work.MessageChan)
|
close(work.MessageChan)
|
||||||
case <-w.QuitChan:
|
case <-w.QuitChan:
|
||||||
logger.Debug.Printf("Stopping worker%d...\n", w.ID)
|
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) {
|
func notify(subject string, text string, outfilename string) {
|
||||||
var notifier, err = notification.NotifierFactory()
|
var notifier, err = notification.NotifierFactory()
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
if notifier == nil {
|
if notifier == nil {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user