Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-add oneline-procfile-manager #2

Merged
merged 1 commit into from Apr 3, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,116 @@
<!doctype html>
<style>
pre {
white-space: pre-line;
margin: 5% 10%;
font-family: Courier;
}
pre pre {
white-space: pre;
overflow-x: scroll;
margin: 0 0 -1em 0;
background-color: #eee;
}
code {
background-color: #eee;
font-family: Courier;
}
footer {
opacity: 0.1;
}
</style>

<link rel=icon href=/favicon-filipp.ico type=image/x-icon/>

<pre>
@═╦╣╚╗ <strong><a href=/>A mazing engineer</a></strong>


03-Jun-2019 <strong>One-line Procfile manager</strong>


A <a href=http://ddollar.github.io/foreman/#PROCFILE>Procfile</a> contains pairs of process names and commands used to run it. It is often used to run several processes locally, during development, typically a web server, asset server, and background job processor:

<pre>
web: bundle exec rails server
assets: yarn serve
jobs: bundle exec sidekiq
</pre>

There is a number of inherent problems with <a href=https://github.com/ddollar/foreman>Foreman</a>:
- the output of all processes is interleaved. It's near impossible to understand what printed what.
- using REPL when stopping at a debug breakpoint splits your input evenly across the processes, making it barely usable.
- selective process restart is impossible.
- selective filtering or focusing of process output is impossible.

There are tools out there, specifically <a href=https://github.com/DarthSim/overmind>overmind</a>. It runs processes in <a href=https://github.com/tmux/tmux>tmux</a>, and that resolves the problems Forman has.
However Overmind is an additional tool that you need to learn.

That led me to a thought that this can be done in a simpler way, running the processes in tmux's split panes.

And a single'ish-line <a href=https://github.com/pirj/toreman>Toreman</a> was born that does exactly that.
Well, initially it was actually single line, but I decided to format it for better readability.

Moving between the panes with &lt;Prefix&gt; h/j/k/l can be configured with:

<pre>
# ~/.tmux.conf
bind h select-pane -L
bind j select-pane -D
bind k select-pane -U
bind l select-pane -R
</pre>

Or you can employ a tmux <a href=https://github.com/tmux-plugins/tmux-pain-control/blob/master/pain_control.tmux#L19>pain control plugin</a>.

Zooming a pane has a standard convenient key binding, &lt;Prefix&gt;+z.

Killing a process is simple as ^C, pausing it - ^Z.

I'm not an expert in the field, and writing shell script feels like solving a puzzle, but it's usually very rewarding.
Full annotated Toreman source below:

<pre class=shell>
grep --invert-match '^#' &lt; Procfile | # Filter out comments
sed -e 's/^[^:]*: //' | # Split into process name/command pairs
xargs -I {} \ # For each pair:
tmux \ #
split-window -v \; \ # - split window
send-keys '[ -s .env ] &amp;&amp; source .env; {}' 'C-m' &amp;&amp; # - type the command, prefixed with a `source .env;`
tmux \ #
select-pane -t 1 \; \ # Switch to initial pane
send-keys 'C-d' \; \ # - and close it
select-layout ${1:-tiled} # Also, select the layout, defaulting to tiled
</pre>

Please do not paste the code above, shell script <a href=https://stackoverflow.com/questions/9522631/how-to-put-a-line-comment-for-a-multi-line-command>won't work with comments inserted like this</a>.

Toreman supports loading the <a href=http://ddollar.github.io/foreman/#ENVIRONMENT>.env file</a>, to load the default environment variables for processes.
Typical .env file looks like this:

<pre>
RAILS_ENV=development
SKIP_BULLET=true
</pre>

Toreman accepts a single argument, a layout to be used. Tiled is the default, the other options are even-horizontal, even-vertical, main-horizontal, and main-vertical.

As per the comments, Toreman might not support alternative shells (e.g. tcsh, ksh) so well.
I've tested on Fish shell, it worked quite fine except that you have to prefix environment variables with an <code>env</code>, e.g.:

<pre>
web: env PORT=8080 bundle exec rails server
</pre>

If you're on macOS and are using Homebrew, installing Toreman is easy as:

<pre class=shell>
brew install pirj/homebrew-toreman/toreman
</pre>

<a href=https://www.reddit.com/r/ruby/comments/bpka4m/toreman_a_oneline_foreman_port_for_use_with_tmux/>Reddit thread</a> with some interesting comments.

<footer>
The content of this article is licensed under <a href=https://creativecommons.org/licenses/by-sa/4.0/>CC-BY-SA</a>.
</footer>
</pre>