Medication Update
This commit is contained in:
+15
-19
@@ -3,7 +3,6 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
@@ -13,12 +12,13 @@ import (
|
||||
"service/internal/infrastructure/cache"
|
||||
"service/internal/infrastructure/config"
|
||||
"service/internal/infrastructure/database"
|
||||
grpcServers "service/internal/infrastructure/transport/grpc/servers"
|
||||
httpServer "service/internal/infrastructure/transport/http/servers"
|
||||
"service/internal/interfaces/minio"
|
||||
|
||||
"service/internal/interfaces/satusehat"
|
||||
"service/internal/worker"
|
||||
"service/pkg/logger"
|
||||
|
||||
imagingStudyWorker "service/internal/worker/satusehat/imagingstudy"
|
||||
// roleComponent "service/internal/master/role/component"
|
||||
roleMaster "service/internal/master/role/master"
|
||||
rolePages "service/internal/master/role/pages"
|
||||
@@ -26,8 +26,6 @@ import (
|
||||
|
||||
_ "service/docs/swagger" // Wajib: import swagger docs yang di-generate oleh swag CLI
|
||||
|
||||
grpcServers "service/internal/infrastructure/transport/grpc/servers"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
@@ -159,20 +157,6 @@ func main() {
|
||||
g, ctx := errgroup.WithContext(ctx)
|
||||
|
||||
// --- [NEW] Start Background Workers ---
|
||||
if cfg.SatuSehat.Enabled {
|
||||
imagingStudyWorkerCfg := imagingStudyWorker.Config{
|
||||
DBManager: dbService,
|
||||
InternalBaseURL: fmt.Sprintf("http://localhost:%d%s", cfg.Server.REST.Port, cfg.Swagger.BasePath), // otomatis: http://localhost:8080/api/v1
|
||||
InternalToken: "", // Isi token di sini jika endpoint internal Usecase Anda membutuhkan Bearer Token
|
||||
OrganizationID: cfg.SatuSehat.OrgID,
|
||||
}
|
||||
isWorker := imagingStudyWorker.NewWorker(imagingStudyWorkerCfg)
|
||||
|
||||
g.Go(func() error {
|
||||
isWorker.Run(ctx)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
serverCount := 0
|
||||
|
||||
@@ -244,6 +228,18 @@ func main() {
|
||||
logger.Default().Fatal("No server enabled (REST and gRPC are both disabled in config)")
|
||||
}
|
||||
|
||||
// --- [NEW] Start Background Workers ---
|
||||
if cfg.SatuSehat.Enabled {
|
||||
// Inisialisasi Worker Manager yang akan mengelola semua background workers.
|
||||
// satusehatClient bisa di-pass jika diperlukan oleh worker lain.
|
||||
var satusehatClient satusehat.SatuSehatClient // Ganti nil dengan inisialisasi client jika sudah ada
|
||||
workerManager := worker.NewManager(cfg, dbService, satusehatClient)
|
||||
|
||||
g.Go(func() error {
|
||||
workerManager.Start(ctx)
|
||||
return nil
|
||||
})
|
||||
}
|
||||
// --- [C] Graceful Shutdown Listener ---
|
||||
g.Go(func() error {
|
||||
quit := make(chan os.Signal, 1)
|
||||
|
||||
@@ -1,83 +0,0 @@
|
||||
// File: /home/meninjar/goprint/service/cmd/api/main.go (Update)
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
|
||||
"service/internal/infrastructure/cache"
|
||||
"service/internal/infrastructure/config"
|
||||
"service/internal/infrastructure/database"
|
||||
"service/internal/infrastructure/monitoring"
|
||||
"service/pkg/logger"
|
||||
|
||||
// Inisialisasi master/reference Ethnic Handler
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// ... existing config loading ...
|
||||
cfg := config.LoadConfig()
|
||||
// 2. Init Logger dengan konfigurasi dari config
|
||||
loggerConfig := logger.Config{
|
||||
Level: cfg.Logger.Level,
|
||||
Format: cfg.Logger.Format,
|
||||
Output: "console",
|
||||
ServiceName: "service-general",
|
||||
Environment: cfg.Server.Mode,
|
||||
}
|
||||
logger.Init(loggerConfig)
|
||||
|
||||
// Initialize Prometheus Metrics
|
||||
metrics := monitoring.NewMetrics("goprint-service")
|
||||
|
||||
// 2. Init Database
|
||||
dbService := database.New(cfg)
|
||||
defer dbService.Close()
|
||||
|
||||
// Run Database Migrations
|
||||
if err := dbService.Migrate(); err != nil {
|
||||
log.Printf("⚠️ Migration warning: %v", err)
|
||||
}
|
||||
|
||||
// Get GORM DB Connection with metrics wrapper
|
||||
// 3. Init Cache using factory pattern
|
||||
cacheFactory := cache.NewFactory(cfg.Cache)
|
||||
cacheManager, err := cacheFactory.CreateManager()
|
||||
if err != nil {
|
||||
log.Fatalf("❌ Failed to initialize cache manager: %v", err)
|
||||
}
|
||||
defer cacheManager.Close()
|
||||
|
||||
// Initialize Health Monitor
|
||||
healthMonitor := monitoring.NewHealthMonitor(metrics, dbService, cacheManager, cfg)
|
||||
|
||||
// Start health monitoring in background
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go healthMonitor.Start(ctx)
|
||||
|
||||
// 4. Init Layers (Repo & Usecase) - Now with wrapped DB
|
||||
// Update all repository initializations to use wrappedDB
|
||||
|
||||
// Example:
|
||||
// ethnicRepo := ethnic.NewRepository(wrappedDB)
|
||||
// ethnicService := ethnic.NewService(ethnicRepo)
|
||||
// ethnicHandler := handlers.NewEthnicHandler(ethnicService)
|
||||
|
||||
// 5. Init HTTP Server with metrics
|
||||
engine := gin.New()
|
||||
|
||||
// Add metrics middleware
|
||||
engine.Use(monitoring.MetricsMiddleware(metrics))
|
||||
|
||||
// Register Prometheus metrics endpoint
|
||||
engine.GET("/metrics", gin.WrapH(promhttp.Handler()))
|
||||
|
||||
// Register health endpoint with metrics
|
||||
healthMonitor.RegisterHealthEndpoint(engine)
|
||||
|
||||
// ... rest of the setup ...
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"service/internal/infrastructure/config"
|
||||
"service/internal/infrastructure/database"
|
||||
satuSehatFactory "service/internal/interfaces/satusehat"
|
||||
"service/internal/worker"
|
||||
"service/pkg/logger"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 1. Load Configuration
|
||||
cfg := config.LoadConfig()
|
||||
|
||||
// 2. Initialize Logger khusus untuk Worker
|
||||
loggerConfig := logger.Config{
|
||||
Level: cfg.Logger.Level,
|
||||
Format: cfg.Logger.Format,
|
||||
Output: "both",
|
||||
ServiceName: "service-worker", // Penanda log khusus worker
|
||||
EnableCaller: cfg.Server.Mode != "production",
|
||||
Environment: cfg.Server.Mode,
|
||||
}
|
||||
logger.Init(loggerConfig)
|
||||
|
||||
logger.Default().Info("Background Worker Microservice starting...",
|
||||
logger.String("environment", cfg.Server.Mode),
|
||||
logger.String("log_level", cfg.Logger.Level),
|
||||
)
|
||||
|
||||
if err := cfg.Validate(); err != nil {
|
||||
logger.Default().Fatal("Config validation failed", logger.ErrorField(err))
|
||||
}
|
||||
|
||||
// 3. Initialize Database Manager
|
||||
dbService := database.New(cfg)
|
||||
defer dbService.Close()
|
||||
|
||||
// 4. Initialize SatuSehat Client (Dibutuhkan oleh worker manager)
|
||||
satusehatFact := satuSehatFactory.NewSatuSehatFactory(cfg.SatuSehat)
|
||||
satusehatClient := satusehatFact.Client()
|
||||
|
||||
// 5. Setup Context untuk Graceful Shutdown
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
// 6. Initialize dan Jalankan Worker Manager
|
||||
workerManager := worker.NewManager(cfg, dbService, satusehatClient)
|
||||
workerManager.Start(ctx)
|
||||
|
||||
// 7. Listener untuk OS Signals (Ctrl+C / SIGTERM)
|
||||
quit := make(chan os.Signal, 1)
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
|
||||
<-quit // Block aplikasi sampai signal diterima
|
||||
logger.Default().Warn("Signal received, shutting down worker service...")
|
||||
cancel() // Batalkan context, trigger penghentian semua worker
|
||||
}
|
||||
Reference in New Issue
Block a user