Running code every x seconds using map from database

Liam Murnux :

Using golang's time.NewTicker, I am trying to run each record from a database (consisting of text and a timer, i.e the number of seconds before it repeats) but it is only repeating the first record it grabs

func LoadTimedCommands() map[string]time.Duration {
    database := InitializeDB()

    rows, _ := database.Query("SELECT TimedResponse, Timer from timedcommands")

    com := make(map[string]time.Duration)
    for rows.Next() {
        var TimedResponse string
        var Timer time.Duration
        rows.Scan(&TimedResponse, &Timer)
        com[TimedResponse] = Timer
    }
    return com
}

func TimedCommands(conn net.Conn, channel string, name string) {
    timedcoms := LoadTimedCommands()
    for k, v := range timedcoms {
        for range time.NewTicker(v * time.Second).C {
            BotSendMsg(conn, channel, k, name)
        }
    }
}

So if I have two records to use from: Sqlite data

Then the code should run the first record every 15 seconds, and the second every 10. But again, only the first one that is loaded into the map is run.

Zak :

Your problem is in this function:

func TimedCommands(conn net.Conn, channel string, name string) {
    timedcoms := LoadTimedCommands()
    for k, v := range timedcoms {
        for range time.NewTicker(v * time.Second).C {
            BotSendMsg(conn, channel, k, name)
        }
    }
}

In this section, where you range over the channel time.NewTicker(...).C:

for range time.NewTicker(v * time.Second).C {
    BotSendMsg(conn, channel, k, name)
}

This will try and read values from this one ticker until the channel C is closed. This means that the outer loop only does one iteration, then we block on the inner loop until C is closed. And we never create more than one Ticker at once.

You could get around this by creating tickers in their own goroutines:

for k, v := range timedcoms {
    go func(conn net.Conn, channel, name, k string, v time.Duration) {
        for range time.NewTicker(v * time.Second).C {
            BotSendMsg(conn, channel, k, name)
        }
    }(conn, channel, k, v)
}

But you will probably want to include some synchronisation to ensure that you can stop the goroutines that you start, or to ensure that BotSendMsg(...) can be called from multiple goroutines at the same time etc.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java Thread every X seconds

Update JLabel every X seconds from ArrayList<List> - Java

Displaying a different values every X seconds using React hooks

Running a cron every 30 seconds

updating state every x seconds

Launch a promise every X seconds

Get data from Observable every x Seconds, with Start and Stop buttons

How can I make a piece of code repeat every x seconds?

Execute an instruction every x seconds in a for loop of an arraylist using JAVA

CSHARP running while every X seconds but until bool is true

Updating Database every X seconds

Update marker on google map every x seconds

How to run a thread every x seconds without using timer control

Update html page every X seconds using ajax

Run certain code after x seconds, every n seconds in python

Perform command every X seconds

Fade in and Fade out watermark on a video every x seconds/minutes for y seconds/minutes using FFMPEG

Extract x seconds from every minute with ffmpeg

jQuery each - every X seconds?

Best way to request data from server every x seconds

Repeating a task every x seconds while running in background

Check for proccess, if not running execute process, Kill process every X seconds

Update google java map with mysql data every X seconds

run code every X seconds

Run code every "x" seconds without setInterval

Updating variable pulled from api every x seconds

Loading data from database in every few seconds without page refresh

Running a request in background every x seconds using Locust

Running CRON every 10 seconds