view pkg/imports/scheduled.go @ 5560:f2204f91d286

Join the log lines of imports to the log exports to recover data from them. Used in SR export to extract information that where in the meta json but now are only found in the log.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Wed, 09 Feb 2022 18:34:40 +0100
parents 5f47eeea988d
children
line wrap: on
line source

// This is Free Software under GNU Affero General Public License v >= 3.0
// without warranty, see README.md and license for details.
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// License-Filename: LICENSES/AGPL-3.0.txt
//
// Copyright (C) 2018 by via donau
//   – Österreichische Wasserstraßen-Gesellschaft mbH
// Software engineering by Intevation GmbH
//
// Author(s):
//  * Sascha L. Teichmann <sascha.teichmann@intevation.de>

package imports

import (
	"context"
	"database/sql"
	"fmt"
	"time"

	"gemma.intevation.de/gemma/pkg/common"
	"gemma.intevation.de/gemma/pkg/log"
	"gemma.intevation.de/gemma/pkg/models"
	"gemma.intevation.de/gemma/pkg/scheduler"
)

func init() {
	run := func(cfgID int64) {
		jobID, err := RunConfiguredImport(cfgID)
		if err != nil {
			log.Errorf("running scheduled import failed: %v\n", err)
			return
		}
		log.Infof("added import #%d to queue\n", jobID)
	}

	for kind := range kindToImportModel {
		scheduler.RegisterAction(string(kind), run)
	}
}

// RunConfiguredImportContext runs an import configured from the database.
func RunConfiguredImportContext(ctx context.Context, conn *sql.Conn, id int64) (int64, error) {
	cfg, err := LoadPersistentConfigContext(ctx, conn, id)
	return runConfiguredImport(id, cfg, err)
}

// RunConfiguredImport runs an import configured from the database.
func RunConfiguredImport(id int64) (int64, error) {
	cfg, err := loadPersistentConfig(id)
	return runConfiguredImport(id, cfg, err)
}

func runConfiguredImport(id int64, cfg *PersistentConfig, err error) (int64, error) {

	if err != nil {
		return 0, err
	}
	if cfg == nil {
		return 0, fmt.Errorf("no config found for id %d", id)
	}

	kind := JobKind(cfg.Kind)

	ctor := ImportModelForJobKind(kind)
	if ctor == nil {
		return 0, fmt.Errorf("no constructor for kind '%s'", cfg.Kind)
	}

	what := ctor()

	// Fill the data structure
	if err := cfg.Attributes.Unmarshal(what); err != nil {
		return 0, err
	}

	converted := ConvertToInternal(kind, what)
	if converted == nil {
		return 0, fmt.Errorf("conversion of model for kind '%s' failed", kind)
	}

	var serialized string
	if serialized, err = common.ToJSONString(converted); err != nil {
		return 0, err
	}

	// Extract the job runtime parameters.
	var (
		email     bool
		due       time.Time
		trys      *int
		waitRetry *time.Duration
	)

	if gqc, ok := what.(models.QueueConfigurationGetter); ok {
		qc := gqc.GetQueueConfiguration()
		if qc.Due != nil {
			due = qc.Due.Time
		}
		trys = qc.Trys
		if qc.WaitRetry != nil {
			waitRetry = &qc.WaitRetry.Duration
		}
	}

	if ge, ok := what.(models.EmailTypeGetter); ok {
		email = ge.GetEmailType().Email
	}

	var jobID int64
	if jobID, err = AddJob(
		kind,
		due,
		trys,
		waitRetry,
		cfg.User,
		email,
		serialized,
	); err != nil {
		return 0, err
	}

	return jobID, nil
}