4040import java .util .function .Predicate ;
4141import java .util .function .UnaryOperator ;
4242import java .util .stream .Collectors ;
43+
4344import org .cloudfoundry .client .CloudFoundryClient ;
4445import org .cloudfoundry .client .v2 .OrderDirection ;
4546import org .cloudfoundry .client .v2 .applications .AbstractApplicationResource ;
154155import org .cloudfoundry .doppler .LogMessage ;
155156import org .cloudfoundry .doppler .RecentLogsRequest ;
156157import org .cloudfoundry .doppler .StreamRequest ;
158+ import org .cloudfoundry .logcache .v1 .EnvelopeType ;
159+ import org .cloudfoundry .logcache .v1 .LogCacheClient ;
160+ import org .cloudfoundry .logcache .v1 .ReadRequest ;
157161import org .cloudfoundry .operations .util .OperationsLogging ;
158162import org .cloudfoundry .util .DateUtils ;
159163import org .cloudfoundry .util .DelayTimeoutException ;
@@ -189,8 +193,6 @@ public final class DefaultApplications implements Applications {
189193
190194 private static final int CF_STAGING_TIME_EXPIRED = 170007 ;
191195
192- private static final int CF_INSUFFICIENT_RESOURCES = 170008 ;
193-
194196 private static final String [] ENTRY_FIELDS_CRASH = {"index" , "reason" , "exit_description" };
195197
196198 private static final String [] ENTRY_FIELDS_NORMAL = {
@@ -200,6 +202,9 @@ public final class DefaultApplications implements Applications {
200202 private static final Comparator <LogMessage > LOG_MESSAGE_COMPARATOR =
201203 Comparator .comparing (LogMessage ::getTimestamp );
202204
205+ private static final Comparator <org .cloudfoundry .logcache .v1 .Envelope > LOG_MESSAGE_COMPARATOR_LOG_CACHE =
206+ Comparator .comparing (org .cloudfoundry .logcache .v1 .Envelope ::getTimestamp );
207+
203208 private static final Duration LOG_MESSAGE_TIMESPAN = Duration .ofMillis (500 );
204209
205210 private static final int MAX_NUMBER_OF_RECENT_EVENTS = 50 ;
@@ -715,11 +720,18 @@ public Mono<Void> pushManifestV3(PushManifestV3Request request) {
715720 .flatMap (
716721 function (
717722 (appId , packageId ) ->
718- buildAndStageAndWaitForRunning (
719- cloudFoundryClient ,
720- manifestApp ,
721- packageId ,
722- appId )))))
723+ buildAndStage (
724+ cloudFoundryClient ,
725+ manifestApp ,
726+ packageId )
727+ .flatMap (
728+ dropletId ->
729+ applyDropletAndWaitForRunning (
730+ cloudFoundryClient ,
731+ manifestApp
732+ .getName (),
733+ appId ,
734+ dropletId ))))))
723735 .then ();
724736 }
725737
@@ -1172,21 +1184,6 @@ private static Mono<Void> bindServices(
11721184 .then ();
11731185 }
11741186
1175- private static Mono <Void > buildAndStageAndWaitForRunning (
1176- CloudFoundryClient cloudFoundryClient ,
1177- ManifestV3Application manifestApp ,
1178- String packageId ,
1179- String appId ) {
1180- return buildAndStage (cloudFoundryClient , manifestApp , packageId )
1181- .flatMap (
1182- dropletId ->
1183- applyDropletAndWaitForRunning (
1184- cloudFoundryClient ,
1185- manifestApp .getName (),
1186- appId ,
1187- dropletId ));
1188- }
1189-
11901187 private static Mono <String > buildAndStage (
11911188 CloudFoundryClient cloudFoundryClient ,
11921189 ManifestV3Application manifestApp ,
@@ -1501,7 +1498,6 @@ private static Mono<ApplicationInstancesResponse> getApplicationInstances(
15011498 CF_INSTANCES_ERROR ,
15021499 CF_STAGING_NOT_FINISHED ,
15031500 CF_STAGING_TIME_EXPIRED ,
1504- CF_INSUFFICIENT_RESOURCES ,
15051501 CF_STAGING_ERROR ),
15061502 t -> Mono .just (ApplicationInstancesResponse .builder ().build ()));
15071503 }
@@ -1617,6 +1613,14 @@ private static Flux<LogMessage> getLogs(
16171613 }
16181614 }
16191615
1616+ private static Flux <LogMessage > getRecentLogs (Mono <LogCacheClient > logCacheClient , String applicationId ) {
1617+ return requestLogsRecentLogCache (logCacheClient , applicationId )
1618+ .filter (e -> EnvelopeType .LOG .getValue ().equals (e .getLog ().getType ().getValue ()))
1619+ .map (org .cloudfoundry .logcache .v1 .Envelope ::getLog )
1620+ .collectSortedList (LOG_MESSAGE_COMPARATOR_LOG_CACHE )
1621+ .flatMapIterable (d -> d );
1622+ }
1623+
16201624 @ SuppressWarnings ("unchecked" )
16211625 private static Map <String , Object > getMetadataRequest (EventEntity entity ) {
16221626 Map <String , Optional <Object >> metadata =
@@ -2509,6 +2513,32 @@ private static Flux<Envelope> requestLogsRecent(
25092513 RecentLogsRequest .builder ().applicationId (applicationId ).build ()));
25102514 }
25112515
2516+ private static Flux <org .cloudfoundry .logcache .v1 .Envelope > requestLogsRecentLogCache (
2517+ Mono <LogCacheClient > logCacheClient , String applicationId ) {
2518+ return logCacheClient .flatMapMany (
2519+ client ->
2520+ client .recentLogs (
2521+ ReadRequest .builder ()
2522+ .sourceId (applicationId )
2523+ .envelopeType (EnvelopeType .LOG )
2524+ .limit (100 )
2525+ .build ()
2526+ )
2527+ .flatMap (
2528+ response ->
2529+ Mono .justOrEmpty (
2530+ response .getEnvelopes ().getBatch ().stream ().findFirst ()
2531+ )
2532+ )
2533+ .repeatWhenEmpty (
2534+ exponentialBackOff (
2535+ Duration .ofSeconds (1 ),
2536+ Duration .ofSeconds (5 ),
2537+ Duration .ofMinutes (1 ))
2538+ )
2539+ );
2540+ }
2541+
25122542 private static Flux <Envelope > requestLogsStream (
25132543 Mono <DopplerClient > dopplerClient , String applicationId ) {
25142544 return dopplerClient .flatMapMany (
0 commit comments