cespare / reflex

Run a command when files change

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Not working with http server

Maykonn opened this issue · comments

I'm trying to use reflex to compile, stop and then restart a http server written in Go. But after http server listening in some port, suppose :8080 the reflex does nothing. In a simple hello-world function works fine, but after start a http server not working.

I have a Make file:

build:
	GOOS=linux go build -o ./build/api ./main.go

start:
	make build
	echo "Starting build/api"
	./build/api
	reflex -r \.go make restart

restart: stop build start

stop:
	echo "Stopping ./build/api if it's running"
	kill -9 `cat ./tmp/.pid`

So I just need to run make start and server will start fine, but never will execute the line reflex -r \.go make restart.

The .pid file on stop command is generated by my go program before start the server. And is working fine. Just the restart command is never executed by reflex.

Someone already had this behavior before?

This is an issue with your Makefile, not a reflex problem. The make process blocks on running ./build/api and never runs reflex. You might be able to fix it by starting up the server with ./build/api &.

But in general I'd advise against using PID files (for this or any other purpose).

Reflex has --start-service mode specifically for these server use cases.

@cespare Thanks for your reply and advice about PID files.
I changed my make start to:

start:
	make build
	echo "Starting build/api"
	./build/api&
	reflex --start-service true -r \.go make start

And I changed a log line in my code just to test and then the output on the cli was something like that:
[00] make[1]: Nothing to be done for 'restart', the server still running but not with the new changes.

This is still makefile problems, not anything to do with reflex.

Google "makefile phony".

I understand now. Thanks.

I rewrite my make start to:

start:
	reflex --start-service true -r \.go
	make build
	./build/api

build:
	GOOS=linux go build -o ./build/api ./main.go

.PHONY: start build

But not works. My program start with the http server as well, but when I change from fmt.Println("test1") to fmt.Println("test2") the program shows "test1" yet, on cli.

May could you provide a better example how to use the --start-service correctly to stop a http server, recompile the code and start the http server again?

Thanks in advance!

There are a few different issues here. One problem is that you need to write --start-service=true, or just --start-service. Another is that your start task blocks on reflex and never runs the other commands.

This should give you some ideas:

.PHONY: build run reflex

build:
	go build -o ./build/api ./main.go

run: build
	./build/api

reflex:
	reflex --start-service -r '\.go$$' make run

Yes, that's right!
Thanks @cespare