derive worker-host and id at runtime if not provided
@@ -24,6 +24,10 @@ import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import org.apache.commons.lang3.StringUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
@@ -108,4 +112,26 @@ public class WorkerConfig implements Serializable {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
return mapper.readValue(new File(yamlFile), WorkerConfig.class);
}
+
+ public String getWorkerId() {
+ if (StringUtils.isBlank(this.workerId)) {
+ this.workerId = getWorkerHostname();
+ }
+ return this.workerId;
+ }
+
+ public String getWorkerHostname() {
+ if (StringUtils.isBlank(this.workerHostname)) {
+ this.workerHostname = unsafeLocalhostResolve();
+ }
+ return this.workerHostname;
+ }
+
+ public static String unsafeLocalhostResolve() {
+ try {
+ return InetAddress.getLocalHost().getHostName();
+ } catch (UnknownHostException ex) {
+ throw new IllegalStateException("Failed to resolve localhost name.", ex);
+ }
+ }
}
