@@ -114,7 +114,7 @@ func NewImageScanServiceImpl(logger *zap.SugaredLogger, scanHistoryRepository re
114114 // Only check for progressing scans if the flag is enabled
115115 if imageScanConfig .EnableProgressingScanCheck {
116116 logger .Infow ("checking for progressing scans at startup" )
117- imageScanService .HandleProgressingScans ()
117+ go imageScanService .HandleProgressingScans ()
118118 } else {
119119 logger .Infow ("skipping progressing scans check at startup as it is disabled" )
120120 }
@@ -961,63 +961,63 @@ func (impl *ImageScanServiceImpl) HandleProgressingScans() {
961961 impl .Logger .Errorw ("error in getting all scans by running state" , "err" , err )
962962 return
963963 }
964-
965- var executionHistoryDirPath string
966- flagForDeleting := false
967- // Create Folder for output data for execution history only if any pending scans are there due to pod died
968964 if len (scanHistories ) > 0 {
969- flagForDeleting = true
965+ var executionHistoryDirPath string
966+ // Create Folder for output data for execution history only if any pending scans are there due to pod died
970967 executionHistoryDirPath = impl .CreateFolderForOutputData (scanHistories [0 ].ImageScanExecutionHistoryId )
971- }
972- wg := & sync.WaitGroup {}
973- wg .Add (len (scanHistories ))
974- imagescanExecutionHistories , err := impl .ScanHistoryRepository .FindAll ()
975- if err != nil {
976- impl .Logger .Errorw ("error in getting scan histories on start up" , "err" , err )
977- return
978- }
979- imageScanToolMetadatas , err := impl .ScanToolMetadataRepository .FindAllActiveTools ()
980- if err != nil {
981- impl .Logger .Errorw ("error in getting all active tools" , "err" , err )
982- }
983- imageScanExecutionHistoryMap := make (map [int ]* repository.ImageScanExecutionHistory )
984- imageScanToolsMap := make (map [int ]* repository.ScanToolMetadata )
985-
986- for _ , imageScanExecutionHistory := range imagescanExecutionHistories {
987- imageScanExecutionHistoryMap [imageScanExecutionHistory .Id ] = imageScanExecutionHistory
988- }
989- for _ , imageScanToolMetaData := range imageScanToolMetadatas {
990- imageScanToolsMap [imageScanToolMetaData .Id ] = imageScanToolMetaData
991- }
992-
993- //System doing image scanning for all pending scans
994- for _ , scanHistory := range scanHistories {
995- scanEvent := bean2.ImageScanEvent {}
996- scanEventJson := imageScanExecutionHistoryMap [scanHistory .ImageScanExecutionHistoryId ].SourceMetadataJson
997- if len (scanEventJson ) == 0 {
998- return
968+ imageScanExecutionHistoryIds := make ([]int , 0 , len (scanHistories ))
969+ for _ , scanHistory := range scanHistories {
970+ imageScanExecutionHistoryIds = append (imageScanExecutionHistoryIds , scanHistory .ImageScanExecutionHistoryId )
999971 }
1000- scanTool := imageScanToolsMap [scanHistory .ScanToolId ]
1001- err = json .Unmarshal ([]byte (scanEventJson ), & scanEvent )
972+ wg := & sync.WaitGroup {}
973+ wg .Add (len (scanHistories ))
974+ imagescanExecutionHistories , err := impl .ScanHistoryRepository .FindByIds (imageScanExecutionHistoryIds )
1002975 if err != nil {
1003- impl .Logger .Errorw ("error in un-marshaling " , "err" , err )
976+ impl .Logger .Errorw ("error in getting scan histories on start up " , "err" , err )
1004977 return
1005978 }
1006- imageScanRenderDto , err := impl .GetImageScanRenderDto ( scanEvent . DockerRegistryId , & scanEvent )
979+ imageScanToolMetadatas , err := impl .ScanToolMetadataRepository . FindAllActiveTools ( )
1007980 if err != nil {
1008- impl .Logger .Errorw ("service error, GetImageScanRenderDto" , "dockerRegistryId" , scanEvent . DockerRegistryId , "err" , err )
981+ impl .Logger .Errorw ("error in getting all active tools" , "err" , err )
1009982 return
1010983 }
1011- _ , _ , err = impl .ScanImageForTool (scanTool , scanHistory .ImageScanExecutionHistoryId , executionHistoryDirPath , wg , 1 , context .Background (), imageScanRenderDto )
1012- if err != nil {
1013- impl .Logger .Errorw ("error in scanning image" , "err" , err )
1014- return
984+ imageScanExecutionHistoryMap := make (map [int ]* repository.ImageScanExecutionHistory )
985+ imageScanToolsMap := make (map [int ]* repository.ScanToolMetadata )
986+
987+ for _ , imageScanExecutionHistory := range imagescanExecutionHistories {
988+ imageScanExecutionHistoryMap [imageScanExecutionHistory .Id ] = imageScanExecutionHistory
1015989 }
1016- }
1017- wg .Wait ()
990+ for _ , imageScanToolMetaData := range imageScanToolMetadatas {
991+ imageScanToolsMap [imageScanToolMetaData .Id ] = imageScanToolMetaData
992+ }
993+
994+ //System doing image scanning for all pending scans
995+ for _ , scanHistory := range scanHistories {
996+ scanEvent := bean2.ImageScanEvent {}
997+ scanEventJson := imageScanExecutionHistoryMap [scanHistory .ImageScanExecutionHistoryId ].SourceMetadataJson
998+ if len (scanEventJson ) == 0 {
999+ return
1000+ }
1001+ scanTool := imageScanToolsMap [scanHistory .ScanToolId ]
1002+ err = json .Unmarshal ([]byte (scanEventJson ), & scanEvent )
1003+ if err != nil {
1004+ impl .Logger .Errorw ("error in un-marshaling" , "err" , err )
1005+ return
1006+ }
1007+ imageScanRenderDto , err := impl .GetImageScanRenderDto (scanEvent .DockerRegistryId , & scanEvent )
1008+ if err != nil {
1009+ impl .Logger .Errorw ("service error, GetImageScanRenderDto" , "dockerRegistryId" , scanEvent .DockerRegistryId , "err" , err )
1010+ return
1011+ }
1012+ _ , _ , err = impl .ScanImageForTool (scanTool , scanHistory .ImageScanExecutionHistoryId , executionHistoryDirPath , wg , 1 , context .Background (), imageScanRenderDto )
1013+ if err != nil {
1014+ impl .Logger .Errorw ("error in scanning image" , "err" , err )
1015+ return
1016+ }
1017+ }
1018+ wg .Wait ()
10181019
1019- //deleting executionDirectoryPath
1020- if flagForDeleting {
1020+ //deleting executionDirectoryPath
10211021 err = os .Remove (executionHistoryDirPath )
10221022 if err != nil {
10231023 impl .Logger .Errorw ("error in deleting executionHistoryDirectory" , "executionHistoryDirPath" , executionHistoryDirPath , "err" , err )
0 commit comments