@@ -902,12 +902,7 @@ func (s *server) UpdateJob(ctx context.Context, request *proto.UpdateJobRequest)
902
902
return nil , xerrors .Errorf ("update job: %w" , err )
903
903
}
904
904
905
- if len (request .Logs ) > 0 {
906
- if job .LogsOverflowed {
907
- return & proto.UpdateJobResponse {
908
- Canceled : job .CanceledAt .Valid ,
909
- }, nil
910
- }
905
+ if len (request .Logs ) > 0 && ! job .LogsOverflowed {
911
906
912
907
//nolint:exhaustruct // We append to the additional fields below.
913
908
insertParams := database.InsertProvisionerJobLogsParams {
@@ -938,11 +933,28 @@ func (s *server) UpdateJob(ctx context.Context, request *proto.UpdateJobRequest)
938
933
newLogSize += len (log .Output )
939
934
}
940
935
936
+ willOverflow := int64 (job .LogsLength )+ int64 (newLogSize ) > 1048576
937
+ if willOverflow {
938
+ err = s .Database .UpdateProvisionerJobLogsOverflowed (ctx , database.UpdateProvisionerJobLogsOverflowedParams {
939
+ ID : parsedID ,
940
+ LogsOverflowed : true ,
941
+ })
942
+ if err != nil {
943
+ s .Logger .Error (ctx , "failed to set logs overflowed flag" , slog .F ("job_id" , parsedID ), slog .Error (err ))
944
+ }
945
+ return & proto.UpdateJobResponse {
946
+ Canceled : job .CanceledAt .Valid ,
947
+ }, nil
948
+ }
949
+
941
950
err = s .Database .UpdateProvisionerJobLogsLength (ctx , database.UpdateProvisionerJobLogsLengthParams {
942
951
ID : parsedID ,
943
952
LogsLength : int32 (newLogSize ), // #nosec G115 - Log output length is limited to 1MB (2^20) which fits in an int32.
944
953
})
945
954
if err != nil {
955
+
956
+ // Even though we do the runtime check for the overflow, we still check for the database error
957
+ // as well.
946
958
if database .IsProvisionerJobLogsLimitError (err ) {
947
959
err = s .Database .UpdateProvisionerJobLogsOverflowed (ctx , database.UpdateProvisionerJobLogsOverflowedParams {
948
960
ID : parsedID ,
@@ -964,6 +976,7 @@ func (s *server) UpdateJob(ctx context.Context, request *proto.UpdateJobRequest)
964
976
s .Logger .Error (ctx , "failed to insert job logs" , slog .F ("job_id" , parsedID ), slog .Error (err ))
965
977
return nil , xerrors .Errorf ("insert job logs: %w" , err )
966
978
}
979
+
967
980
// Publish by the lowest log ID inserted so the log stream will fetch
968
981
// everything from that point.
969
982
lowestID := logs [0 ].ID
0 commit comments