view pkg/scheduler/scheduler.go @ 1540:251ee25accce

Droped on scheduler in favor for a third party library.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Fri, 07 Dec 2018 18:34:53 +0100
parents
children 03fcad10104a
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 scheduler

import (
	"errors"
	"sync"

	"github.com/robfig/cron"
)

var ErrNoSuchAction = errors.New("No such action")

type funcJob struct {
	bound bool
	fn    func()
}

type scheduler struct {
	cr      *cron.Cron
	actions map[string]*funcJob
	mu      sync.Mutex
}

var global = scheduler{
	cr:      cron.New(),
	actions: make(map[string]*funcJob),
}

func AddAction(name string, fn func()) {
	global.addAction(name, fn)
}

func AddSchedule(spec, action string) error {
	return global.addAction(spec, action)
}

func (s *scheduler) addSchedule(spec, action string) error {
	s.mu.Lock()
	defer s.mu.Unlock()
	if s.actions[action] == nil {
		return ErrNoSuchAction
	}
	// TODO: Implement me!
}

func (s *scheduler) addAction(name string, fn func()) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.actions[name] = &funcJob{fn: fn}
}