requestscheduler

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 20, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Executable = func(args ...interface{}) {

	if len(args) > 3 {
		fmt.Println("Executable expects 3 arguments")
	}
	var msg Message
	for i, arg := range args {
		switch i {
		case 0:
			action, ok := arg.(string)
			if !ok {
				fmt.Println("action is not string")
			}
			msg.Action = action
		case 1:
			url, ok := arg.(string)
			if !ok {
				fmt.Println("url is not string")
			}
			msg.Url = url
		case 2:
			payload, ok := arg.(map[string]interface{})
			if !ok {
				fmt.Println("payload is not string")
			}
			msg.Payload = payload
		default:
			fmt.Println("Executable expects 3 arguments")
		}
	}
	switch strings.ToLower(msg.Action) {
	case "post":
		payload, err1 := json.Marshal(msg.Payload)
		if err1 != nil {
			fmt.Printf("Cannot process sent payload %s for url %s. %v\n", payload, msg.Url, err1)
		}
		requestBody := bytes.NewBuffer(payload)
		_, err2 := http.Post(msg.Url, "application/json", requestBody)
		if err2 != nil {
			fmt.Printf("An error occured POSTing to %s. %v\n", msg.Url, err2)
		}
	case "get":
		_, err := http.Get(msg.Url)
		if err != nil {
			fmt.Printf("An error occured GETing from %s. %v\n", msg.Url, err)
		}
	default:
		fmt.Printf("Unknown action %s\n", msg.Action)
	}
}

Functions

func HandleIncomingRequests

func HandleIncomingRequests(w http.ResponseWriter, r *http.Request)

Types

type EnqueuedMessage

type EnqueuedMessage struct {
	// contains filtered or unexported fields
}

EnqueuedMessage contains function executed at given time by the TimerQueue.

type Message

type Message struct {
	Action  string                 `json:"action"`
	Url     string                 `json:"url"`
	Payload map[string]interface{} `json:"payload"`
	Delay   int                    `json:"delay"`
}

type PriorityQueue

type PriorityQueue []*EnqueuedMessage

func (PriorityQueue) Len

func (pq PriorityQueue) Len() int

func (PriorityQueue) Less

func (pq PriorityQueue) Less(i, j int) bool

func (*PriorityQueue) Pop

func (pq *PriorityQueue) Pop() interface{}

func (*PriorityQueue) Push

func (pq *PriorityQueue) Push(m interface{})

func (PriorityQueue) Swap

func (pq PriorityQueue) Swap(i, j int)

type ScheduledFunction

type ScheduledFunction func(args ...interface{})

ScheduledFunction is the function invoked by EnqueuedMessage struct when time comes

type ScheduledFunctionsQueue

type ScheduledFunctionsQueue struct {
	// contains filtered or unexported fields
}

ScheduledFunctionsQueue is priority queue of scheduled functions

func InitQueue

func InitQueue() *ScheduledFunctionsQueue

func NewScheduledFunctionsQueue

func NewScheduledFunctionsQueue(ctx context.Context) *ScheduledFunctionsQueue

NewScheduledFunctionsQueue creates a new instance of ScheduledFunctionsQueue

func (*ScheduledFunctionsQueue) Drain

func (q *ScheduledFunctionsQueue) Drain()

func (*ScheduledFunctionsQueue) Push

func (q *ScheduledFunctionsQueue) Push(executionTime time.Time, fn ScheduledFunction, args ...interface{}) bool

Push adds to-be-executed function to the queue. Returns true or false (if context is cancelled)

func (*ScheduledFunctionsQueue) StartProcessingScheduledFunctions

func (q *ScheduledFunctionsQueue) StartProcessingScheduledFunctions()

StartProcessingScheduledFunctions start goroutine within the given context that processes enqueued messages. New goroutine executes for each message.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL