Added an events bus, and pushed OpportunitiesUpdated to WebSocket clients when the fetcher stores new opportunities
internal/events decouples the parts of JobSearch that cause events from
the parts that react to them. bus.go has Bus: Subscribe, and a synchronous
Publish that calls each handler in turn on the publisher's goroutine, so
handlers mustn't block (the hub doesn't: it queues per client). events.go
has the event types and their payloads, starting with OpportunitiesUpdated.
With fetcher.WithEvents, a save that stores new opportunities, or new
releases of stored ones, publishes OpportunitiesUpdated with their UUIDs and
counts. An opportunity whose row is only rewritten, as when it is fetched
again from the newest stored release, isn't counted as updated, and nothing
is published when nothing changed. store.SaveResult now reports
NewOpportunity for each opportunity saved.
job-search serve subscribes its WebSocket hub to the bus, so every client
gets {"type":"event","eventType":"OpportunitiesUpdated","payload":{"new":
[...],"updated":[...],"summary":{"new":n,"updated":n}}}. The Envelope gains
an eventType field, and Hub.PushEvent and Server.PushEvent send events.
This commit is contained in:
1 parent
284a589627
commit
6e101d2b4d
13 files changed
+448
-26
No files matched your search
@@ -16,6 +16,7 @@ import (
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"git.avsdev.uk/Go/job-search/internal/cli"
|
||||
"git.avsdev.uk/Go/job-search/internal/events"
|
||||
"git.avsdev.uk/Go/job-search/internal/fetcher"
|
||||
"git.avsdev.uk/Go/job-search/internal/logging"
|
||||
"git.avsdev.uk/Go/job-search/internal/server"
|
||||
@@ -30,9 +31,10 @@ func NewCommand(opts *cli.Options) *cobra.Command {
|
||||
Long: `Serve the REST API under /api and the WebSocket endpoint under /ws.
|
||||
|
||||
It also runs a worker for every enabled source, as fetcher service does,
|
||||
saving what they fetch to database.file if it is set. A source that stops
|
||||
with an error stops the server too, and the server stopping stops the
|
||||
sources.`,
|
||||
saving what they fetch to database.file if it is set, and pushing an
|
||||
OpportunitiesUpdated event to every WebSocket client when a save stores new
|
||||
opportunities or new releases. A source that stops with an error stops the
|
||||
server too, and the server stopping stops the sources.`,
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
slog.Debug("command persistent pre-run", "command", "serve")
|
||||
},
|
||||
@@ -136,6 +138,14 @@ sources.`,
|
||||
)
|
||||
}
|
||||
|
||||
// Events from the sources, such as new opportunities, are pushed to
|
||||
// every WebSocket client.
|
||||
var bus events.Bus
|
||||
bus.Subscribe(func(event events.Event) {
|
||||
appServer.PushEvent(event.Type, event.Payload)
|
||||
})
|
||||
fetcherOptions = append(fetcherOptions, fetcher.WithEvents(&bus))
|
||||
|
||||
sourceFetcher := fetcher.New(&conf.Sources, fetcherOptions...)
|
||||
fetcherDone := make(chan error, 1)
|
||||
go func() {
|
||||
|
||||
Reference in new issue
Block a user