Executing Multiple Instances of Code Snippet at Specific Intervals Using Constant Timestamp

25 Views Asked by At

I need to run multiple instances of the same code snippet at regular intervals. For example, I want to run four instances, where the first instance starts executing 5 seconds after a constant timestamp (e.g., 3PM), the second instance starts 10 seconds after, the third instance starts 15 seconds after, and the fourth instance starts 20 seconds after. Additionally, I want to repeat this pattern periodically, with the first instance starting again at 25 seconds after the constant timestamp and so on. How can I achieve this scheduling arrangement in my code?

I was trying using time.Ticker object in go, but was unable to come up with a solution to this, basically what I am looking for is, that I am passing in the ID to each instance while executing them, and then I want them to call a function after a particular multiple of seconds have passed since a t=0.

// We'll make a ticker that ticks every 5*nodeID seconds
proposalTicker := time.NewTicker(time.Duration(5*nodeID) * time.Second)
// Execute the proposal function every time the ticker ticks
go func() {
    for range proposalTicker.C {
        // Propose a new block 
        node.ProposeBlock() 
    }
}()
0

There are 0 best solutions below