From 19f180d71158c18334a5c4ffa97fc77678dcd4d4 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 8 Apr 2024 22:13:08 +0100 Subject: [PATCH] Expand CIF updates to ensure new sequence number is equal to oldSequence +1 --- cif/update.go | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/cif/update.go b/cif/update.go index a45f926..fa6c324 100644 --- a/cif/update.go +++ b/cif/update.go @@ -1,6 +1,7 @@ package cif import ( + "errors" "time" "git.fjla.uk/owlboard/timetable-mgr/dbAccess" @@ -70,15 +71,32 @@ func runCifUpdateDownload(cfg *helpers.Configuration, metadata *dbAccess.CifMeta return err } - // Check metadata sequence - Handle a metadata sequence error. Probably by deleting metadata so next update triggers full download - //// I need to check what the sequence looks like in a full download first. - //// Regarding metadata, I will need to replace 'metadata *dbAccess.CifMetadata' with the new metadata to ensure it is checked correctly in each iteration. + // Check CIF Sequence + // Skip if LastSequence is >= to this sequence + if metadata.LastSequence >= parsed.header.Metadata.Sequence { + log.Msg.Info("Skipping CIF file, already processed", zap.Int64("LastSequence", metadata.LastSequence), zap.Int64("New sequence", parsed.header.Metadata.Sequence)) + continue + } + + // Fail if sequence number is not as expected + if parsed.header.Metadata.Sequence != metadata.LastSequence+1 { + err := errors.New("out of sequence CIF data") + log.Msg.Error("CIF sequence not as expected", zap.Error(err), zap.Int64("LastSequence", metadata.LastSequence), zap.Int64("New Sequence", parsed.header.Metadata.Sequence)) + return err + } + + // Do further sequence checks - parsed.header.Metadata.Sequence MUST = metadata.LastSequence + 1 + + log.Msg.Debug("CIF Data has passed checks and should now be processed <<<<<<") // Process CIF file metadata = generateMetadata(&parsed.header) } - // Write metadata + ok := dbAccess.PutCifMetadata(metadata) + if !ok { + log.Msg.Warn("CIF Data updated, but metadata write failed.") + } return nil }