view pkg/imports/scheduled.go @ 2549:9bf6b767a56a

client: refactored and improved splitscreen for diagrams To make different diagrams possible, the splitscreen view needed to be decoupled from the cross profiles. Also the style has changed to make it more consistent with the rest of the app. The standard box header is now used and there are collapse and expand animations.
author Markus Kottlaender <markus@intevation.de>
date Fri, 08 Mar 2019 08:50:47 +0100
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
}