66 "flag"
77 "log/slog"
88 "os"
9+ "os/signal"
910 "path/filepath"
11+ "syscall"
1012 "time"
1113
1214 "cloud.google.com/go/datastore"
@@ -22,7 +24,8 @@ import (
2224func main () {
2325 logger .InitGlobalLogger ()
2426
25- strictValidation := flag .Bool ("strict-validation" , false , "Fail to import entries that do not pass validation." )
27+ strictValidation := flag .Bool ("strict-validation" , false , "Fail to import entries that do not pass validation. " +
28+ "Note: this only applies to SourceRepositories with strict_validation=true" )
2629 runDelete := flag .Bool ("delete" , false , "Bypass importing and propagate record deletions from source to Datastore" )
2730 deleteThresholdPct := flag .Float64 ("delete-threshold-pct" , 10.0 , "More than this percent of records for a given source being deleted triggers an error" )
2831 workDir := flag .String ("work-dir" , "/work" , "Work directory for git repos" )
@@ -49,32 +52,35 @@ func main() {
4952 httpClient .Logger = importer.RetryableHTTPLeveledLogger {}
5053 config .HTTPClient = httpClient .StandardClient ()
5154
52- datastoreClient , err := datastore .NewClient (context .Background (), project )
55+ ctx , stop := signal .NotifyContext (context .Background (), syscall .SIGINT , syscall .SIGTERM )
56+ defer stop ()
57+
58+ datastoreClient , err := datastore .NewClient (ctx , project )
5359 if err != nil {
5460 logger .Fatal ("Failed to create datastore client" , slog .Any ("error" , err ))
5561 }
5662 config .SourceRepoStore = db .NewSourceRepositoryStore (datastoreClient )
5763 // Needed for deletions only
5864 config .VulnerabilityStore = db .NewVulnerabilityStore (datastoreClient )
5965
60- psClient , err := pubsub .NewClient (context . Background () , project )
66+ psClient , err := pubsub .NewClient (ctx , project )
6167 if err != nil {
6268 logger .Fatal ("Failed to create pubsub client" , slog .Any ("error" , err ))
6369 }
6470 config .Publisher = & clients.GCPPublisher {Publisher : psClient .Publisher (importer .TasksTopic )}
6571
66- storageClient , err := storage .NewClient (context . Background () )
72+ storageClient , err := storage .NewClient (ctx )
6773 if err != nil {
6874 logger .Fatal ("Failed to create GCS client" , slog .Any ("error" , err ))
6975 }
7076 config .GCSProvider = clients .NewGCSStorageProvider (storageClient )
7177
7278 if * runDelete {
73- if err := importer .RunDeletions (context . Background () , config ); err != nil {
79+ if err := importer .RunDeletions (ctx , config ); err != nil {
7480 logger .Fatal ("Importer-deleter failed" , slog .Any ("error" , err ))
7581 }
7682 } else {
77- if err := importer .Run (context . Background () , config ); err != nil {
83+ if err := importer .Run (ctx , config ); err != nil {
7884 logger .Fatal ("Importer failed" , slog .Any ("error" , err ))
7985 }
8086 }
0 commit comments