view pkg/imports/scheduled.go @ 4306:2336f75637d1

sections: Use blown up axis as geometry. (Nicked from current stretches) As using the waterway area geometry just imposes another sources of errors (and failed on me repeatedly) this change eliminates these potential problems.
author Sascha Wilde <wilde@intevation.de>
date Mon, 02 Sep 2019 18:26:12 +0200
parents 78ec61acf72e
children 5f47eeea988d
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
}