feat(app): stats added in to check if build in last build and also if theres a pending file
This commit is contained in:
@@ -3,3 +3,10 @@
|
||||
APP_MODE=dev
|
||||
# The port is only needed if you plan to use a different port but you will need to change it in the wrapper too
|
||||
PORT=8080
|
||||
|
||||
# postgres connection
|
||||
DATABASE_HOST=localhost
|
||||
DATABASE_PORT=5432
|
||||
DATABASE_USER=postgres
|
||||
DATABASE_PASSWORD=password
|
||||
DATABASE_DB=lst
|
||||
|
||||
@@ -5,22 +5,29 @@ import (
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"lst.net/pkg"
|
||||
)
|
||||
|
||||
// ---- Handle Build Counter ----
|
||||
func bumpBuild() (int, error) {
|
||||
data, err := os.ReadFile("../.build")
|
||||
buildNum := 0
|
||||
if err == nil { // if file exists, parse current number
|
||||
num, err := strconv.Atoi(strings.TrimSpace(string(data)))
|
||||
if err == nil {
|
||||
buildNum = num
|
||||
}
|
||||
}
|
||||
buildNum++
|
||||
err = os.WriteFile("../.build", []byte(fmt.Sprintf("%d", buildNum)), 0644)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return buildNum, nil
|
||||
}
|
||||
data, err := os.ReadFile("../.build")
|
||||
buildNum := 0
|
||||
if err == nil { // if file exists, parse current number
|
||||
num, err := strconv.Atoi(strings.TrimSpace(string(data)))
|
||||
if err == nil {
|
||||
buildNum = num
|
||||
}
|
||||
}
|
||||
buildNum++
|
||||
err = os.WriteFile("../.build", []byte(fmt.Sprintf("%d", buildNum)), 0644)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
// update the db so we have the build number in here going forward.
|
||||
|
||||
pkg.UpdateServerStats(int64(buildNum))
|
||||
|
||||
return buildNum, nil
|
||||
}
|
||||
|
||||
@@ -27,6 +27,9 @@ require (
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/gorilla/websocket v1.4.2 // indirect
|
||||
github.com/hirochachacha/go-smb2 v1.1.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/pgx/v5 v5.7.6 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.7 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
|
||||
@@ -43,6 +43,12 @@ github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0U
|
||||
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/hirochachacha/go-smb2 v1.1.0 h1:b6hs9qKIql9eVXAiN0M2wSFY5xnhbHAQoCwRKbaRTZI=
|
||||
github.com/hirochachacha/go-smb2 v1.1.0/go.mod h1:8F1A4d5EZzrGu5R7PU163UcMRDJQl4FtcxjBfsY8TZE=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
|
||||
github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
|
||||
48
controller/pkg/update_server_stats.go
Normal file
48
controller/pkg/update_server_stats.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func UpdateServerStats(buildNumber int64) {
|
||||
|
||||
//url := "postgres://username:password@localhost:5432/database_name"
|
||||
|
||||
url := fmt.Sprintf(
|
||||
"postgres://%v:%v@%v:%v/%v",
|
||||
os.Getenv("DATABASE_USER"),
|
||||
os.Getenv("DATABASE_PASSWORD"),
|
||||
os.Getenv("DATABASE_HOST"),
|
||||
os.Getenv("DATABASE_PORT"),
|
||||
os.Getenv("DATABASE_DB"),
|
||||
)
|
||||
|
||||
ctx := context.Background()
|
||||
conn, err := pgx.Connect(ctx, url)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
||||
//os.Exit(1)
|
||||
}
|
||||
defer conn.Close(ctx)
|
||||
|
||||
sql := `
|
||||
INSERT INTO public."serverStats" (id, build, "lastUpdate")
|
||||
VALUES ($1, $2, NOW())
|
||||
ON CONFLICT (id) DO UPDATE
|
||||
SET build = EXCLUDED.build,
|
||||
"lastUpdate" = NOW();
|
||||
`
|
||||
|
||||
_, err = conn.Exec(ctx, sql, "serverStats", buildNumber)
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
//os.Exit(1)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -15,6 +15,7 @@ import (
|
||||
"time"
|
||||
|
||||
socketio "github.com/googollee/go-socket.io"
|
||||
"lst.net/pkg"
|
||||
)
|
||||
|
||||
func UpdateApp(server *socketio.Server) <-chan string {
|
||||
@@ -54,6 +55,7 @@ func UpdateApp(server *socketio.Server) <-chan string {
|
||||
updates <- msg
|
||||
server.BroadcastToRoom("/", "update", "updateLogs", msg)
|
||||
}
|
||||
|
||||
msg := "Stopping the services"
|
||||
updates <- msg
|
||||
server.BroadcastToRoom("/", "update", "updateLogs", msg)
|
||||
@@ -127,6 +129,16 @@ func UpdateApp(server *socketio.Server) <-chan string {
|
||||
msg = "Deployment finished successfully"
|
||||
updates <- msg
|
||||
|
||||
// remove the prefix
|
||||
cleaned := strings.Replace(strconv.Itoa(version), "release-", "", 1)
|
||||
|
||||
num, err := strconv.Atoi(cleaned)
|
||||
|
||||
if err != nil {
|
||||
fmt.Println("Error parsing number:", err)
|
||||
}
|
||||
pkg.UpdateServerStats(int64(num))
|
||||
|
||||
server.BroadcastToRoom("/", "update", "updateLogs", msg)
|
||||
default:
|
||||
msg := fmt.Sprintf("Error: too many zip files in root: %v\n", zips)
|
||||
@@ -194,6 +206,7 @@ func Unzip(srcZip, destDir string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user