cc-backend/tools/nats-manager/main.go

138 lines
2.9 KiB
Go
Raw Permalink Normal View History

// Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg.
// All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"flag"
"fmt"
"log"
2023-09-05 23:26:34 +02:00
"net/http"
"os"
"github.com/ClusterCockpit/cc-backend/internal/scheduler"
"github.com/nats-io/nats.go"
)
func usage() {
log.Printf("Usage: nats-pub [-s server] [-creds file] <subject> <msg>\n")
flag.PrintDefaults()
}
func showUsageAndExit(exitcode int) {
usage()
os.Exit(exitcode)
}
func setupPublisher() {
var urls = flag.String("s", nats.DefaultURL, "The nats server URLs (separated by comma)")
var userCreds = flag.String("creds", "", "User Credentials File")
var showHelp = flag.Bool("h", false, "Show help message")
log.SetFlags(0)
flag.Usage = usage
flag.Parse()
if *showHelp {
showUsageAndExit(0)
}
args := flag.Args()
if len(args) != 2 {
showUsageAndExit(1)
}
fmt.Printf("Hello Nats\n")
// Connect Options.
opts := []nats.Option{nats.Name("NATS Sample Publisher")}
// Use UserCredentials
if *userCreds != "" {
opts = append(opts, nats.UserCredentials(*userCreds))
}
// Connect to NATS
nc, err := nats.Connect(*urls, opts...)
if err != nil {
log.Fatal(err)
}
defer nc.Close()
subj, msg := args[0], []byte(args[1])
nc.Publish(subj, msg)
nc.Flush()
if err := nc.LastError(); err != nil {
log.Fatal(err)
} else {
log.Printf("Published [%s] : '%s'\n", subj, msg)
}
os.Exit(0)
}
2023-09-05 23:26:34 +02:00
func injectPayload() {
// Read the JSON file
2023-09-26 02:58:34 +02:00
jobsData, err := os.ReadFile("slurm_0038.json")
dbData, err := os.ReadFile("slurmdb_0038-large.json")
2023-09-05 23:26:34 +02:00
if err != nil {
fmt.Println("Error reading JSON file:", err)
return
}
// Create an HTTP handler function
http.HandleFunc("/slurm/v0.0.38/jobs", func(w http.ResponseWriter, r *http.Request) {
// Set the response content type to JSON
w.Header().Set("Content-Type", "application/json")
// Write the raw JSON data to the response writer
_, err := w.Write(jobsData)
if err != nil {
http.Error(w, "Error writing jobsData payload", http.StatusInternalServerError)
return
}
})
http.HandleFunc("/slurmdb/v0.0.38/jobs", func(w http.ResponseWriter, r *http.Request) {
// Set the response content type to JSON
w.Header().Set("Content-Type", "application/json")
// Write the raw JSON data to the response writer
_, err := w.Write(dbData)
if err != nil {
http.Error(w, "Error writing dbData payload", http.StatusInternalServerError)
return
}
})
// Start the HTTP server on port 8080
fmt.Println("Listening on :8080...")
2023-10-25 23:27:06 +02:00
go http.ListenAndServe(":8080", nil)
2023-09-05 23:26:34 +02:00
}
2023-10-25 23:27:06 +02:00
func loadSlurmNatsScheduler() {
cfgData := []byte(`{"target": "localhost"}`)
var sch scheduler.SlurmNatsScheduler
// sch.URL = "nats://127.0.0.1:1223"
sch.Init(cfgData)
2023-09-05 23:26:34 +02:00
// go injectPayload()
2023-10-25 23:27:06 +02:00
}
func main() {
var sch scheduler.SlurmRestSchedulerConfig
sch.Init()
// injectPayload()
sch.Sync()
os.Exit(0)
}