CPU usage increasing day by day because of Cron

I am running a cron whose code is written in Golang,and i am using mongoDb(version -4.4) as database, the cron runs after every 5 minutes of interval, i have been noticing that when i start the cron within a week the cpu usage hikes from 0 to 60% or even more and i have to restart the cron service so that it shouldn’t effect the services of cron later on, which means that there’s an memory leak, resources are not being released properly, i’m not able to figure out what might be the reason, i have been using the single db connection for the whole cron let’s say a single cron is being for 2000 merchants. I’m running my cron with Worker pool using Buffered Channels. Below is the code which i am using for running my cron using worker pool

func RunCronForDbNames(cronType, subsType string) {
	cronStatus := GetCronStatusConditions(cronType)
	if cronStatus {
		dbNames, _ := controller.GetDbNamesForCron(subsType, cronType)
		if len(dbNames) == 0 {
			return
		}
		client, ctx := ConnectDb("ip")
		defer client.Disconnect(ctx)
		cronDataChannel := make(chan view.CronChannelData, len(dbNames))
		var wg sync.WaitGroup
		startTime := time.Now().Unix()
		var cronChData view.CronChannelData
		cronChData.Database.Client = client
		cronChData.Database.Ctx = ctx
		cronChData.StartTime = startTime
		for _, dbName := range dbNames {
			isContinue := db.CheckGlobalCronStatus(cronType, dbNames)
			if !isContinue {
				continue
			}
			wg.Add(1)
			contextKeys := make(map[string]interface{})
			contextKeys["db_name"] = dbName
			contextKeys["role"] = ""
			var c gin.Context
			c.Keys = make(map[string]any)
			c.Keys = contextKeys
			cronChData.C = c
			cronChData.Database.MainDatabase = dbName
			cronDataChannel <- cronChData
		}
		close(cronDataChannel)
		for w := 1; w <= 10; w++ {
			go Worker(w, cronDataChannel, &wg, cronType)
		}
		wg.Wait()
	}
	return
}

My worker is running with 10 workers at a time

func Worker(id int, jobs <-chan view.CronChannelData, wg *sync.WaitGroup, cronType string) {
    switch cronType {
    case config.CompleteBookingCron:
        for job := range jobs {
            controller.CompleteBookingsCron(job, wg)
        }
    }
    return
}

In CompleteBookingsCron, I’m subtracting the WaitGroup using wg.Done() and marking the bookings as completed and send mails and Sms to our customers based on their settings. For sending Emails and Sms, go-routines are being used.

Can someone help me find out what could be the reason for the increasing of cpu usage, what things should i follow in order to getting the resources freed up properly which should not increase the Cpu usage?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.