view pkg/imports/scheduled.go @ 3678:8f58851927c0

client: make layer factory only return new layer config for individual maps instead of each time it is invoked. The purpose of the factory was to support multiple maps with individual layers. But returning a new config each time it is invoked leads to bugs that rely on the layer's state. Now this factory reuses the same objects it created before, per map.
author Markus Kottlaender <markus@intevation.de>
date Mon, 17 Jun 2019 17:31:35 +0200
parents 78002c5e838c
children 78ec61acf72e
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"
	"log"
	"time"

	"gemma.intevation.de/gemma/pkg/common"
	"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.Printf("error: running scheduled import failed: %v\n", err)
			return
		}
		log.Printf("info: 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
}