func InvokeLoop(fs *firestore.Client, registerChan <-chan string, wg *sync.WaitGroup) {
	timeoutChan := make(chan string)

	for {
		next, id, webhook, err := GetNextTimeout(fs)
		if err != nil {
			log.Println(err.Error())
			continue
		}

		// Has the timeout already expired?
		if next.Before(time.Now()) {
			changed, field, err := webhook.Invoke(fs, id, false)
			if err != nil {
				log.Println(err.Error())
			}
			if changed {
				err = InvokeAllWithField(fs, field, id)
				if err != nil {
					log.Println(err.Error())
				}
			}
			continue
		}

		// Sleep until next timeout
		go func() {
			time.Sleep(time.Until(next))
			timeoutChan <- "timeout"
		}()

		// Either we sleep until the next timeout expires,
		// or we run all the logic again if a new webhook gets registered
		select {
		case <-timeoutChan:
			log.Println("Timeout reached, invoking webhook:", id)
		case <-registerChan:
			log.Println("New webhook registered")
			continue
		}

		// Now invoke the webhook, since it has timed out by now
		changed, field, err := webhook.Invoke(fs, id, false)
		if err != nil {
			log.Println(err.Error())
		}
		if changed {
			err = InvokeAllWithField(fs, field, id)
			if err != nil {
				log.Println(err.Error())
			}
		}
	}

	wg.Done()
}
// Loop over webhooks and invoke them if the should be invoked.
Loop over webhooks and invoke them if the should be invoked.
// Loop over webhooks and invoke them if the should be invoked .
func InvokeLoop(fs *firestore.Client, registerChan <-chan string, wg *sync.WaitGroup) {
	timeoutChan := make(chan string)
	for {
		next, id, webhook, err := GetNextTimeout(fs)
		if err != nil {
			log.Println(err.Error())
			continue
		}
		if next.Before(time.Now()) {
			changed, field, err := webhook.Invoke(fs, id, false)
			if err != nil {
				log.Println(err.Error())
			}
			if changed {
				err = InvokeAllWithField(fs, field, id)
				if err != nil {
					log.Println(err.Error())
				}
			}
			continue
		}
		go func() {
			time.Sleep(time.Until(next))
			timeoutChan <- "timeout"
		}()
		select {
		case <-timeoutChan:
			log.Println("Timeout reached, invoking webhook:", id)
		case <-registerChan:
			log.Println("New webhook registered")
			continue
		}
		changed, field, err := webhook.Invoke(fs, id, false)
		if err != nil {
			log.Println(err.Error())
		}
		if changed {
			err = InvokeAllWithField(fs, field, id)
			if err != nil {
				log.Println(err.Error())
			}
		}
	}
	wg.Done()
}
func InvokeLoop ( fs * firestore . Client , registerChan <- chan string , wg * sync . WaitGroup ) { timeoutChan := make ( chan string ) for { next , id , webhook , err := GetNextTimeout ( fs ) if err != nil { log . Println ( err . Error ( ) ) continue } if next . Before ( time . Now ( ) ) { changed , field , err := webhook . Invoke ( fs , id , false ) if err != nil { log . Println ( err . Error ( ) ) } if changed { err = InvokeAllWithField ( fs , field , id ) if err != nil { log . Println ( err . Error ( ) ) } } continue } go func ( ) { time . Sleep ( time . Until ( next ) ) timeoutChan <- " " } ( ) select { case <- timeoutChan : log . Println ( " " , id ) case <- registerChan : log . Println ( " " ) continue } changed , field , err := webhook . Invoke ( fs , id , false ) if err != nil { log . Println ( err . Error ( ) ) } if changed { err = InvokeAllWithField ( fs , field , id ) if err != nil { log . Println ( err . Error ( ) ) } } } wg . Done ( ) }
Loop over webhooks and invoke them if the should be invoked.
Loop over webhooks and invoke them if the should be invoked .
// Has the timeout already expired? // Sleep until next timeout // Either we sleep until the next timeout expires, // or we run all the logic again if a new webhook gets registered // Now invoke the webhook, since it has timed out by now