1818using Newtonsoft . Json . Converters ;
1919using Newtonsoft . Json . Serialization ;
2020using Runner . Server . Models ;
21+ using Runner . Server . Services ;
2122
2223namespace Runner . Server . Controllers
2324{
@@ -28,33 +29,26 @@ public class TimeLineWebConsoleLogController : VssControllerBase
2829 {
2930
3031 private IMemoryCache _cache ;
32+ private readonly WebConsoleLogService _webConsoleLogService ;
3133
32- public TimeLineWebConsoleLogController ( IMemoryCache cache , IConfiguration conf ) : base ( conf )
34+ public TimeLineWebConsoleLogController ( IMemoryCache cache , IConfiguration conf , WebConsoleLogService webConsoleLogService ) : base ( conf )
3335 {
3436 _cache = cache ;
37+ _webConsoleLogService = webConsoleLogService ;
3538 }
3639
3740 [ HttpGet ( "{timelineId}/{recordId}" ) ]
3841 public IEnumerable < TimelineRecordLogLine > GetLogLines ( Guid timelineId , Guid recordId )
3942 {
40- if ( TimelineController . dict . TryGetValue ( timelineId , out var rec ) && rec . Item2 . TryGetValue ( recordId , out var value ) ) {
41- return from line in value where line != null select line ;
42- }
43- return null ;
43+ return _webConsoleLogService . GetLogLines ( timelineId , recordId ) ;
4444 }
4545
4646 [ HttpGet ( "{timelineId}" ) ]
4747 public ConcurrentDictionary < Guid , List < TimelineRecordLogLine > > GetLogLines ( Guid timelineId )
4848 {
49- if ( TimelineController . dict . TryGetValue ( timelineId , out var rec ) ) {
50- return rec . Item2 ;
51- }
52- return null ;
49+ return _webConsoleLogService . GetLogLines ( timelineId ) ;
5350 }
5451
55- public delegate void LogFeedEvent ( object sender , Guid timelineId , Guid recordId , TimelineRecordFeedLinesWrapper record ) ;
56- public static event LogFeedEvent logfeed ;
57-
5852 [ HttpGet ]
5953 public IActionResult Message ( [ FromQuery ] Guid timelineId , [ FromQuery ] long [ ] runid )
6054 {
@@ -64,17 +58,17 @@ public IActionResult Message([FromQuery] Guid timelineId, [FromQuery] long[] run
6458 await using ( var writer = new StreamWriter ( stream ) { NewLine = "\n " } ) {
6559 var queue2 = Channel . CreateUnbounded < KeyValuePair < string , string > > ( new UnboundedChannelOptions { SingleReader = true } ) ;
6660 var chwriter = queue2 . Writer ;
67- LogFeedEvent handler = ( sender , timelineId2 , recordId , record ) => {
68- ( List < TimelineRecord > , ConcurrentDictionary < Guid , List < TimelineRecordLogLine > > ) val ;
61+ WebConsoleLogService . LogFeedEvent handler = ( sender , timelineId2 , recordId , record ) => {
6962 Job job ;
70- if ( timelineId == timelineId2 || timelineId == Guid . Empty && ( runid . Length == 0 || TimelineController . dict . TryGetValue ( timelineId2 , out val ) && val . Item1 . Any ( ) && _cache . TryGetValue ( val . Item1 [ 0 ] . Id , out job ) && runid . Contains ( job . runid ) ) ) {
63+ TimelineRecord record1 ;
64+ if ( timelineId == timelineId2 || timelineId == Guid . Empty && ( runid . Length == 0 || ( record1 = _webConsoleLogService . GetTimeLine ( timelineId2 ) ? . FirstOrDefault ( ) ) != null && _cache . TryGetValue ( record1 . Id , out job ) && runid . Contains ( job . runid ) ) ) {
7165 chwriter . WriteAsync ( new KeyValuePair < string , string > ( "log" , JsonConvert . SerializeObject ( new { timelineId = timelineId2 , recordId , record } , new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver ( ) , Converters = new List < JsonConverter > { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy ( ) } } } ) ) ) ;
7266 }
7367 } ;
7468 TimelineController . TimeLineUpdateDelegate handler2 = ( timelineId2 , timeline ) => {
75- ( List < TimelineRecord > , ConcurrentDictionary < Guid , List < TimelineRecordLogLine > > ) val ;
7669 Job job ;
77- if ( timelineId == timelineId2 || timelineId == Guid . Empty && ( runid . Length == 0 || TimelineController . dict . TryGetValue ( timelineId2 , out val ) && val . Item1 . Any ( ) && _cache . TryGetValue ( val . Item1 [ 0 ] . Id , out job ) && runid . Contains ( job . runid ) ) ) {
70+ TimelineRecord record2 ;
71+ if ( timelineId == timelineId2 || timelineId == Guid . Empty && ( runid . Length == 0 || ( record2 = _webConsoleLogService . GetTimeLine ( timelineId2 ) ? . FirstOrDefault ( ) ) != null && _cache . TryGetValue ( record2 . Id , out job ) && runid . Contains ( job . runid ) ) ) {
7872 chwriter . WriteAsync ( new KeyValuePair < string , string > ( "timeline" , JsonConvert . SerializeObject ( new { timelineId = timelineId2 , timeline } , new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver ( ) , Converters = new List < JsonConverter > { new StringEnumConverter { NamingStrategy = new CamelCaseNamingStrategy ( ) } } } ) ) ) ;
7973 }
8074 } ;
@@ -111,7 +105,7 @@ public IActionResult Message([FromQuery] Guid timelineId, [FromQuery] long[] run
111105
112106 }
113107 } , requestAborted ) ;
114- logfeed += handler ;
108+ _webConsoleLogService . LogFeed += handler ;
115109 TimelineController . TimeLineUpdate += handler2 ;
116110 MessageController . OnRepoDownload += rd ;
117111 FinishJobController . OnJobCompleted += completed ;
@@ -121,7 +115,7 @@ public IActionResult Message([FromQuery] Guid timelineId, [FromQuery] long[] run
121115 } catch ( OperationCanceledException ) {
122116
123117 } finally {
124- logfeed -= handler ;
118+ _webConsoleLogService . LogFeed -= handler ;
125119 TimelineController . TimeLineUpdate -= handler2 ;
126120 MessageController . OnRepoDownload -= rd ;
127121 FinishJobController . OnJobCompleted -= completed ;
@@ -131,16 +125,6 @@ public IActionResult Message([FromQuery] Guid timelineId, [FromQuery] long[] run
131125 } , "text/event-stream" ) ;
132126 }
133127
134- public static void AppendTimelineRecordFeed ( TimelineRecordFeedLinesWrapper record , Guid timelineId , Guid recordId ) {
135- logfeed ? . Invoke ( null , timelineId , recordId , record ) ;
136- ( List < TimelineRecord > , ConcurrentDictionary < Guid , List < TimelineRecordLogLine > > ) timeline ;
137- timeline = TimelineController . dict . GetOrAdd ( timelineId , g => ( new List < TimelineRecord > ( ) , new ConcurrentDictionary < Guid , List < TimelineRecordLogLine > > ( ) ) ) ;
138- timeline . Item2 . AddOrUpdate ( record . StepId , t => record . Value . Select ( ( s , i ) => new TimelineRecordLogLine ( s , null ) ) . ToList ( ) , ( g , t ) => {
139- t . AddRange ( record . Value . Select ( ( s ) => new TimelineRecordLogLine ( s , null ) ) ) ;
140- return t ;
141- } ) ;
142- }
143-
144128 [ HttpPost ( "{scopeIdentifier}/{hubName}/{planId}/{timelineId}/{recordId}" ) ]
145129 [ Authorize ( AuthenticationSchemes = "Bearer" , Policy = "AgentJob" ) ]
146130 public IActionResult AppendTimelineRecordFeed ( Guid scopeIdentifier , string hubName , Guid planId , Guid timelineId , Guid recordId , [ FromBody , Vss ] TimelineRecordFeedLinesWrapper record )
@@ -150,7 +134,7 @@ public IActionResult AppendTimelineRecordFeed(Guid scopeIdentifier, string hubNa
150134 var nl = record . Value . SelectMany ( lines => regex . Split ( lines ) ) . ToList ( ) ;
151135 record . Value . Clear ( ) ;
152136 record . Value . AddRange ( nl ) ;
153- Task . Run ( ( ) => AppendTimelineRecordFeed ( record , timelineId , recordId ) ) ;
137+ Task . Run ( ( ) => _webConsoleLogService . AppendTimelineRecordFeed ( record , timelineId , recordId ) ) ;
154138 return Ok ( ) ;
155139 }
156140
@@ -181,7 +165,7 @@ public async Task Get(Guid timelineId)
181165 var regex = new Regex ( "\r ?\n " ) ;
182166 var nl = livelogfeed . Value . SelectMany ( lines => regex . Split ( lines ) ) . ToList ( ) ;
183167 var record = new TimelineRecordFeedLinesWrapper ( livelogfeed . StepId , nl ) ;
184- AppendTimelineRecordFeed ( record , timelineId , livelogfeed . StepId ) ;
168+ _webConsoleLogService . AppendTimelineRecordFeed ( record , timelineId , livelogfeed . StepId ) ;
185169 }
186170 }
187171 } finally {
0 commit comments