This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

feat(scheduler): add message show_scheduled_jobs to display a table in the logs

+54 -5
+2 -1
events.go
··· 8 8 // EventClearScheduledJobs is used to clear all future scheduled jobs for a given churros object 9 9 // For example, when adding a new ticket to an event, we want to unschedule all future notifications for the event since the shotgun date may have changed 10 10 EventClearScheduledJobs Event = "clear_scheduled_jobs" 11 + EventShowScheduledJobs Event = "show_scheduled_jobs" 11 12 EventNewPost Event = "new_post" 12 13 EventGodchildRequest Event = "godchild_request" 13 14 EventNewComment Event = "new_comment" ··· 30 31 // When to push the notification 31 32 SendAt time.Time `json:"send_at"` 32 33 // Type of event that triggered the notification 33 - Event Event `json:"event" jsonschema:"enum=clear_scheduled_jobs,enum=new_post,enum=godchild_request,enum=new_comment,enum=comment_reply,enum=custom,enum=test,enum=godchild_accepted,enum=godchild_rejected,enum=pending_signup,enum=login_stuck,enum=booking_paid,enum=contribution_paid,enum=shotgun_opens_soon,enum=shotgun_closes_soon"` 34 + Event Event `json:"event" jsonschema:"enum=clear_scheduled_jobs,enum=show_scheduled_jobs,enum=new_post,enum=godchild_request,enum=new_comment,enum=comment_reply,enum=custom,enum=test,enum=godchild_accepted,enum=godchild_rejected,enum=pending_signup,enum=login_stuck,enum=booking_paid,enum=contribution_paid,enum=shotgun_opens_soon,enum=shotgun_closes_soon"` 34 35 // Churros ID of the ressource (the ticket, the post, the comment, etc) 35 36 // Used to determine to whom the notification should be sent 36 37 // For godchild_request, this is not a user id, but a godparent request id.
+8
messages.go
··· 70 70 } 71 71 return string(out) 72 72 } 73 + 74 + func (msg Message) JSONBytes() []byte { 75 + out, err := json.Marshal(msg) 76 + if err != nil { 77 + return nil 78 + } 79 + return out 80 + }
+3 -2
receiver.go
··· 18 18 return fmt.Errorf("while unmarshaling received message: %w", err) 19 19 } 20 20 21 - ll.Log("Received", "cyan", "%-10s | %-10s on %s", message.Id, message.Event, message.ChurrosObjectId) 22 - CreateInDatabaseNotification(message, "feur") 21 + if message.Event != EventShowScheduledJobs { 22 + ll.Log("Received", "cyan", "%-10s | %-10s on %s", message.Id, message.Event, message.ChurrosObjectId) 23 + } 23 24 24 25 if message.Event == EventClearScheduledJobs { 25 26 UnscheduleAllForObject(message.ChurrosObjectId)
+19 -2
scheduler.go
··· 8 8 var schedules = cmap.New[Message]() 9 9 10 10 func (job Message) Unschedule() { 11 + ll.Debug("Unscheduling %s", job.Id) 11 12 schedules.Remove(job.Id) 12 13 } 13 14 ··· 21 22 } 22 23 23 24 func (job Message) Schedule() { 24 - ll.Log("Scheduling", "magenta", "%s for %s", job.Id, job.SendAt) 25 + if job.Event != EventShowScheduledJobs { 26 + ll.Log("Scheduling", "magenta", "%s for %s", job.Id, job.SendAt) 27 + } 25 28 schedules.Set(job.Id, job) 26 29 } 27 30 ··· 34 37 for { 35 38 for _, job := range schedules.Items() { 36 39 if job.ShouldRun() { 37 - ll.Log("Running", "cyan", "[dim]%s[reset] job for %s on %s", job.Id, job.Event, job.ChurrosObjectId) 40 + if job.Event != EventShowScheduledJobs { 41 + ll.Log("Running", "cyan", "[dim]%s[reset] job for %s on %s", job.Id, job.Event, job.ChurrosObjectId) 42 + } 38 43 job.Unschedule() 39 44 go func() { 45 + if job.Event == EventShowScheduledJobs { 46 + ShowScheduledJobs() 47 + return 48 + } 40 49 err := job.Run() 41 50 if err != nil { 42 51 ll.ErrorDisplay("could not run job %s", err, job.Id) ··· 47 56 } 48 57 } 49 58 } 59 + 60 + func ShowScheduledJobs() { 61 + ll.Log("Showing", "magenta", "%d scheduled jobs", schedules.Count()) 62 + ll.Log("", "reset", "[dim]%-15s | %-20s | %-20s", "ID", "Event", "Object ID") 63 + for _, job := range schedules.Items() { 64 + ll.Log("", "reset", "%-15s | %-20s | %-20s", job.Id, job.Event, job.ChurrosObjectId) 65 + } 66 + }
+22
server/main.go
··· 107 107 cancel() 108 108 }() 109 109 110 + // Send EventShowScheduledJobs to the stream every 5 minutes 111 + go func() { 112 + for { 113 + select { 114 + case <-ctx.Done(): 115 + return 116 + default: 117 + msg := notella.Message{ 118 + Id: fmt.Sprintf("show-jobs-%d:%d", time.Now().Hour(), time.Now().Minute()), 119 + Event: notella.EventShowScheduledJobs, 120 + } 121 + 122 + _, err := js.Publish(notella.SubjectName, msg.JSONBytes()) 123 + if err != nil { 124 + ll.ErrorDisplay("could not send scheduled jobs message", err) 125 + } 126 + 127 + time.Sleep(5 * time.Minute) 128 + } 129 + } 130 + }() 131 + 110 132 // Continuously fetch and process messages 111 133 go func() { 112 134 for {