@@ -281,84 +281,98 @@ func ViewPost(ctx *context_module.Context) {
281281 resp .State .CurrentJob .Steps = make ([]* ViewJobStep , 0 ) // marshal to '[]' instead fo 'null' in json
282282 resp .Logs .StepsLog = make ([]* ViewStepLog , 0 ) // marshal to '[]' instead fo 'null' in json
283283 if task != nil {
284- steps := actions .FullSteps (task )
285-
286- for _ , v := range steps {
287- resp .State .CurrentJob .Steps = append (resp .State .CurrentJob .Steps , & ViewJobStep {
288- Summary : v .Name ,
289- Duration : v .Duration ().String (),
290- Status : v .Status .String (),
291- })
284+ steps , logs , err := convertToViewModel (ctx , req .LogCursors , task )
285+ if err != nil {
286+ ctx .Error (http .StatusInternalServerError , err .Error ())
287+ return
292288 }
289+ resp .State .CurrentJob .Steps = append (resp .State .CurrentJob .Steps , steps ... )
290+ resp .Logs .StepsLog = append (resp .Logs .StepsLog , logs ... )
291+ }
293292
294- for _ , cursor := range req .LogCursors {
295- if ! cursor .Expanded {
296- continue
297- }
293+ ctx .JSON (http .StatusOK , resp )
294+ }
295+
296+ func convertToViewModel (ctx * context_module.Context , cursors []LogCursor , task * actions_model.ActionTask ) ([]* ViewJobStep , []* ViewStepLog , error ) {
297+ var viewJobs []* ViewJobStep
298+ var logs []* ViewStepLog
299+
300+ steps := actions .FullSteps (task )
301+
302+ for _ , v := range steps {
303+ viewJobs = append (viewJobs , & ViewJobStep {
304+ Summary : v .Name ,
305+ Duration : v .Duration ().String (),
306+ Status : v .Status .String (),
307+ })
308+ }
298309
299- step := steps [cursor .Step ]
300-
301- // if task log is expired, return a consistent log line
302- if task .LogExpired {
303- if cursor .Cursor == 0 {
304- resp .Logs .StepsLog = append (resp .Logs .StepsLog , & ViewStepLog {
305- Step : cursor .Step ,
306- Cursor : 1 ,
307- Lines : []* ViewStepLogLine {
308- {
309- Index : 1 ,
310- Message : ctx .Locale .TrString ("actions.runs.expire_log_message" ),
311- // Timestamp doesn't mean anything when the log is expired.
312- // Set it to the task's updated time since it's probably the time when the log has expired.
313- Timestamp : float64 (task .Updated .AsTime ().UnixNano ()) / float64 (time .Second ),
314- },
310+ for _ , cursor := range cursors {
311+ if ! cursor .Expanded {
312+ continue
313+ }
314+
315+ step := steps [cursor .Step ]
316+
317+ // if task log is expired, return a consistent log line
318+ if task .LogExpired {
319+ if cursor .Cursor == 0 {
320+ logs = append (logs , & ViewStepLog {
321+ Step : cursor .Step ,
322+ Cursor : 1 ,
323+ Lines : []* ViewStepLogLine {
324+ {
325+ Index : 1 ,
326+ Message : ctx .Locale .TrString ("actions.runs.expire_log_message" ),
327+ // Timestamp doesn't mean anything when the log is expired.
328+ // Set it to the task's updated time since it's probably the time when the log has expired.
329+ Timestamp : float64 (task .Updated .AsTime ().UnixNano ()) / float64 (time .Second ),
315330 },
316- Started : int64 (step .Started ),
317- })
318- }
319- continue
331+ },
332+ Started : int64 (step .Started ),
333+ })
320334 }
335+ continue
336+ }
321337
322- logLines := make ([]* ViewStepLogLine , 0 ) // marshal to '[]' instead fo 'null' in json
323-
324- index := step .LogIndex + cursor .Cursor
325- validCursor := cursor .Cursor >= 0 &&
326- // !(cursor.Cursor < step.LogLength) when the frontend tries to fetch next line before it's ready.
327- // So return the same cursor and empty lines to let the frontend retry.
328- cursor .Cursor < step .LogLength &&
329- // !(index < task.LogIndexes[index]) when task data is older than step data.
330- // It can be fixed by making sure write/read tasks and steps in the same transaction,
331- // but it's easier to just treat it as fetching the next line before it's ready.
332- index < int64 (len (task .LogIndexes ))
333-
334- if validCursor {
335- length := step .LogLength - cursor .Cursor
336- offset := task .LogIndexes [index ]
337- logRows , err := actions .ReadLogs (ctx , task .LogInStorage , task .LogFilename , offset , length )
338- if err != nil {
339- ctx .ServerError ("actions.ReadLogs" , err )
340- return
341- }
342-
343- for i , row := range logRows {
344- logLines = append (logLines , & ViewStepLogLine {
345- Index : cursor .Cursor + int64 (i ) + 1 , // start at 1
346- Message : row .Content ,
347- Timestamp : float64 (row .Time .AsTime ().UnixNano ()) / float64 (time .Second ),
348- })
349- }
338+ logLines := make ([]* ViewStepLogLine , 0 ) // marshal to '[]' instead fo 'null' in json
339+
340+ index := step .LogIndex + cursor .Cursor
341+ validCursor := cursor .Cursor >= 0 &&
342+ // !(cursor.Cursor < step.LogLength) when the frontend tries to fetch next line before it's ready.
343+ // So return the same cursor and empty lines to let the frontend retry.
344+ cursor .Cursor < step .LogLength &&
345+ // !(index < task.LogIndexes[index]) when task data is older than step data.
346+ // It can be fixed by making sure write/read tasks and steps in the same transaction,
347+ // but it's easier to just treat it as fetching the next line before it's ready.
348+ index < int64 (len (task .LogIndexes ))
349+
350+ if validCursor {
351+ length := step .LogLength - cursor .Cursor
352+ offset := task .LogIndexes [index ]
353+ logRows , err := actions .ReadLogs (ctx , task .LogInStorage , task .LogFilename , offset , length )
354+ if err != nil {
355+ return nil , nil , fmt .Errorf ("actions.ReadLogs: %w" , err )
350356 }
351357
352- resp .Logs .StepsLog = append (resp .Logs .StepsLog , & ViewStepLog {
353- Step : cursor .Step ,
354- Cursor : cursor .Cursor + int64 (len (logLines )),
355- Lines : logLines ,
356- Started : int64 (step .Started ),
357- })
358+ for i , row := range logRows {
359+ logLines = append (logLines , & ViewStepLogLine {
360+ Index : cursor .Cursor + int64 (i ) + 1 , // start at 1
361+ Message : row .Content ,
362+ Timestamp : float64 (row .Time .AsTime ().UnixNano ()) / float64 (time .Second ),
363+ })
364+ }
358365 }
366+
367+ logs = append (logs , & ViewStepLog {
368+ Step : cursor .Step ,
369+ Cursor : cursor .Cursor + int64 (len (logLines )),
370+ Lines : logLines ,
371+ Started : int64 (step .Started ),
372+ })
359373 }
360374
361- ctx . JSON ( http . StatusOK , resp )
375+ return viewJobs , logs , nil
362376}
363377
364378// Rerun will rerun jobs in the given run
0 commit comments