@@ -1437,43 +1437,63 @@ func (s *server) CompleteJob(ctx context.Context, completed *proto.CompletedJob)
1437
1437
// If there is a plan, or a module files archive we need to insert a
1438
1438
// template_version_terraform_values row.
1439
1439
if len (plan )> 0 || len (moduleFiles )> 0 {
1440
- // ...but the plan and the module files archive are both optional! So
1441
- // we need to fallback to a valid JSON object if the plan was omitted.
1442
- if len (plan )== 0 {
1443
- plan = []byte ("{}" )
1444
- }
1445
-
1446
- // ...and we only want to insert a files row if an archive was provided.
1447
- var fileID uuid.NullUUID
1448
- if len (moduleFiles )> 0 {
1449
- hashBytes := sha256 .Sum256 (moduleFiles )
1450
- hash := hex .EncodeToString (hashBytes [:])
1451
- // nolint:gocritic // Requires system privileges
1452
- file ,err := s .Database .InsertFile (dbauthz .AsSystemRestricted (ctx ), database.InsertFileParams {
1453
- ID :uuid .New (),
1454
- Hash :hash ,
1455
- CreatedBy :uuid .Nil ,// TODO
1456
- CreatedAt :dbtime .Now (),
1457
- Mimetype :tarMimeType ,
1458
- Data :moduleFiles ,
1440
+ err := s .Database .InTx (func (tx database.Store )error {
1441
+ // ...but the plan and the module files archive are both optional! So
1442
+ // we need to fallback to a valid JSON object if the plan was omitted.
1443
+ if len (plan )== 0 {
1444
+ plan = []byte ("{}" )
1445
+ }
1446
+
1447
+ // ...and we only want to insert a files row if an archive was provided.
1448
+ var fileID uuid.NullUUID
1449
+ if len (moduleFiles )> 0 {
1450
+ hashBytes := sha256 .Sum256 (moduleFiles )
1451
+ hash := hex .EncodeToString (hashBytes [:])
1452
+
1453
+ // nolint:gocritic // Requires reading "system" files
1454
+ file ,err := s .Database .GetFileByHashAndCreator (dbauthz .AsSystemRestricted (ctx ), database.GetFileByHashAndCreatorParams {Hash :hash ,CreatedBy :uuid .Nil })
1455
+ if err == nil {
1456
+ // This set of modules is already cached, which means we can reuse them
1457
+ fileID = uuid.NullUUID {
1458
+ Valid :true ,
1459
+ UUID :file .ID ,
1460
+ }
1461
+ }else if ! xerrors .Is (err ,sql .ErrNoRows ) {
1462
+ return xerrors .Errorf ("check for cached modules: %w" ,err )
1463
+ }else {
1464
+ // nolint:gocritic // Requires creating a "system" file
1465
+ file ,err = s .Database .InsertFile (dbauthz .AsSystemRestricted (ctx ), database.InsertFileParams {
1466
+ ID :uuid .New (),
1467
+ Hash :hash ,
1468
+ CreatedBy :uuid .Nil ,
1469
+ CreatedAt :dbtime .Now (),
1470
+ Mimetype :tarMimeType ,
1471
+ Data :moduleFiles ,
1472
+ })
1473
+ if err != nil {
1474
+ return xerrors .Errorf ("insert template version terraform modules: %w" ,err )
1475
+ }
1476
+ fileID = uuid.NullUUID {
1477
+ Valid :true ,
1478
+ UUID :file .ID ,
1479
+ }
1480
+ }
1481
+ }
1482
+
1483
+ err = s .Database .InsertTemplateVersionTerraformValuesByJobID (ctx , database.InsertTemplateVersionTerraformValuesByJobIDParams {
1484
+ JobID :jobID ,
1485
+ UpdatedAt :now ,
1486
+ CachedPlan :plan ,
1487
+ CachedModuleFiles :fileID ,
1459
1488
})
1460
1489
if err != nil {
1461
- return nil ,xerrors .Errorf ("insert template version terraform modules: %w" ,err )
1462
- }
1463
- fileID = uuid.NullUUID {
1464
- Valid :true ,
1465
- UUID :file .ID ,
1490
+ return xerrors .Errorf ("insert template version terraform data: %w" ,err )
1466
1491
}
1467
- }
1468
1492
1469
- err = s .Database .InsertTemplateVersionTerraformValuesByJobID (ctx , database.InsertTemplateVersionTerraformValuesByJobIDParams {
1470
- JobID :jobID ,
1471
- UpdatedAt :now ,
1472
- CachedPlan :plan ,
1473
- CachedModuleFiles :fileID ,
1474
- })
1493
+ return nil
1494
+ },nil )
1475
1495
if err != nil {
1476
- return nil ,xerrors . Errorf ( "insert template version terraform data: %w" , err )
1496
+ return nil ,err
1477
1497
}
1478
1498
}
1479
1499