2222
2323import java .io .OutputStream ;
2424import java .net .URI ;
25+ import java .time .Duration ;
2526import java .util .ArrayList ;
2627import java .util .Collections ;
2728import java .util .List ;
3637import javax .annotation .PreDestroy ;
3738import javax .inject .Inject ;
3839
40+ import eu .openanalytics .containerproxy .event .ProxyStartFailedEvent ;
41+ import eu .openanalytics .containerproxy .event .ProxyStartEvent ;
42+ import eu .openanalytics .containerproxy .event .ProxyStopEvent ;
3943import org .apache .logging .log4j .LogManager ;
4044import org .apache .logging .log4j .Logger ;
45+ import org .springframework .context .ApplicationEventPublisher ;
4146import org .springframework .security .access .AccessDeniedException ;
4247import org .springframework .stereotype .Service ;
4348
@@ -92,7 +97,10 @@ public class ProxyService {
9297
9398 @ Inject
9499 private LogService logService ;
95-
100+
101+ @ Inject
102+ private ApplicationEventPublisher applicationEventPublisher ;
103+
96104 @ PreDestroy
97105 public void shutdown () {
98106 try {
@@ -222,6 +230,7 @@ public Proxy startProxy(ProxySpec spec, boolean ignoreAccessControl) throws Cont
222230 backend .startProxy (proxy );
223231 } finally {
224232 if (proxy .getStatus () != ProxyStatus .Up ) activeProxies .remove (proxy );
233+ applicationEventPublisher .publishEvent (new ProxyStartFailedEvent (this , proxy .getUserId (), spec .getId ()));
225234 }
226235
227236 for (Entry <String , URI > target : proxy .getTargets ().entrySet ()) {
@@ -239,7 +248,9 @@ public Proxy startProxy(ProxySpec spec, boolean ignoreAccessControl) throws Cont
239248
240249 log .info (String .format ("Proxy activated [user: %s] [spec: %s] [id: %s]" , proxy .getUserId (), spec .getId (), proxy .getId ()));
241250 eventService .post (EventType .ProxyStart .toString (), proxy .getUserId (), spec .getId ());
242-
251+
252+ applicationEventPublisher .publishEvent (new ProxyStartEvent (this , proxy .getUserId (), spec .getId (), Duration .ofMillis (proxy .getStartupTimestamp () - proxy .getCreatedTimestamp ())));
253+
243254 return proxy ;
244255 }
245256
@@ -263,6 +274,11 @@ public void stopProxy(Proxy proxy, boolean async, boolean ignoreAccessControl) {
263274 logService .detach (proxy );
264275 log .info (String .format ("Proxy released [user: %s] [spec: %s] [id: %s]" , proxy .getUserId (), proxy .getSpec ().getId (), proxy .getId ()));
265276 eventService .post (EventType .ProxyStop .toString (), proxy .getUserId (), proxy .getSpec ().getId ());
277+
278+ applicationEventPublisher .publishEvent (new ProxyStopEvent (this , proxy .getUserId (),
279+ proxy .getSpec ().getId (),
280+ Duration .ofMillis (System .currentTimeMillis () - proxy .getStartupTimestamp ())));
281+
266282 } catch (Exception e ){
267283 log .error ("Failed to release proxy " + proxy .getId (), e );
268284 }
0 commit comments