2024-07-19 20:02:18 +08:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
2025-11-14 08:42:26 +08:00
|
|
|
"caatsm/pkg/di"
|
|
|
|
|
"context"
|
2025-11-14 21:42:04 +08:00
|
|
|
"errors"
|
2024-07-22 12:42:30 +08:00
|
|
|
"fmt"
|
2025-11-14 08:42:26 +08:00
|
|
|
"os"
|
|
|
|
|
"os/signal"
|
|
|
|
|
"syscall"
|
2025-11-14 21:42:04 +08:00
|
|
|
"time"
|
2024-08-05 10:11:33 +08:00
|
|
|
|
|
|
|
|
"github.com/urfave/cli/v2"
|
2024-07-19 20:02:18 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func main() {
|
2024-08-05 10:11:33 +08:00
|
|
|
app := setupApp()
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
|
fmt.Printf("Error running application: %v\n", err)
|
2025-11-14 08:42:26 +08:00
|
|
|
os.Exit(1)
|
2024-08-05 10:11:33 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func setupApp() *cli.App {
|
2025-11-14 08:42:26 +08:00
|
|
|
return &cli.App{
|
2024-08-05 10:58:20 +08:00
|
|
|
Name: "telegram message process",
|
2025-11-14 08:42:26 +08:00
|
|
|
Usage: "A Civil Aviation Authority Telegram Message Processor",
|
2024-08-05 10:11:33 +08:00
|
|
|
Commands: []*cli.Command{
|
|
|
|
|
{
|
2024-08-05 10:58:20 +08:00
|
|
|
Name: "listen",
|
|
|
|
|
Usage: "Listen to nats messages",
|
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
|
&cli.StringFlag{
|
|
|
|
|
Name: "nats",
|
2024-08-05 11:42:33 +08:00
|
|
|
Aliases: []string{"n"},
|
2024-08-05 10:58:20 +08:00
|
|
|
Usage: "Nats server address",
|
|
|
|
|
Value: "nats://localhost:4222",
|
|
|
|
|
EnvVars: []string{"NATS_SERVER"},
|
|
|
|
|
},
|
|
|
|
|
&cli.StringFlag{
|
|
|
|
|
Name: "topic",
|
2024-08-05 11:42:33 +08:00
|
|
|
Aliases: []string{"t"},
|
2024-08-05 10:58:20 +08:00
|
|
|
Usage: "Nats topic to listen to",
|
2025-11-14 21:42:04 +08:00
|
|
|
Value: "telegram.serial",
|
2024-08-05 10:58:20 +08:00
|
|
|
EnvVars: []string{"NATS_SUBJECT"},
|
|
|
|
|
},
|
|
|
|
|
},
|
2024-08-05 10:11:33 +08:00
|
|
|
Action: executeListen,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
2024-08-05 10:58:20 +08:00
|
|
|
}
|
|
|
|
|
|
2024-08-05 10:11:33 +08:00
|
|
|
func executeListen(c *cli.Context) error {
|
2025-11-14 08:42:26 +08:00
|
|
|
// Initialize dependencies using Wire
|
|
|
|
|
processor, consumer, err := di.InitializeApp()
|
2024-08-05 11:23:22 +08:00
|
|
|
if err != nil {
|
2025-11-14 08:42:26 +08:00
|
|
|
return fmt.Errorf("failed to initialize app: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create context with cancellation
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
// Handle graceful shutdown
|
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
|
|
|
|
signal.Notify(sigChan, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
|
|
// Start consumer in a goroutine
|
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
go func() {
|
|
|
|
|
if err := consumer.Start(ctx); err != nil {
|
|
|
|
|
errChan <- fmt.Errorf("consumer error: %w", err)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2025-11-14 21:42:04 +08:00
|
|
|
var runErr error
|
|
|
|
|
|
2025-11-14 08:42:26 +08:00
|
|
|
// Wait for signal or error
|
|
|
|
|
select {
|
|
|
|
|
case sig := <-sigChan:
|
|
|
|
|
fmt.Printf("Received signal: %v, shutting down...\n", sig)
|
|
|
|
|
cancel()
|
|
|
|
|
case err := <-errChan:
|
2025-11-14 21:42:04 +08:00
|
|
|
cancel()
|
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
|
|
|
runErr = err
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
waitTimeout := 5 * time.Second
|
|
|
|
|
select {
|
|
|
|
|
case err := <-errChan:
|
|
|
|
|
if err != nil && !errors.Is(err, context.Canceled) {
|
|
|
|
|
runErr = err
|
|
|
|
|
}
|
|
|
|
|
case <-time.After(waitTimeout):
|
|
|
|
|
fmt.Printf("Timed out waiting for consumer shutdown after %s\n", waitTimeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := consumer.Shutdown(context.Background()); err != nil {
|
|
|
|
|
if runErr == nil {
|
|
|
|
|
runErr = fmt.Errorf("failed to drain NATS connection: %w", err)
|
|
|
|
|
}
|
2024-08-05 11:23:22 +08:00
|
|
|
}
|
2025-11-14 08:42:26 +08:00
|
|
|
|
|
|
|
|
// Note: processor is initialized but not directly used here
|
|
|
|
|
_ = processor
|
|
|
|
|
|
2025-11-14 21:42:04 +08:00
|
|
|
return runErr
|
2024-07-19 20:02:18 +08:00
|
|
|
}
|