Scheduler will not run a scheduled job - go

I am unable to get prprpus scheduler library with Go to work. I was trying to recreate the functionality of JavaScript's setinterval. The for loop should not block the scheduled job right?
package main
import (
"fmt"
"github.com/prprprus/scheduler"
)
func keepAlive2() {
fmt.Println("Keep alive 2")
}
func main() {
s, schedulerErr := scheduler.NewScheduler(1000)
if schedulerErr != nil {
panic(schedulerErr)
}
s.Every().Second(1).Do(keepAlive2)
for {
}
}

Busy-wait tight loops like
for {
}
will not allow other goroutines execute. It will not yield. If you need to wait, use one of the synchronization primitives, such as:
ch:=make(chan struct{})
<- ch

Related

Why not fini() functions for golang package?

There's built-in init() function for package initialization. Why not fini for destruction ? For example, I initial a goroutine pool inside my package and I want to all goroutines in the pool to finish their task before exiting instead of being forced to exit when the whole program exit.
If there's fini function. I can use sync.Wait there to fulfill my goal.
Another merit for built-in init is that it can and only can be called once,which will be my own concern if I use user-define functions as alternatives for them.
Try the following code:
package main
import (
"fmt"
"sync"
"time"
)
func main() {
defer fini()
wg.Add(1)
go routine()
fmt.Println("... in progress ... ")
}
func fini() {
wg.Wait()
fmt.Println("Done")
}
func init() {
fmt.Println("Hi")
}
func routine() {
fmt.Println("Doing somthing ...")
time.Sleep(1000 * time.Millisecond)
wg.Done()
}
var wg sync.WaitGroup
Run:
$ go run .
Hi
... in progress ...
Doing somthing ...
Done
Doing fini in function main can solves my problem

Quit channel on a worker pool implementation

What I eventually want to accomplish is to dynamically scale my workers up OR down, depending on the workload.
The code below successfully parses data when a Task is coming through w.Channel
func (s *Storage) StartWorker(w *app.Worker) {
go func() {
for {
w.Pool <- w.Channel // register current worker to the worker pool
select {
case task := <-w.Channel: // received a work request, do some work
time.Sleep(task.Delay)
fmt.Println(w.WorkerID, "processing task:", task.TaskName)
w.Results <- s.ProcessTask(w, &task)
case <-w.Quit:
fmt.Println("Closing channel for", w.WorkerID)
return
}
}
}()
}
The blocking point here is the line below.
w.Pool <- w.Channel
In that sense, if I try to stop a worker(s) in any part of my program with:
w.Quit <- true
the case <-w.Quit: is blocked and never receives until there's another incoming Task on w.Channel (and I guess select statement here is random for each case selection).
So how can I stop a channel(worker) independently?
See below sample code, it declares a fanout function that is reponsible to size up/down the workers.
It works by using timeouts to detect that new workers has ended or are required to spawn.
there is an inner loop to ensure that each item is processed before moving on, blocking the source when it is needed.
package main
import (
"fmt"
"io"
"log"
"net"
"os"
)
func main() {
input := make(chan string)
fanout(input)
}
func fanout() {
workers := 0
distribute := make(chan string)
workerEnd := make(chan bool)
for i := range input {
done := false
for done {
select {
case distribute<-i:
done = true
case <-workerEnd:
workers--
default:
if workers <10 {
workers++
go func(){
work(distribute)
workerEnd<-true
}()
}
}
}
}
}
func work(input chan string) {
for {
select {
case i := <-input:
<-time.After(time.Millisecond)
case <-time.After(time.Second):
return
}
}
}

How to run single instance of goroutine

I have a goroutine that will be run multiple times. But it can only run one at a time (single instance). What is the correct/idiomatic way to make sure a certain goroutine can run only one at a time?
Here is my contrived example code to illustrate the point:
func main() {
// Contrived example!!!!!!
// theCaller() may be run at multiple, unpredictable times
// theJob() must only be run one at a time
go theCaller()
go theCaller()
go theCaller()
}
func theCaller() {
if !jobIsRunning { // race condition here!
jobIsRunning = true
go theJob()
}
}
var jobIsRunning bool
// Can run multiple times, but only one at a time
func theJob() {
defer jobDone()
do_something()
}
func jobDone() {
jobIsRunning = false
}
Based on question and other comments from the OP, it looks like the goal is to start a new job if and only if a job is not already running.
Use a boolean variable protected by a sync.Mutex to record the running state of of the job. Set the variable to true when starting a job and to false when the job completes. Test this variable to determine if a job should be started.
var (
jobIsRunning bool
JobIsrunningMu sync.Mutex
)
func maybeStartJob() {
JobIsrunningMu.Lock()
start := !jobIsRunning
jobIsRunning = true
JobIsrunningMu.Unlock()
if start {
go func() {
theJob()
JobIsrunningMu.Lock()
jobIsRunning = false
JobIsrunningMu.Unlock()
}()
}
}
func main() {
maybeStartJob()
maybeStartJob()
maybeStartJob()
}
The lower-level sync/atomic package can also be used and may have better performance than using a mutex.
var jobIsRunning uint32
func maybeStartJob() {
if atomic.CompareAndSwapUint32(&jobIsRunning, 0, 1) {
go func() {
theJob()
atomic.StoreUint32(&jobIsRunning, 0)
}()
}
}
The sync/atomic package documentation warns that the functions in the package require great care to use correctly and that most applications should use the sync package.

How to test that a function was called in a goroutine?

I'd like to make sure that we're starting a goroutine by calling a function with the right arguments.
For example:
func MyTest(t *testing.T) {
service.EXPECT().MyMockFunc(1)
service.MyFunc()
}
func MyFunc() {
go MyMockFunc(1)
}
When I run this test, it fails because (I believe) MyMockFunc only gets called after the test has already finished running.
Is there a way to test that I started a goroutine by calling a function with the right arguments?
Note: Ideally, I'd like to keep the arguments I pass to MyMockFunc as is (not add a channel arg for instance).
Using a channel and assuming you can fire the goroutine from the test:
package main
import (
"fmt"
"testing"
"time"
)
func MyMockFunc(n int) int {
fmt.Println("MyMockFunc is called")
time.Sleep(5 * time.Second)
return n + 1
}
func TestMyMockFunc(t *testing.T) {
result := make(chan int)
go func() {
result <- MyMockFunc(1)
}()
if <-result != 2 {
t.Fatalf("Expecting 2")
}
}

Why is the following code sample stuck after some iterations?

I am trying to learn golang and i got a little piece of code that i do not understand why it gets stuck after some time.
package main
import "log"
func main() {
deliveryChann := make(chan bool, 10000)
go func() {
for {
deliveryChann <- true
log.Println("Sent")
}
}()
go func() {
for {
select {
case <-deliveryChann:
log.Println("received")
}
}
}()
go func() {
for {
select {
case <-deliveryChann:
log.Println("received")
}
}
}()
go func() {
for {
select {
case <-deliveryChann:
log.Println("received")
}
}
}()
for {
}
}
An basic start on how to investigate would suffice.
The main goroutine (running the for {} loop) is hogging the thread, and none of the other goroutines are able to execute because of it. If you change the end of your main function to:
for {
runtime.Gosched()
}
then the thread will be released and another goroutine made active.
func Gosched()
Gosched yields the processor, allowing other goroutines to run. It does not suspend the current goroutine, so execution resumes automatically.
-- https://golang.org/pkg/runtime/#Gosched
Order of execution of goroutings is undefined. Code gets stuck is legal. You can be more deterministic doing communication with main(). For example place
for {
deliveryChann <- true
log.Println("Sent")
}
in main() instead of go func()

Resources