-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yevhen Vydolob <[email protected]>
- Loading branch information
Showing
8 changed files
with
122 additions
and
11 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
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,62 @@ | ||
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 statusChangeEvent struct { | ||
Status *types.ClusterStatusResult `json:"status"` | ||
Error string `json:"error,omitempty"` | ||
} | ||
|
||
type statusChange struct { | ||
listenerDisposable events.Disposable | ||
machineClient machine.Client | ||
} | ||
|
||
func newStatusChangeStream(server *EventServer) EventStream { | ||
return newStream(newStatusChangeListener(server.machine), newEventPublisher(StatusChange, server.sseServer)) | ||
} | ||
|
||
func newStatusChangeListener(client machine.Client) EventProducer { | ||
return &statusChange{ | ||
machineClient: client, | ||
} | ||
} | ||
|
||
func (st *statusChange) Start(publisher EventPublisher) { | ||
st.listenerDisposable = events.StatusChanged.AddListener(func(changedEvent events.StatusChangedEvent) { | ||
logging.Debugf("State Changed Event %s", changedEvent) | ||
var event statusChangeEvent | ||
status, err := st.machineClient.Status() | ||
// if we cannot receive actual state, send error state with error description | ||
if err != nil { | ||
event = statusChangeEvent{Status: &types.ClusterStatusResult{ | ||
CrcStatus: state.Error, | ||
}, Error: err.Error()} | ||
} else { | ||
status.CrcStatus = changedEvent.State // override with actual reported state | ||
event = statusChangeEvent{Status: status} | ||
if changedEvent.Error != nil { | ||
event.Error = changedEvent.Error.Error() | ||
} | ||
|
||
} | ||
data, _ := json.Marshal(event) | ||
publisher.Publish(&sse.Event{Event: []byte(StatusChange), Data: data}) | ||
}) | ||
} | ||
|
||
func (st *statusChange) Stop() { | ||
if st.listenerDisposable != nil { | ||
st.listenerDisposable() | ||
st.listenerDisposable = nil | ||
} | ||
} |
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,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]() | ||
) |