Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong Result on Sync Cron #514

Closed
guhkun13 opened this issue Feb 23, 2024 · 1 comment
Closed

Wrong Result on Sync Cron #514

guhkun13 opened this issue Feb 23, 2024 · 1 comment

Comments

@guhkun13
Copy link

guhkun13 commented Feb 23, 2024

Hi, as I am developing my schedule service, I found that this package does not have any feature to save/persist the job that have been inserted, and so each time the program restarted, the job will also be gone.
So I came up with idea to save the job to the database and in my case, i am using mongodb.
The idea is each time the program restarted, I will do "sync cron" from my db to my cron.
Here is my code:

func SyncCron(myCollection *mongo.Collection) *cron.Cron {

	c := cron.New()

	filter := bson.M{}
	cursor, err := myCollection.Find(context.TODO(), filter)
	if err != nil {
		log.Fatal().Err(err).Msg("failed to find schedule collection")
	}

	records := model.MyModel{}
	err = cursor.All(context.TODO(), &records)
	if err != nil {
		log.Fatal().Err(err).Msg("failed to translate data from db to object")
	}

	for _, record := range records {
		entryIdInt := -1
		if record.IsActive {
			log.Info().
				Interface("record", record).
				Str("record.Url", record.Url).
				Msg("add record")

                       // iterate each record to be sync/saved again to the cron, 
                       // including the record.url (in this case, my command is to call some url using curl GET) 
                       // and the url is mostly unique
			entryId, err := c.AddFunc(record.Spec, func() {
				http.DefaultClient.Get(record.Url)
			})
			if err != nil {
				log.Fatal().Err(err).Msg("cron.AddFunc failed")
			}
			entryIdInt = int(entryId)
		}

		filter := bson.M{"compression_id": record.CompressionId}
		update := bson.M{"$set": bson.M{"entry_id": int(entryIdInt)}}

		updateRes, err := myCollection.UpdateOne(context.TODO(), filter, update)
		if err != nil {
			log.Fatal().Err(err).Msg("scheduleCollection.UpdateOne failed")
		}

		log.Info().Int64("updated count", updateRes.MatchedCount).Msg("update result")
	}

	c.Start()

	return c
}

But the problem is, when the cron running, all the records hit to the same url same with the last one from the db records.
For example I have 3 records, with url to Url-A, Url-B, Url-C.
After sync, all record will hit to Url-C instead of to 3 different URL.

Any help on why this happened will be very much appreciated.
Thank you

@guhkun13 guhkun13 changed the title Wrong Sync Cron Wrong Result on Sync Cron Feb 23, 2024
@guhkun13
Copy link
Author

guhkun13 commented Feb 23, 2024

[SOLVED]
I found the solution,
it seems that the parameter "cmd" with data type func() need to be returned from another function as a func.

-updated code below

  1. new function that will return func()
func createCurlCommand(url string) func() {
	return func() {
		http.DefaultClient.Get(url)
	}
}
  1. on line that insert it into new function.
entryId, err := c.AddFunc(record.Spec, createCurlCommand(record.Url))

now the result is right. ID called are unique and different from each other.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant