Skip to content

Commit

Permalink
reverting changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Raezil committed Dec 7, 2024
1 parent 4439125 commit 410fea6
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
3 changes: 1 addition & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,9 @@ func main() {
}
```

3. Get the dependency
```sh
go get github.com/Shibbaz/[email protected]
go get github.com/Raezil/GoEventBus
```

4. Run the project
Expand Down
24 changes: 17 additions & 7 deletions eventstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,23 @@ import (
"fmt"
"log"
"sync"

"github.com/streadway/amqp"
)

// RabbitMQ settings
const (
rabbitMQURL = "amqp://guest:guest@localhost:5672/"
rabbitMQExchange = "events_exchange"
)

// EventStore handles publishing and dispatching events
type EventStore struct {
Mutex sync.Mutex
Dispatcher *Dispatcher
Events *sync.Pool
RabbitMQ *amqp.Connection
Channel *amqp.Channel
}

// NewEventStore initializes an EventStore with a dispatcher and an event pool
Expand All @@ -28,6 +38,10 @@ func NewEventStore(dispatcher *Dispatcher) *EventStore {

// Publish adds an event to the event pool
func (eventstore *EventStore) Publish(event *Event) {
if event == nil {
log.Println("Attempted to publish a nil event")
return
}
eventstore.Events.Put(event)
}

Expand Down Expand Up @@ -59,8 +73,7 @@ func (eventstore *EventStore) Commit() error {
return fmt.Errorf("error handling event: %w", err)
}

log.Printf("Event id: %s was successfully published", event.Id)

log.Printf("Event id: %s was successfully processed", event.Id)
return nil
}

Expand All @@ -70,18 +83,15 @@ func (eventstore *EventStore) Broadcast() error {
defer eventstore.Mutex.Unlock()

var lastErr error
// Try to commit an event
for {
err := eventstore.Commit()
if err != nil {
// If there are no more events to process, break the loop
if err.Error() != "" {
if err.Error() == "no events to process" {
break
}
// Capture the last error if something else goes wrong
lastErr = err
log.Printf("Error processing event: %v", err)
}

}

return lastErr
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/Raezil/GoEventBus

go 1.22.2

require github.com/google/uuid v1.6.0
require (
github.com/google/uuid v1.6.0
github.com/streadway/amqp v1.1.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/streadway/amqp v1.1.0 h1:py12iX8XSyI7aN/3dUT8DFIDJazNJsVJdxNVEpnQTZM=
github.com/streadway/amqp v1.1.0/go.mod h1:WYSrTEYHOXHd0nwFeUXAe2G2hRnQT+deZJJf88uS9Bg=
4 changes: 3 additions & 1 deletion go.work
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
go 1.22.2

use .
use (
.
)

0 comments on commit 410fea6

Please sign in to comment.