-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Will propagated to client via SSE in ith own channel, to inform clients about CRC status change. Status modified in "status_change_stream.go", as current status implementation has no single point of change, so tracking transition between states is challenging. Signed-off-by: Yevhen Vydolob <[email protected]>
- Loading branch information
Showing
5 changed files
with
116 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package events | ||
|
||
import ( | ||
"encoding/json" | ||
|
||
"github.com/crc-org/crc/v2/pkg/crc/logging" | ||
"github.com/crc-org/crc/v2/pkg/crc/machine" | ||
"github.com/crc-org/crc/v2/pkg/crc/machine/state" | ||
"github.com/crc-org/crc/v2/pkg/crc/machine/types" | ||
"github.com/crc-org/crc/v2/pkg/events" | ||
"github.com/r3labs/sse/v2" | ||
) | ||
|
||
type serializableEvent struct { | ||
Status *types.ClusterStatusResult `json:"status"` | ||
Error string `json:"error,omitempty"` | ||
} | ||
|
||
type statusChangeListener struct { | ||
machineClient machine.Client | ||
publisher EventPublisher | ||
} | ||
|
||
func newStatusChangeStream(server *EventServer) EventStream { | ||
return newStream(newStatusChangeListener(server.machine), newEventPublisher(StatusChange, server.sseServer)) | ||
} | ||
|
||
func newStatusChangeListener(client machine.Client) EventProducer { | ||
return &statusChangeListener{ | ||
machineClient: client, | ||
} | ||
} | ||
|
||
func (st *statusChangeListener) Notify(changedEvent events.StatusChangedEvent) { | ||
logging.Debugf("State Changed Event %s", changedEvent) | ||
var event serializableEvent | ||
status, err := st.machineClient.Status() | ||
// if we cannot receive actual state, send error state with error description | ||
if err != nil { | ||
event = serializableEvent{Status: &types.ClusterStatusResult{ | ||
CrcStatus: state.Error, | ||
}, Error: err.Error()} | ||
} else { | ||
// event could be fired, before actual code, which change state is called | ||
// so status could contain 'old' state, replace it with state received in event | ||
status.CrcStatus = changedEvent.State // override with actual reported state | ||
event = serializableEvent{Status: status} | ||
if changedEvent.Error != nil { | ||
event.Error = changedEvent.Error.Error() | ||
} | ||
|
||
} | ||
data, err := json.Marshal(event) | ||
if err != nil { | ||
logging.Errorf("Could not serealize status changed event in to JSON: %s", err) | ||
return | ||
} | ||
st.publisher.Publish(&sse.Event{Event: []byte(StatusChange), Data: data}) | ||
} | ||
|
||
func (st *statusChangeListener) Start(publisher EventPublisher) { | ||
st.publisher = publisher | ||
events.StatusChanged.AddListener(st) | ||
|
||
} | ||
|
||
func (st *statusChangeListener) Stop() { | ||
events.StatusChanged.RemoveListener(st) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package events | ||
|
||
import ( | ||
"github.com/crc-org/crc/v2/pkg/crc/machine/state" | ||
) | ||
|
||
type StatusChangedEvent struct { | ||
State state.State | ||
Error error | ||
} | ||
|
||
var ( | ||
StatusChanged = NewEvent[StatusChangedEvent]() | ||
) |