on main help sign in
fix(events): prevent early events from being dropped before init (#2611)
d9f857fChristian Rocha
internal/cmd/run.go+4−1
@@ -86,7 +86,6 @@ crush run --continue "Follow up on your last response"
86 }
86 }
87
87
88 event.SetNonInteractive(true)
88 event.SetNonInteractive(true)
89− event.AppInitialized()
90
89
91 switch {
90 switch {
92 case sessionID != "":
91 case sessionID != "":
@@ -102,6 +101,8 @@ crush run --continue "Follow up on your last response"
102 }
101 }
103 defer cleanup()
102 defer cleanup()
104
103
104+ event.AppInitialized()
105+
105 if sessionID != "" {
106 if sessionID != "" {
106 sess, err := resolveSessionByID(ctx, c, ws.ID, sessionID)
107 sess, err := resolveSessionByID(ctx, c, ws.ID, sessionID)
107 if err != nil {
108 if err != nil {
@@ -127,6 +128,8 @@ crush run --continue "Follow up on your last response"
127 }
128 }
128 defer cleanup()
129 defer cleanup()
129
130
131+ event.AppInitialized()
132+
130 if !ws.Config().IsConfigured() {
133 if !ws.Config().IsConfigured() {
131 return fmt.Errorf("no providers configured - please run 'crush' to set up a provider interactively")
134 return fmt.Errorf("no providers configured - please run 'crush' to set up a provider interactively")
132 }
135 }
internal/cmd/session.go+17−9
@@ -107,13 +107,16 @@ func sessionSetup(cmd *cobra.Command) (context.Context, *sessionServices, func()
107 dataDir, _ := cmd.Flags().GetString("data-dir")
107 dataDir, _ := cmd.Flags().GetString("data-dir")
108 ctx := cmd.Context()
108 ctx := cmd.Context()
109
109
110+ cfg, err := config.Init("", dataDir, false)
111+ if err != nil {
112+ return nil, nil, nil, fmt.Errorf("failed to initialize config: %w", err)
113+ }
110 if dataDir == "" {
114 if dataDir == "" {
111− cfg, err := config.Init("", "", false)
112− if err != nil {
113− return nil, nil, nil, fmt.Errorf("failed to initialize config: %w", err)
114− }
115 dataDir = cfg.Config().Options.DataDirectory
115 dataDir = cfg.Config().Options.DataDirectory
116 }
116 }
117+ if shouldEnableMetrics(cfg.Config()) {
118+ event.Init()
119+ }
117
120
118 conn, err := db.Connect(ctx, dataDir)
121 conn, err := db.Connect(ctx, dataDir)
119 if err != nil {
122 if err != nil {
@@ -130,7 +133,6 @@ func sessionSetup(cmd *cobra.Command) (context.Context, *sessionServices, func()
130
133
131func runSessionList(cmd *cobra.Command, _ []string) error {
134func runSessionList(cmd *cobra.Command, _ []string) error {
132 event.SetNonInteractive(true)
135 event.SetNonInteractive(true)
133− event.SessionListed(sessionListJSON)
134
136
135 ctx, svc, cleanup, err := sessionSetup(cmd)
137 ctx, svc, cleanup, err := sessionSetup(cmd)
136 if err != nil {
138 if err != nil {
@@ -138,6 +140,8 @@ func runSessionList(cmd *cobra.Command, _ []string) error {
138 }
140 }
139 defer cleanup()
141 defer cleanup()
140
142
143+ event.SessionListed(sessionListJSON)
144+
141 list, err := svc.sessions.List(ctx)
145 list, err := svc.sessions.List(ctx)
142 if err != nil {
146 if err != nil {
143 return fmt.Errorf("failed to list sessions: %w", err)
147 return fmt.Errorf("failed to list sessions: %w", err)
@@ -253,7 +257,6 @@ func resolveSessionID(ctx context.Context, svc session.Service, id string) (sess
253
257
254func runSessionShow(cmd *cobra.Command, args []string) error {
258func runSessionShow(cmd *cobra.Command, args []string) error {
255 event.SetNonInteractive(true)
259 event.SetNonInteractive(true)
256− event.SessionShown(sessionShowJSON)
257
260
258 ctx, svc, cleanup, err := sessionSetup(cmd)
261 ctx, svc, cleanup, err := sessionSetup(cmd)
259 if err != nil {
262 if err != nil {
@@ -261,6 +264,8 @@ func runSessionShow(cmd *cobra.Command, args []string) error {
261 }
264 }
262 defer cleanup()
265 defer cleanup()
263
266
267+ event.SessionShown(sessionShowJSON)
268+
264 sess, err := resolveSessionID(ctx, svc.sessions, args[0])
269 sess, err := resolveSessionID(ctx, svc.sessions, args[0])
265 if err != nil {
270 if err != nil {
266 return err
271 return err
@@ -280,7 +285,6 @@ func runSessionShow(cmd *cobra.Command, args []string) error {
280
285
281func runSessionDelete(cmd *cobra.Command, args []string) error {
286func runSessionDelete(cmd *cobra.Command, args []string) error {
282 event.SetNonInteractive(true)
287 event.SetNonInteractive(true)
283− event.SessionDeletedCommand(sessionDeleteJSON)
284
288
285 ctx, svc, cleanup, err := sessionSetup(cmd)
289 ctx, svc, cleanup, err := sessionSetup(cmd)
286 if err != nil {
290 if err != nil {
@@ -288,6 +292,8 @@ func runSessionDelete(cmd *cobra.Command, args []string) error {
288 }
292 }
289 defer cleanup()
293 defer cleanup()
290
294
295+ event.SessionDeletedCommand(sessionDeleteJSON)
296+
291 sess, err := resolveSessionID(ctx, svc.sessions, args[0])
297 sess, err := resolveSessionID(ctx, svc.sessions, args[0])
292 if err != nil {
298 if err != nil {
293 return err
299 return err
@@ -315,7 +321,6 @@ func runSessionDelete(cmd *cobra.Command, args []string) error {
315
321
316func runSessionRename(cmd *cobra.Command, args []string) error {
322func runSessionRename(cmd *cobra.Command, args []string) error {
317 event.SetNonInteractive(true)
323 event.SetNonInteractive(true)
318− event.SessionRenamed(sessionRenameJSON)
319
324
320 ctx, svc, cleanup, err := sessionSetup(cmd)
325 ctx, svc, cleanup, err := sessionSetup(cmd)
321 if err != nil {
326 if err != nil {
@@ -323,6 +328,8 @@ func runSessionRename(cmd *cobra.Command, args []string) error {
323 }
328 }
324 defer cleanup()
329 defer cleanup()
325
330
331+ event.SessionRenamed(sessionRenameJSON)
332+
326 sess, err := resolveSessionID(ctx, svc.sessions, args[0])
333 sess, err := resolveSessionID(ctx, svc.sessions, args[0])
327 if err != nil {
334 if err != nil {
328 return err
335 return err
@@ -351,7 +358,6 @@ func runSessionRename(cmd *cobra.Command, args []string) error {
351
358
352func runSessionLast(cmd *cobra.Command, _ []string) error {
359func runSessionLast(cmd *cobra.Command, _ []string) error {
353 event.SetNonInteractive(true)
360 event.SetNonInteractive(true)
354− event.SessionLastShown(sessionLastJSON)
355
361
356 ctx, svc, cleanup, err := sessionSetup(cmd)
362 ctx, svc, cleanup, err := sessionSetup(cmd)
357 if err != nil {
363 if err != nil {
@@ -359,6 +365,8 @@ func runSessionLast(cmd *cobra.Command, _ []string) error {
359 }
365 }
360 defer cleanup()
366 defer cleanup()
361
367
368+ event.SessionLastShown(sessionLastJSON)
369+
362 list, err := svc.sessions.List(ctx)
370 list, err := svc.sessions.List(ctx)
363 if err != nil {
371 if err != nil {
364 return fmt.Errorf("failed to list sessions: %w", err)
372 return fmt.Errorf("failed to list sessions: %w", err)
internal/cmd/stats.go+9−6
@@ -121,18 +121,21 @@ type HourDayHeatmapPt struct {
121}
121}
122
122
123func runStats(cmd *cobra.Command, _ []string) error {
123func runStats(cmd *cobra.Command, _ []string) error {
124− event.StatsViewed()
125−
126 dataDir, _ := cmd.Flags().GetString("data-dir")
124 dataDir, _ := cmd.Flags().GetString("data-dir")
127 ctx := cmd.Context()
125 ctx := cmd.Context()
128
126
127+ cfg, err := config.Init("", dataDir, false)
128+ if err != nil {
129+ return fmt.Errorf("failed to initialize config: %w", err)
130+ }
129 if dataDir == "" {
131 if dataDir == "" {
130− cfg, err := config.Init("", "", false)
131− if err != nil {
132− return fmt.Errorf("failed to initialize config: %w", err)
133− }
134 dataDir = cfg.Config().Options.DataDirectory
132 dataDir = cfg.Config().Options.DataDirectory
135 }
133 }
134+ if shouldEnableMetrics(cfg.Config()) {
135+ event.Init()
136+ }
137+
138+ event.StatsViewed()
136
139
137 conn, err := db.Connect(ctx, dataDir)
140 conn, err := db.Connect(ctx, dataDir)
138 if err != nil {
141 if err != nil {