1
0
Fork 0
mirror of https://github.com/postmannen/ctrl.git synced 2025-03-31 01:24:31 +00:00

removed HttpGetScheduled request method

This commit is contained in:
postmannen 2025-01-10 18:54:45 +01:00
parent 69a929d478
commit 6416dca082
3 changed files with 5 additions and 154 deletions

View file

@ -15,5 +15,3 @@ bool flags with default value set to "false" becomes "true" if false is set.
Remove these error logs:
`level=WARN msg="Thu Jan 9 12:14:24 2025, node: btdev1, error: readFolder: failed to open readFile from readFolder: open readfolder/msg2.yaml: no such file or directory\n"`
Remove httpGetScheduled

View file

@ -111,9 +111,6 @@ const (
ErrorLog Method = "errorLog"
// Http Get
HttpGet Method = "httpGet"
// Http Get Scheduled
// The second element of the MethodArgs slice holds the timer defined in seconds.
HttpGetScheduled Method = "httpGetScheduled"
// Tail file
TailFile Method = "tailFile"
// REQNone is used when there should be no reply.
@ -192,12 +189,11 @@ func (m Method) GetMethodsAvailable() MethodsAvailable {
// The hello publisher will not subscribe for messages, it will
// only start a procFunc, so we we don't need a handler with a method,
// so we set it to nil.
HelloPublisher: Handler(nil),
ErrorLog: Handler(methodErrorLog),
HttpGet: Handler(methodHttpGet),
HttpGetScheduled: Handler(methodHttpGetScheduled),
TailFile: Handler(methodTailFile),
PublicKey: Handler(methodPublicKey),
HelloPublisher: Handler(nil),
ErrorLog: Handler(methodErrorLog),
HttpGet: Handler(methodHttpGet),
TailFile: Handler(methodTailFile),
PublicKey: Handler(methodPublicKey),
KeysUpdateRequest: Handler(methodKeysUpdateRequest),
KeysUpdateReceive: Handler(methodKeysUpdateReceive),

View file

@ -1,11 +1,9 @@
package ctrl
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
"time"
)
@ -105,144 +103,3 @@ func methodHttpGet(proc process, message Message, node string) ([]byte, error) {
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
return ackMsg, nil
}
// ---
// handler to do a Http Get Scheduled.
// The second element of the MethodArgs slice holds the timer defined in seconds.
func methodHttpGetScheduled(proc process, message Message, node string) ([]byte, error) {
proc.errorKernel.logDebug("<--- REQHttpGetScheduled received", "fromNode", message.FromNode, "data", message.Data)
proc.processes.wg.Add(1)
go func() {
defer proc.processes.wg.Done()
// --- Check and prepare the methodArgs
switch {
case len(message.MethodArgs) < 3:
er := fmt.Errorf("error: methodHttpGet: got <3 number methodArgs. Want URL, Schedule Interval in seconds, and the total time in minutes the scheduler should run for")
proc.errorKernel.errSend(proc, message, er, logWarning)
return
}
url := message.MethodArgs[0]
scheduleInterval, err := strconv.Atoi(message.MethodArgs[1])
if err != nil {
er := fmt.Errorf("error: methodHttpGetScheduled: schedule interval value is not a valid int number defined as a string value seconds: %v, bailing out: %v", err, message.MethodArgs)
proc.errorKernel.errSend(proc, message, er, logWarning)
return
}
schedulerTotalTime, err := strconv.Atoi(message.MethodArgs[2])
if err != nil {
er := fmt.Errorf("error: methodHttpGetScheduled: scheduler total time value is not a valid int number defined as a string value minutes: %v, bailing out: %v", err, message.MethodArgs)
proc.errorKernel.errSend(proc, message, er, logWarning)
return
}
// --- Prepare and start the scheduler.
outCh := make(chan []byte)
ticker := time.NewTicker(time.Second * time.Duration(scheduleInterval))
// Prepare a context that will be for the schedule as a whole.
// Individual http get's will create their own context's
// derived from this one.
ctxScheduler, cancel := context.WithTimeout(proc.ctx, time.Minute*time.Duration(schedulerTotalTime))
go func() {
// Prepare the http request.
client := http.Client{
Timeout: time.Second * time.Duration(message.MethodTimeout),
}
for {
select {
case <-ticker.C:
proc.processes.wg.Add(1)
// Get a context with the timeout specified in message.MethodTimeout
// for the individual http requests.
ctx, cancel := getContextForMethodTimeout(proc.ctx, message)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
er := fmt.Errorf("error: methodHttpGet: NewRequest failed: %v, error: %v", err, message.MethodArgs)
proc.errorKernel.errSend(proc, message, er, logWarning)
cancel()
return
}
// Run each individual http get in it's own go routine, and
// deliver the result on the outCh.
go func() {
defer proc.processes.wg.Done()
resp, err := client.Do(req)
if err != nil {
er := fmt.Errorf("error: methodHttpGet: client.Do failed: %v, error: %v", err, message.MethodArgs)
proc.errorKernel.errSend(proc, message, er, logWarning)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
cancel()
er := fmt.Errorf("error: methodHttpGet: not 200, were %#v, error: %v", resp.StatusCode, message)
proc.errorKernel.errSend(proc, message, er, logWarning)
return
}
body, err := io.ReadAll(resp.Body)
if err != nil {
er := fmt.Errorf("error: methodHttpGet: io.ReadAll failed : %v, methodArgs: %v", err, message.MethodArgs)
proc.errorKernel.errSend(proc, message, er, logWarning)
}
out := body
select {
case outCh <- out:
case <-ctx.Done():
return
case <-ctxScheduler.Done():
// If the scheduler context is done then we also want to kill
// all running http request.
cancel()
return
}
}()
case <-ctxScheduler.Done():
cancel()
return
}
}
}()
for {
select {
case <-ctxScheduler.Done():
// fmt.Printf(" * DEBUG: <-ctxScheduler.Done()\n")
cancel()
er := fmt.Errorf("error: methodHttpGet: schedule context timed out: %v", message.MethodArgs)
proc.errorKernel.errSend(proc, message, er, logWarning)
return
case out := <-outCh:
// Prepare and queue for sending a new message with the output
// of the action executed.
newReplyMessage(proc, message, out)
}
}
}()
ackMsg := []byte("confirmed from: " + node + ": " + fmt.Sprint(message.ID))
return ackMsg, nil
}