Skip to content

Commit 7920e86

Browse files
authored
Add a feature to provide each process the generated ports from other processes (#194)
* Add a feature to provide each process the generated ports from other processes - include the ports generated for other processes in each process definition - Add `OVERMIND_PROCESS_<name>_PORT` settings to each process wrapper script - add docs for the feature * Address PR feedback - drop the extra command line flag and gate - add a bit of extra safety to the port environment variable name * PR feedback: don't carry the other ports around in the procfile Construct the other service port list as the script is generated * Explicitly allow only [a-zA-Z0-9_] in the process names * Precompile the process names
1 parent 1f96fb0 commit 7920e86

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,17 @@ $ overmind start -N
159159
$ OVERMIND_NO_PORT=1 overmind start
160160
```
161161

162+
#### Referencing the ports of other processes
163+
164+
If overmind is specifying a port, each other process will be provided with environment variables that include them. Those variables will be have the format `OVERMIND_PROCESS_<name>_PORT` where "name" is the name of the process from the procfile, with any characters that are invalid in an environment variable replaced with `_`.
165+
166+
If processes are scaled, those port names will be numbered as well, based on the scaling of the process.
167+
168+
```Procfile
169+
web: bin/rails server -p $PORT
170+
proxy: ngrok http --subdomain overmind $OVERMIND_PROCESS_web_PORT
171+
```
172+
162173
#### Running only the specified processes
163174

164175
You can specify the names of processes you want to run:

start/command.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"os"
77
"os/signal"
88
"path/filepath"
9+
"regexp"
910
"sync"
1011
"syscall"
1112
"time"
@@ -17,6 +18,11 @@ import (
1718

1819
var defaultColors = []int{2, 3, 4, 5, 6, 42, 130, 103, 129, 108}
1920

21+
// This is a set of characters that are not allowed in a process name.
22+
// It's expressed as a negation; all character not matching the set of allowed
23+
// characters will be replaced with an underscore.
24+
var disallowedProcNameCharacters = regexp.MustCompile(`[^a-zA-Z0-9_]`)
25+
2026
type command struct {
2127
title string
2228
timeout int
@@ -85,7 +91,7 @@ func newCommand(h *Handler) (*command, error) {
8591
isIgnored := len(ignoredProcNames) != 0 && utils.StringsContain(ignoredProcNames, e.OrigName)
8692

8793
if shouldRun && !isIgnored {
88-
scriptFilePath := c.createScriptFile(&e, h.Shell, !h.NoPort)
94+
scriptFilePath := c.createScriptFile(&e, pf, h.Shell, !h.NoPort)
8995

9096
c.processes = append(c.processes, newProcess(
9197
c.tmux,
@@ -163,14 +169,22 @@ func (c *command) Quit() {
163169
c.stopTrig <- syscall.SIGINT
164170
}
165171

166-
func (c *command) createScriptFile(e *procfileEntry, shell string, setPort bool) string {
172+
func (c *command) createScriptFile(e *procfileEntry, procFile procfile, shell string, setPort bool) string {
167173
scriptFile, err := os.Create(filepath.Join(c.scriptDir, e.Name))
168174
utils.FatalOnErr(err)
169175

170176
fmt.Fprintf(scriptFile, "#!/usr/bin/env %s\n", shell)
171177
if setPort {
172178
fmt.Fprintf(scriptFile, "export PORT=%d\n", e.Port)
179+
180+
for _, pf := range procFile {
181+
if pf.Name != e.Name {
182+
safeProcessName := sanitizeProcName(pf.Name)
183+
fmt.Fprintf(scriptFile, "export OVERMIND_PROCESS_%s_PORT=%d\n", safeProcessName, pf.Port)
184+
}
185+
}
173186
}
187+
174188
fmt.Fprintf(scriptFile, "export PS=%s\n", e.Name)
175189
fmt.Fprintln(scriptFile, e.Command)
176190

@@ -247,3 +261,7 @@ func (c *command) waitForTimeoutOrStop() {
247261
case <-c.stopTrig:
248262
}
249263
}
264+
265+
func sanitizeProcName(name string) string {
266+
return disallowedProcNameCharacters.ReplaceAllString(name, "_")
267+
}

0 commit comments

Comments
 (0)