Skip to content

Commit 571af10

Browse files
authored
Fix to expose existing rules (#100)
Signed-off-by: Kotaro Inoue <[email protected]>
1 parent 6fb8fad commit 571af10

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

internal/cmd/sync.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,11 @@ var syncCmd = &cli.Command{
7272
for {
7373
slog.Info("syncing...")
7474
if err := s.Sync(c.Context); err != nil {
75-
return err
75+
slog.Error("failed to sync", slog.String("error", err.Error()))
76+
} else {
77+
slog.Info("synced")
7678
}
77-
slog.Info("finish sync and sleep", slog.Int("interval", interval))
79+
slog.Info("sleep", slog.Int("interval", interval))
7880
time.Sleep(time.Duration(interval) * time.Second)
7981
}
8082
} else { // one shot mode

internal/syncer/syncer.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,14 @@ func (s *syncer) registerRuleToEPGStation(ctx context.Context, work annictWork)
175175
// recording rule with same keyword has already been registered
176176
// skip registration
177177
// TODO: Remove this logic after introducing cleanup logic
178+
slog.Debug("recording rule with same keyword has already been registered", slog.String("keyword", work.Title), slog.String("annict_work_id", work.ID), slog.Int("number_of_rules", len(rules)))
179+
for _, rule := range rules {
180+
syncerRecordingRuleSynced.WithLabelValues(strconv.Itoa(rule.Id), work.ID).Set(1)
181+
ruleIDs = append(ruleIDs, RecordingRuleID(rule.Id))
182+
}
183+
if err := s.setRecordingRuleIDsByAnnictWorkID(work.ID, ruleIDs); err != nil {
184+
return fmt.Errorf("failed to store recording rule IDs for Annict work ID %s: %w", work.ID, err)
185+
}
178186
slog.Debug("recording rule with same keyword has already been registered", slog.String("keyword", work.Title))
179187
return nil
180188
}
@@ -213,10 +221,12 @@ func (s *syncer) registerRuleToEPGStation(ctx context.Context, work annictWork)
213221
AllowEndLack: false,
214222
},
215223
}
224+
slog.Debug("registering rules into EPGStation", slog.String("annict_work_title", work.Title), slog.String("annict_work_id", work.ID))
216225
r, err := s.esClient.PostRules(ctx, body)
217226
if err != nil {
218227
return err
219228
}
229+
slog.Debug("got response from EPGStation", slog.String("annict_work_title", work.Title), slog.String("annict_work_id", work.ID), slog.Int("status_code", r.StatusCode))
220230
res, err := epgstation.ParsePostRulesResponse(r)
221231
if err != nil {
222232
return err
@@ -225,11 +235,12 @@ func (s *syncer) registerRuleToEPGStation(ctx context.Context, work annictWork)
225235
return fmt.Errorf("failed to register rules into EPGStation: %s", res.Body)
226236
}
227237
ids := RecordingRuleIDs{RecordingRuleID(res.JSON201.RuleId)}
238+
slog.Debug("store recording rule IDs into DB", slog.String("annict_work_title", work.Title), slog.String("annict_work_id", work.ID), slog.Int("number_of_rules", len(ids)), slog.Int("rule_id", int(ids[0])))
228239
if err := s.setRecordingRuleIDsByAnnictWorkID(work.ID, ids); err != nil {
229240
return err
230241
}
242+
slog.Debug("registered rules into EPGStation", slog.String("annict_work_title", work.Title), slog.String("annict_work_id", work.ID), slog.Int("number_of_rules", len(ids)))
231243
syncerRecordingRuleSynced.WithLabelValues(strconv.Itoa(int(ids[0])), work.ID).Set(1)
232-
// TODO(musaprg): output response in the log message
233244
return nil
234245
}
235246

0 commit comments

Comments
 (0)