Orlando Torres
Orlando Torres

Reputation: 1

How To Create a Window Service Using Confluent-Kafka-Go

I have a service when I use golang.org/x/sys/windows/svc to create my api as window service. This works perfect. I was able to follow this How to use Golang client on Windows #128 and thats work to. I can consume, produce my api work as I want. I can use both librerys and when I run the service in debug mode everything is fine. But when the api is a window service the service dont start I got a 1053 error message. I was running several test and I can say the service dont work when I use the confluent librery. I know that because this librery dont support window something is missing when .exe run as service but I can figure out what it is.

How to reproduce I make a little program apart from my main project. Just copy and paste the example from here golang.org/x/sys/windows/svc and add a consumer from confluent.

    if _, err := os.Stat(".\LOGSmsPrueba\"); os.IsNotExist(err) {
    os.MkdirAll(".\LOGSmsPrueba\", 0700)
    }
    filelogs, _ := os.OpenFile(".\LOGSmsPrueba\LOGS.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 
    0644)
    
    defer filelogs.Close()
    
    log := zerolog.New(filelogs).With().Logger()
    log.Debug().Msg("Before The Thread Of Consumer")
    go func() {
    
        log := zerolog.New(filelogs).With().Logger()
    
        kafkaservice.ConsumidorKafka(&log)
    }()
    
    changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
    loop:
    for {
    select {
    
        case c := <-r:
            switch c.Cmd {
            case svc.Interrogate:
                changes <- c.CurrentStatus
            
                time.Sleep(100 * time.Millisecond)
                changes <- c.CurrentStatus
            case svc.Stop, svc.Shutdown:
            
                testOutput := strings.Join(args, "-")
                testOutput += fmt.Sprintf("-%d", c.Context)
                elog.Info(1, testOutput)
                break loop
            case svc.Pause:
                changes <- svc.Status{State: svc.Paused, Accepts: cmdsAccepted}
    
            case svc.Continue:
                changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}
    
            default:
                elog.Error(1, fmt.Sprintf("unexpected control request #%d", c))
            }
        }
    }
    changes <- svc.Status{State: svc.StopPending}
    return

Checklist

librdkafka Version: 1.7.0 confluent-kafka-go Version:1.7.0 golang.org/x/sys v0.0.0-20210615035016

Upvotes: 0

Views: 461

Answers (1)

Orlando Torres
Orlando Torres

Reputation: 1

Finally I made it work. The only part that was missing was compile in a static way like this => go build -ldflags '-extldflags "-static"'. This fixed the issue with a missing dll, the problem was that on console everything works fine. I never see the missing dll. But when I work on a window server 2016 the console did not run. The specific error was "Run bin file Systerm Error "libwinpthread-1.dll was not found". I fix thats with static compile and came back to prove the window service with this compilation too. And finally i have my go service running as window service and using confluent kafka librery.

Upvotes: 0

Related Questions