Home > Blockchain >  Run a background task in a SINGLE gofiber process
Run a background task in a SINGLE gofiber process

Time:10-26

I have a background task (cleanUp) that deletes outdated files.

func main() {
    // 4 procs/childs max
    runtime.GOMAXPROCS(4)

    // start a cleanup cron-job
    go cleanUp()

    app := fiber.New(fiber.Config{
        Prefork:   true,
    })
    app.Post("/", handleFileupload)
    log.Fatal(app.Listen(":4000"))
}

func cleanUp() {
    fmt.Println("Cleaning Up..")
    for {
        // deletes old files here
        time.Sleep(60 * time.Second)
    }
}

At the moment cleanUp runs on all 4 processes and prints Cleaning Up.. 4 times.

But I want it to run only on a single process and print a single Cleaning Up... I have tried waitGroups and channels but failed to achieve what I wanted.

How can I run it only on a single process to avoid race conditions?

Here is the output:

Cleaning Up..

 ┌───────────────────────────────────────────────────┐  ┌───────────────────────────────────────────────────┐
 │                   Fiber v2.38.1                   │  │ Child PIDs ... 79378, 79379, 79380, 79381         │
 │               http://127.0.0.1:4000               │  └───────────────────────────────────────────────────┘
 │       (bound on host 0.0.0.0 and port 4000)       │ 
 │                                                   │ 
 │ Handlers ............. 5  Processes ........... 4 │ 
 │ Prefork ........ Enabled  PID ............. 79377 │ 
 └───────────────────────────────────────────────────┘ 

Cleaning Up..
Cleaning Up..
Cleaning Up..
Cleaning Up..

CodePudding user response:

Fiber spawns several processes to handle incoming requests. What you need is launch the cleanup routine in the master process and skip it in the child processes.

Fiber provides fiber.IsChild function:

    // start a cleanup cron-job
    if !fiber.IsChild() {
        go cleanUp()
    }

I slightly modified your example to print process ID in the handler and the cleanup goroutine:

func main() {
    // 4 procs/childs max
    runtime.GOMAXPROCS(4)

    // start a cleanup cron-job
    if !fiber.IsChild() {
        go cleanUp()
    }

    app := fiber.New(fiber.Config{
        Prefork: true,
    })
    app.Post("/", handleFileupload)
    log.Fatal(app.Listen(":4000"))
}

func cleanUp() {
    fmt.Println("Cleaning Up.. Pid:", syscall.Getpid())
    for {
        // deletes old files here
        time.Sleep(1 * time.Second)
        fmt.Println("Cleaning Up.. Pid:", syscall.Getpid())
    }
}

func handleFileupload(ctx *fiber.Ctx) error {
    println("Upload: pid ", syscall.Getpid())
    return nil
}

Result:

Cleaning Up.. Pid: 27035

 ┌───────────────────────────────────────────────────┐  ┌───────────────────────────────────────────────────┐
 │                   Fiber v2.39.0                   │  │ Child PIDs ... 27041, 27042, 27044, 27045         │
 │               http://127.0.0.1:4000               │  └───────────────────────────────────────────────────┘
 │       (bound on host 0.0.0.0 and port 4000)       │ 
 │                                                   │ 
 │ Handlers ............. 1  Processes ........... 4 │ 
 │ Prefork ........ Enabled  PID ............. 27035 │ 
 └───────────────────────────────────────────────────┘ 

Cleaning Up.. Pid: 27035
Cleaning Up.. Pid: 27035
Cleaning Up.. Pid: 27035
Cleaning Up.. Pid: 27035
Cleaning Up.. Pid: 27035
Cleaning Up.. Pid: 27035
Cleaning Up.. Pid: 27035

As you see, cleanup runs in the master process only.

  • Related