Can't restart service on the same port without error


#1

I’m trying to develop my first package. My project code is all here: https://github.com/dbhagen/habitat-p4broker
I’m developing on OS X 10.13.4.
$ brew --version
Homebrew 1.6.7
Homebrew/homebrew-core (git revision 60e56c; last commit 2018-06-07)

$ brew tap
habitat-sh/habitat
homebrew/cask
homebrew/core
homebrew/dupes
homebrew/services
homebrew/versions

$ hab --version
hab 0.56.0/20180530234342

$ docker --version
Docker version 18.03.1-ce, build 9ee9f40

Right now, everything works great. The first time the service is built/run, I get this:
[4][default:/src:0]# hab-sup(AG): The dbhagen/p4broker service was successfully loaded hab-sup(MR): Starting dbhagen/p4broker p4broker.default(UCW): Watching user.toml p4broker.default(HK): init, compiled to /hab/svc/p4broker/hooks/init p4broker.default(HK): run, compiled to /hab/svc/p4broker/hooks/run p4broker.default(HK): Hooks compiled p4broker.default(SR): Hooks recompiled default(CF): Updated p4broker.conf 8bc3c6e73c996f62ed0ce1e28b0c065fb3df2e6ed1d732b26b137a3c35e96738 p4broker.default(SR): Configuration recompiled p4broker.default(SR): Initializing p4broker.default(SV): Starting service as user=hab, group=hab p4broker.default(E): Perforce Broker info: p4broker.default(E): Perforce Broker starting... p4broker.default(E): Perforce Broker info: p4broker.default(E): Loaded configuration from: /hab/svc/p4broker/config/p4broker.conf p4broker.default(O): Perforce Broker starting...

But any time following, I get this:
[5][default:/src:0]# hab-sup(AG): The dbhagen/p4broker service was successfully loaded hab-sup(MR): Starting dbhagen/p4broker p4broker.default(UCW): Watching user.toml p4broker.default(HK): Hooks compiled p4broker.default(SR): Initializing p4broker.default(SV): Starting service as user=hab, group=hab p4broker.default(O): Perforce Broker starting... p4broker.default(E): Perforce Broker info: p4broker.default(E): Perforce Broker starting... p4broker.default(E): Perforce Broker info: p4broker.default(E): Loaded configuration from: /hab/svc/p4broker/config/p4broker.conf p4broker.default(E): Perforce Broker error: p4broker.default(E): Listen 2018 failed. p4broker.default(E): TCP listen on 2018 failed. p4broker.default(E): bind: 0.0.0.0:2018: Address already in use

I’ve tried the following and no change:
hab svc stop dbhagen/p4broker hab svc unload dbhagen/p4broker hab svc load dbhagen/p4broker hab svc start dbhagen/p4broker

The only thing that works is exiting the Hab Studio and reentering.

Any help would be great! Thanks


#2

Attaching my plan.sh file
pkg_name=p4broker
pkg_origin=dbhagen
pkg_version=“0.1.0”
pkg_maintainer=“Daniel Hagen <>”
pkg_license=(“Apache-2.0”)
pkg_source=“http://www.perforce.com/downloads/perforce/r18.1/bin.linux26x86_64/p4broker
pkg_filename=“p4broker”
pkg_shasum=“5be8b8e6c327e590d83c0597d27120fee1ff628f568342309bcde4b5b6e669d9”
pkg_deps=(core/glibc)
pkg_build_deps=(core/make core/gcc core/patchelf)
# pkg_lib_dirs=(lib)
# pkg_include_dirs=(include)
pkg_bin_dirs=(bin)
# pkg_pconfig_dirs=(lib/pconfig)
pkg_svc_run=“p4broker -c $pkg_svc_config_path/p4broker.conf”
pkg_exports=(
[host]=srv.address
[port]=srv.port
)
pkg_exposes=(port)
# pkg_binds=(
# [database]=“port host”
# )
# pkg_binds_optional=(
# [storage]=“port host”
# )
# pkg_interpreters=(bin/bash)
# pkg_svc_user=“hab”
# pkg_svc_group="$pkg_svc_user"
# pkg_description=“Some description.”
# pkg_upstream_url=“http://example.com/project-name

do_build() {
	# No build needed.
	return 0
}

do_unpack() {
	# No unpack needed
	return 0
}

do_install() {
  mkdir -p $pkg_prefix/bin
  cp $HAB_CACHE_SRC_PATH/p4broker $pkg_prefix/bin/p4broker
  chmod +x $pkg_prefix/bin/p4broker
  patchelf --interpreter "$(pkg_path_for glibc)/lib/ld-linux-x86-64.so.2" ${pkg_prefix}/bin/p4broker

  mkdir -p $pkg_svc_var_path/p4/broker
  mkdir -p $pkg_svc_var_path/p4/broker/log
  chmod 770 $pkg_svc_var_path/p4/broker/log
  chown hab:hab $pkg_svc_var_path/p4/broker/log
  

  mkdir -p $pkg_svc_var_path/p4broker_sslkeys
  chmod 700 $pkg_svc_var_path/p4broker_sslkeys
  chown hab:hab $pkg_svc_var_path/p4broker_sslkeys
  export P4SSLDIR=$pkg_svc_var_path/p4broker_sslkeys
  #$pkg_prefix/bin/p4broker -Gc
  #$pkg_prefix/bin/p4broker -Gf
}`

#3

I think your run hook is incorrect. It should be:

export P4SSLDIR={{pkg.svc_var_path}}/p4broker_sslkeys
exec p4broker -c {{pkg.svc_config_path}}/p4broker.conf

#4

I’m sorry, the version you were looking at was mid rebasing of the app data folders. The latest commit should show the paths unified.


#5

Strange, can you try changing your shebang to look like this:

#!/bin/bash -xe
#
exec 2>&1

#6

I think you’ll want the exec before the p4broker call


#7

I assume you mean in my run hook. I’ve added it, but now when I run service start I get no output.
[9][default:/src:0]# hab svc start dbhagen/p4broker


#8

do you see output when you run sl after that command?


#9

Can’t run sl without ctrl+c-ing out of the interactive session. Not sure why the run hook modification made it an interactive session…


#10

I take that back. Exited Hab Studio and reentered and it’s running.


#11
p4broker.default(SR): Initializing
p4broker.default(SV): Starting service as user=hab, group=hab
p4broker.default(E): + export P4SSLDIR=/hab/svc/p4broker/data/sslkeys
p4broker.default(E): + P4SSLDIR=/hab/svc/p4broker/data/sslkeys
p4broker.default(E): + exec p4broker -c /hab/svc/p4broker/config/p4broker.conf
p4broker.default(O): Perforce Broker starting...
p4broker.default(O): Perforce Broker info:
p4broker.default(O): 	Perforce Broker starting...
p4broker.default(O): Perforce Broker info:
p4broker.default(O): 	Loaded configuration from: /hab/svc/p4broker/config/p4broker.conf

#12

Ok, that’s curious. It’s now working reliably. Any thoughts on why adding exec and modifying the shebang would make it behave better?


#13

It’s the way the process gets forked and started. You want your program to be the thing the supervisor is watching, not the run hook. Exec replaces the current shell with a new one starting your command.