You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
guhkun13
changed the title
Wrong Sync Cron
Wrong Result on Sync Cron
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:
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
The text was updated successfully, but these errors were encountered: