2023-03-31 11:49:03 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-04-10 11:43:13 +02:00
|
|
|
"flag"
|
2023-03-31 11:49:03 +02:00
|
|
|
"log"
|
2023-04-10 11:43:13 +02:00
|
|
|
"net/http"
|
2023-03-31 11:49:03 +02:00
|
|
|
_ "net/http/pprof"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2023-04-10 11:43:13 +02:00
|
|
|
"runtime/pprof"
|
|
|
|
"runtime/trace"
|
2023-03-31 11:49:03 +02:00
|
|
|
"syscall"
|
|
|
|
|
2023-04-05 19:04:52 +02:00
|
|
|
"gitea.urkob.com/urko/crono"
|
2023-03-31 11:49:03 +02:00
|
|
|
"gitea.urkob.com/urko/ess-etl-go/config"
|
2023-04-10 14:52:05 +02:00
|
|
|
"gitea.urkob.com/urko/ess-etl-go/internal/etl"
|
2023-03-31 11:49:03 +02:00
|
|
|
"gitea.urkob.com/urko/ess-etl-go/internal/request"
|
|
|
|
"gitea.urkob.com/urko/ess-etl-go/internal/xml_loader"
|
|
|
|
"gitea.urkob.com/urko/ess-etl-go/pkg/adapter/repository/mongodb/employee_wi"
|
|
|
|
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo/options"
|
|
|
|
)
|
|
|
|
|
2023-04-10 11:43:13 +02:00
|
|
|
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
|
|
|
|
var traceflag = flag.Bool("trace", false, "write trace to file")
|
2023-03-31 11:49:03 +02:00
|
|
|
|
|
|
|
func main() {
|
2023-04-10 11:43:13 +02:00
|
|
|
flag.Parse()
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
f, err := os.Create(*cpuprofile)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
pprof.StartCPUProfile(f)
|
|
|
|
defer pprof.StopCPUProfile()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add pprof endpoints
|
|
|
|
go func() {
|
|
|
|
if *cpuprofile != "" {
|
|
|
|
log.Println(http.ListenAndServe("localhost:6060", nil))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if *traceflag {
|
|
|
|
log.Println("trace on")
|
|
|
|
trace.Start(os.Stdout)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if *traceflag {
|
|
|
|
trace.Stop()
|
|
|
|
}
|
|
|
|
}()
|
2023-03-31 11:49:03 +02:00
|
|
|
|
|
|
|
cr := crono.New()
|
2023-04-10 11:43:13 +02:00
|
|
|
if !*traceflag {
|
|
|
|
defer cr.Table()
|
|
|
|
}
|
2023-03-31 11:49:03 +02:00
|
|
|
|
2023-04-10 11:43:13 +02:00
|
|
|
cfg := config.NewConfig(".env")
|
2023-03-31 11:49:03 +02:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
dbOpts := options.Client()
|
|
|
|
dbOpts.ApplyURI(cfg.DbAddress)
|
|
|
|
|
|
|
|
client, err := mongo.NewClient(dbOpts)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("mongo.NewClient", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("mongodb client is connected")
|
|
|
|
|
|
|
|
if err = client.Connect(ctx); err != nil {
|
|
|
|
log.Fatalln("client.Connect", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
employeeWICollection := client.Database(cfg.DbName).Collection(cfg.EmployeeWorkInformationCollection)
|
|
|
|
if err = employeeWICollection.Drop(ctx); err != nil {
|
|
|
|
log.Fatalln("employeeWICollection.Drop", err)
|
|
|
|
}
|
2023-04-10 14:52:05 +02:00
|
|
|
repo := employee_wi.NewRepo(employeeWICollection)
|
2023-03-31 11:49:03 +02:00
|
|
|
|
|
|
|
r := request.NewRequestService(cfg.AmsApi, cfg.AmsApiKey)
|
|
|
|
|
|
|
|
ewiLoader := xml_loader.NewEmployeeWILoader(r)
|
|
|
|
from, to := "2023-01-01", "2023-01-31"
|
|
|
|
cr.MarkAndRestart("dependencies loaded")
|
|
|
|
|
2023-04-10 14:52:05 +02:00
|
|
|
etlLoader := etl.New(ewiLoader, repo)
|
2023-04-10 16:16:16 +02:00
|
|
|
err = etlLoader.FanOut(ctx, cfg.EmployeeIdList, from, to)
|
|
|
|
// err = etlLoader.FanOutV2(ctx, cfg.EmployeeIdList, from, to)
|
|
|
|
// err = etlLoader.FanOut2(ctx, cfg.EmployeeIdList, from, to)
|
|
|
|
// err = etlLoader.Main(signalContext(ctx), cr, cfg.EmployeeIdList, from, to)
|
2023-04-10 14:52:05 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalln("etlLoader.FanOut", err)
|
|
|
|
}
|
|
|
|
cr.MarkAndRestart("FanOut")
|
2023-03-31 11:49:03 +02:00
|
|
|
|
|
|
|
log.Println("gracefully shutdown")
|
|
|
|
}
|
|
|
|
|
|
|
|
func signalContext(ctx context.Context) context.Context {
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(sigs, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
log.Println("listening for shutdown signal")
|
|
|
|
<-sigs
|
|
|
|
log.Println("shutdown signal received")
|
|
|
|
signal.Stop(sigs)
|
|
|
|
close(sigs)
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
return ctx
|
|
|
|
}
|