Skip to content

armgit / posta

Public Imported from GitHub
0

Self-hosted email delivery platform that enables applications to send emails via HTTP APIs while handling SMTP delivery, templates, storage, security, and analytics. Fully self-hostable alternative to services like SendGrid or Mailgun.

2.5 KB
Raw Blame
// Copyright (c) 2026 Jonas Kaninda.
//
// This file is part of Posta.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package workermon

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"strings"
	"time"

	"github.com/redis/go-redis/v9"
)

const (
	heartbeatInterval = 15 * time.Second
	heartbeatTTL      = 45 * time.Second
	heartbeatPrefix   = "posta:worker:"
)

type WorkerHeartbeat struct {
	Version  string `json:"version"`
	CommitID string `json:"commit_id"`
}

func heartbeatKey(host string, pid int) string {
	return fmt.Sprintf("%s%s:%d", heartbeatPrefix, host, pid)
}

func StartHeartbeat(ctx context.Context, rdb *redis.Client, version, commitID string) {
	if rdb == nil {
		return
	}
	host, _ := os.Hostname()
	key := heartbeatKey(host, os.Getpid())
	payload, _ := json.Marshal(WorkerHeartbeat{Version: version, CommitID: commitID})

	set := func() {
		c, cancel := context.WithTimeout(context.Background(), 3*time.Second)
		defer cancel()
		_ = rdb.Set(c, key, payload, heartbeatTTL).Err()
	}

	set()
	go func() {
		t := time.NewTicker(heartbeatInterval)
		defer t.Stop()
		for {
			select {
			case <-ctx.Done():
				c, cancel := context.WithTimeout(context.Background(), 3*time.Second)
				_ = rdb.Del(c, key).Err()
				cancel()
				return
			case <-t.C:
				set()
			}
		}
	}()
}

func ReadHeartbeats(ctx context.Context, rdb *redis.Client) map[string]WorkerHeartbeat {
	out := map[string]WorkerHeartbeat{}
	if rdb == nil {
		return out
	}
	var cursor uint64
	for {
		keys, next, err := rdb.Scan(ctx, cursor, heartbeatPrefix+"*", 100).Result()
		if err != nil {
			return out
		}
		for _, k := range keys {
			val, err := rdb.Get(ctx, k).Result()
			if err != nil {
				continue
			}
			var hb WorkerHeartbeat
			if json.Unmarshal([]byte(val), &hb) == nil {
				out[strings.TrimPrefix(k, heartbeatPrefix)] = hb
			}
		}
		if next == 0 {
			break
		}
		cursor = next
	}
	return out
}