18
18
19
19
import * as extensionApi from '@podman-desktop/api' ;
20
20
import type { Status , CrcStatus as CrcStatusApi } from './types' ;
21
- import { commander } from './daemon-commander' ;
21
+ import { commander , getCrcApiUrl } from './daemon-commander' ;
22
22
import { isNeedSetup } from './crc-setup' ;
23
+ import { EventSource } from './events/eventsource' ;
24
+ import type { CrcVersion } from './crc-cli' ;
25
+ import { compare } from 'compare-versions' ;
26
+ import { PRE_SSE_VERSION } from './constants' ;
23
27
24
28
const defaultStatus : Status = { CrcStatus : 'Unknown' , Preset : 'openshift' } ;
25
29
const setupStatus : Status = { CrcStatus : 'Need Setup' , Preset : 'Unknown' } ;
26
30
const errorStatus : Status = { CrcStatus : 'Error' , Preset : 'Unknown' } ;
27
31
28
32
export class CrcStatus {
29
- private updateTimer : NodeJS . Timer ;
30
33
private _status : Status ;
31
34
private isSetupGoing : boolean ;
35
+ private statusEventSource : EventSource ;
36
+ private crcVersion : CrcVersion ;
37
+ private updateTimer : NodeJS . Timer ;
32
38
private statusChangeEventEmitter = new extensionApi . EventEmitter < Status > ( ) ;
33
39
public readonly onStatusChange = this . statusChangeEventEmitter . event ;
34
40
@@ -37,30 +43,61 @@ export class CrcStatus {
37
43
}
38
44
39
45
startStatusUpdate ( ) : void {
40
- if ( this . updateTimer ) {
41
- return ; // we already set timer
42
- }
43
- this . updateTimer = setInterval ( async ( ) => {
44
- try {
45
- // we don't need to update status while setup is going
46
- if ( this . isSetupGoing ) {
47
- this . _status = createStatus ( 'Starting' , this . _status . Preset ) ;
48
- return ;
46
+ if ( compare ( this . crcVersion . version , PRE_SSE_VERSION , '<=' ) ) {
47
+ if ( this . updateTimer ) {
48
+ return ; // we already set timer
49
+ }
50
+ this . updateTimer = setInterval ( async ( ) => {
51
+ try {
52
+ // we don't need to update status while setup is going
53
+ if ( this . isSetupGoing ) {
54
+ this . _status = createStatus ( 'Starting' , this . _status . Preset ) ;
55
+ return ;
56
+ }
57
+ const oldStatus = this . _status ;
58
+ this . _status = await commander . status ( ) ;
59
+ // notify listeners when status changed
60
+ if ( oldStatus . CrcStatus !== this . _status . CrcStatus ) {
61
+ this . statusChangeEventEmitter . fire ( this . _status ) ;
62
+ }
63
+ } catch ( e ) {
64
+ console . error ( 'CRC Status tick: ' + e ) ;
65
+ this . _status = defaultStatus ;
49
66
}
50
- const oldStatus = this . _status ;
51
- this . _status = await commander . status ( ) ;
52
- // notify listeners when status changed
53
- if ( oldStatus . CrcStatus !== this . _status . CrcStatus ) {
67
+ } , 2000 ) ;
68
+ } else {
69
+ if ( this . statusEventSource ) {
70
+ return ;
71
+ }
72
+
73
+ this . statusEventSource = new EventSource ( getCrcApiUrl ( ) + '/events?stream=status_change' ) ;
74
+ this . statusEventSource . on ( 'status_change' , ( e : MessageEvent ) => {
75
+ const data = e . data ;
76
+ try {
77
+ if ( this . isSetupGoing ) {
78
+ this . _status = createStatus ( 'Starting' , this . _status . Preset ) ;
79
+ return ;
80
+ }
81
+ console . error ( `On Status: ${ data } ` ) ;
82
+ this . _status = JSON . parse ( data ) . status ;
54
83
this . statusChangeEventEmitter . fire ( this . _status ) ;
84
+ } catch ( err ) {
85
+ console . error ( err ) ;
86
+ this . _status = defaultStatus ;
55
87
}
56
- } catch ( e ) {
57
- console . error ( 'CRC Status tick: ' + e ) ;
88
+ } ) ;
89
+ this . statusEventSource . on ( 'error' , e => {
90
+ console . error ( e ) ;
58
91
this . _status = defaultStatus ;
59
- }
60
- } , 2000 ) ;
92
+ } ) ;
93
+ }
61
94
}
62
95
63
96
stopStatusUpdate ( ) : void {
97
+ if ( this . statusEventSource ) {
98
+ this . statusEventSource . close ( ) ;
99
+ this . statusEventSource = undefined ;
100
+ }
64
101
if ( this . updateTimer ) {
65
102
clearInterval ( this . updateTimer ) ;
66
103
}
@@ -74,7 +111,8 @@ export class CrcStatus {
74
111
this . _status = errorStatus ;
75
112
}
76
113
77
- async initialize ( ) : Promise < void > {
114
+ async initialize ( crcVersion : CrcVersion ) : Promise < void > {
115
+ this . crcVersion = crcVersion ;
78
116
if ( isNeedSetup ) {
79
117
this . _status = setupStatus ;
80
118
return ;
@@ -107,7 +145,7 @@ export class CrcStatus {
107
145
case 'Stopping' :
108
146
return 'stopping' ;
109
147
case 'Stopped' :
110
- case 'No Cluster ' :
148
+ case 'NoVM ' :
111
149
return 'stopped' ;
112
150
default :
113
151
return 'unknown' ;
@@ -124,7 +162,7 @@ export class CrcStatus {
124
162
return 'stopping' ;
125
163
case 'Stopped' :
126
164
return 'configured' ;
127
- case 'No Cluster ' :
165
+ case 'NoVM ' :
128
166
return 'installed' ;
129
167
case 'Error' :
130
168
return 'error' ;
0 commit comments