Skip to content

Instantly share code, notes, and snippets.

@aashish-chaubey
Last active November 16, 2023 20:06
Show Gist options
  • Select an option

  • Save aashish-chaubey/45e538959f92d8cc71327d44511cf7f7 to your computer and use it in GitHub Desktop.

Select an option

Save aashish-chaubey/45e538959f92d8cc71327d44511cf7f7 to your computer and use it in GitHub Desktop.

Revisions

  1. aashish-chaubey revised this gist Nov 16, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -62,4 +62,6 @@ func main() {
    fmt.Println("Error adding cron job:", err)
    return
    }
    start()
    select {}
    }
  2. aashish-chaubey created this gist Nov 16, 2023.
    65 changes: 65 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    package main

    import (
    "fmt"
    "http://github.com/robfig/cron/v3"
    "sync"
    "time"
    )

    func one() {
    fmt.Println("One")
    return
    }

    func two() {
    fmt.Println("Two")
    return
    }

    func three() {
    time.Sleep(5 * time.Second)
    fmt.Println("Three")
    return
    }

    func secondary_func(done chan bool) {
    defer close(done)

    one()
    two()
    three()

    }

    func main() {
    c := cron.New()

    _, err := c.AddFunc("*/2 * * * *", func() {
    users := []string{"user1", "user2", "user3"}
    var wg sync.WaitGroup

    for i := 0; i < len(users); i++ {
    wg.Add(1)
    go func(user string) {
    defer wg.Done()
    fmt.Println(user)

    done := make(chan bool)
    go secondary_func(done)

    select {
    case <-done:
    case <-time.After(2 * time.Second):
    fmt.Println("Goroutine timed out")
    }
    }(users[i])
    }
    wg.Wait()
    })

    if err != nil {
    fmt.Println("Error adding cron job:", err)
    return
    }
    }