Plug framework export

Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
This commit is contained in:
Bryan Frimin
2025-09-04 10:34:21 +02:00
committed by Sacha Al Himdani
parent d47cac911d
commit 9a3c4d3965
9 changed files with 206 additions and 32 deletions

View File

@@ -65,15 +65,13 @@ INSERT INTO framework_exports (
tenant_id,
framework_id,
status,
created_at,
expires_at
created_at
) VALUES (
@id,
@tenant_id,
@framework_id,
@status,
@created_at,
@expires_at
@created_at
)`
args := pgx.StrictNamedArgs{
@@ -130,6 +128,7 @@ SELECT
id,
framework_id,
status,
file_id,
created_at,
started_at,
completed_at

View File

@@ -249,8 +249,6 @@ WHERE %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), filter.SQLFragment(), cursor.SQLFragment())
fmt.Printf("\n%s\n", q)
args := pgx.NamedArgs{"control_id": controlID}
maps.Copy(args, scope.SQLArguments())
maps.Copy(args, filter.SQLArguments())

View File

@@ -1,6 +1,6 @@
CREATE TYPE framework_export_status AS ENUM (
'pending',
'processing',
'processing',
'completed',
'failed'
);
@@ -16,5 +16,14 @@ CREATE TABLE framework_exports (
completed_at TIMESTAMP WITH TIME ZONE
);
ALTER TABLE framework_exports ADD CONSTRAINT framework_exports_framework_id_fkey
FOREIGN KEY (framework_id)
REFERENCES frameworks(id)
ON UPDATE CASCADE
ON DELETE CASCADE;
ALTER TABLE framework_exports ADD CONSTRAINT framework_exports_file_id_fkey
FOREIGN KEY (file_id)
REFERENCES files(id)
ON UPDATE CASCADE
ON DELETE SET NULL;

View File

@@ -71,8 +71,10 @@ type (
func (s FrameworkService) RequestExport(
ctx context.Context,
frameworkID gid.GID,
) error {
return s.svc.pg.WithTx(ctx, func(conn pg.Conn) error {
) (error, *coredata.FrameworkExport) {
frameworkExport := &coredata.FrameworkExport{}
err := s.svc.pg.WithTx(ctx, func(conn pg.Conn) error {
framework := &coredata.Framework{}
if err := framework.LoadByID(ctx, conn, s.svc.scope, frameworkID); err != nil {
return fmt.Errorf("cannot load framework: %w", err)
@@ -80,7 +82,7 @@ func (s FrameworkService) RequestExport(
now := time.Now()
frameworkExport := &coredata.FrameworkExport{
frameworkExport = &coredata.FrameworkExport{
ID: gid.New(framework.ID.TenantID(), coredata.FrameworkExportEntityType),
FrameworkID: frameworkID,
Status: coredata.FrameworkExportStatusPending,
@@ -93,6 +95,12 @@ func (s FrameworkService) RequestExport(
return nil
})
if err != nil {
return err, nil
}
return nil, frameworkExport
}
func (s FrameworkService) Export(

View File

@@ -201,6 +201,13 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
}
scope := coredata.NewScope(fe.ID.TenantID())
fe.Status = coredata.FrameworkExportStatusProcessing
fe.StartedAt = ref.Ref(time.Now())
if err := fe.Update(ctx, tx, scope); err != nil {
return fmt.Errorf("cannot update framework export: %w", err)
}
framework := &coredata.Framework{}
if err := framework.LoadByID(ctx, tx, scope, fe.FrameworkID); err != nil {
fe.Status = coredata.FrameworkExportStatusFailed
@@ -209,9 +216,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err)
}
// s.logger.Error(ctx, "cannot load framework", "error", err)
return nil
return fmt.Errorf("cannot load framework: %w", err)
}
tenantService := s.WithTenant(fe.ID.TenantID())
@@ -225,9 +230,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err)
}
// s.logger.Error(ctx, "cannot create temp file", "error", err)
return nil
return fmt.Errorf("cannot create temp file: %w", err)
}
defer tempFile.Close()
defer os.Remove(tempFile.Name())
@@ -240,8 +243,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err)
}
// s.logger.Error(ctx, "cannot export framework", "error", err)
return nil
return fmt.Errorf("cannot export framework: %w", err)
}
uuid, err := uuid.NewV4()
@@ -252,8 +254,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err)
}
// s.logger.Error(ctx, "cannot generate UUID", "error", err)
return nil
return fmt.Errorf("cannot update framework export: %w", err)
}
if _, err := tempFile.Seek(0, 0); err != nil {
@@ -263,8 +264,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err)
}
// s.logger.Error(ctx, "cannot seek temp file", "error", err)
return nil
return fmt.Errorf("cannot seek temp file: %w", err)
}
fileInfo, err := tempFile.Stat()
@@ -275,8 +275,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err)
}
// s.logger.Error(ctx, "cannot get temp file info", "error", err)
return nil
return fmt.Errorf("cannot stat temp file: %w", err)
}
_, err = s.s3.PutObject(
@@ -300,8 +299,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
return fmt.Errorf("cannot update framework export: %w", err)
}
// s.logger.Error(ctx, "cannot upload file to S3", "error", err)
return nil
return fmt.Errorf("cannot upload file to S3: %w", err)
}
now := time.Now()
@@ -312,6 +310,7 @@ func (s *Service) ExportFrameworkJob(ctx context.Context) error {
MimeType: "application/zip",
FileName: fmt.Sprintf("%s Archive %s.zip", framework.Name, time.Now().Format("2006-01-02")),
FileKey: uuid.String(),
FileSize: int(fileInfo.Size()),
CreatedAt: now,
UpdatedAt: now,
}

View File

@@ -330,9 +330,19 @@ func (impl *Implm) Run(
}
}()
frameworkExporterCtx, stopFrameworkExporter := context.WithCancel(context.Background())
wg.Add(1)
go func() {
defer wg.Done()
if err := impl.runFrameworkExporter(frameworkExporterCtx, proboService, l.Named("framework-exporter")); err != nil {
cancel(fmt.Errorf("framework exporter crashed: %w", err))
}
}()
<-ctx.Done()
stopMailer()
stopFrameworkExporter()
stopApiServer()
wg.Wait()
@@ -342,6 +352,26 @@ func (impl *Implm) Run(
return context.Cause(ctx)
}
func (impl *Implm) runFrameworkExporter(
ctx context.Context,
proboService *probo.Service,
l *log.Logger,
) error {
LOOP:
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(30 * time.Second):
if err := proboService.ExportFrameworkJob(ctx); err != nil {
if !errors.Is(err, coredata.ErrNoFrameworkExportAvailable) {
l.ErrorCtx(ctx, "cannot process framework export", log.Error(err))
}
}
goto LOOP
}
}
func (impl *Implm) runApiServer(
ctx context.Context,
l *log.Logger,

View File

@@ -1680,7 +1680,15 @@ func (r *mutationResolver) GenerateFrameworkStateOfApplicability(ctx context.Con
// ExportFramework is the resolver for the exportFramework field.
func (r *mutationResolver) ExportFramework(ctx context.Context, input types.ExportFrameworkInput) (*types.ExportFrameworkPayload, error) {
panic(fmt.Errorf("not implemented: ExportFramework - exportFramework"))
prb := r.ProboService(ctx, input.FrameworkID.TenantID())
err, exportJobID := prb.Frameworks.RequestExport(ctx, input.FrameworkID)
if err != nil {
return nil, fmt.Errorf("cannot export framework: %w", err)
}
return &types.ExportFrameworkPayload{
ExportJobID: exportJobID.ID,
}, nil
}
// CreateControl is the resolver for the createControl field.
@@ -3963,10 +3971,6 @@ func (r *queryResolver) Node(ctx context.Context, id gid.GID) (types.Node, error
return types.NewVendor(vendor), nil
case coredata.FrameworkEntityType:
if err := prb.Frameworks.Export(ctx, id, nil); err != nil {
panic(fmt.Errorf("cannot export organization frameworks: %w", err))
}
framework, err := prb.Frameworks.Get(ctx, id)
if err != nil {
panic(fmt.Errorf("cannot get framework: %w", err))