view pkg/imports/scheduled.go @ 5591:0011f50cf216 surveysperbottleneckid

Removed no longer used alternative api for surveys/ endpoint. As bottlenecks in the summary for SR imports are now identified by their id and no longer by the (not guarantied to be unique!) name, there is no longer the need to request survey data by the name+date tuple (which isn't reliable anyway). So the workaround was now reversed.
author Sascha Wilde <wilde@sha-bang.de>
date Wed, 06 Apr 2022 13:30:29 +0200
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
}