Skip to content

Commit

Permalink
Add support to configure the number of web workers for puma (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
camertron authored Jan 27, 2023
2 parents 86f3a3f + 0237d55 commit 2bf333d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/docs/customizing_docker_build.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ The assets phase compiles static assets managed by both the asset pipeline and W
The webserver phase instructs the Docker image to use a webserver to run your app. Currently only the Rails default, [Puma](https://github.com/puma/puma), is supported (including puma in your Gemfile is all you need to do - no other configuration is necessary).

* `webserver_phase.port = Integer`: Sets the port the webserver should listen on.
* `webserver_phase.workers = Integer`: Sets the number of webserver workers to spawn. Defaults to 4.
* `webserver_phase.webserver = Symbol`: Sets the webserver to use. Must be `:puma`. Additional webservers may be supported in the future if there is demand. The only reason to set this field manually is if Kuby can't detect Puma in your Gemfile for some reason.

## Creating A Custom Build Phase
Expand Down
12 changes: 11 additions & 1 deletion lib/kuby/docker/webserver_phase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Puma < Webserver
def apply_to(dockerfile)
dockerfile.cmd(
'puma',
'--workers', '4',
'--workers', phase.workers.to_s,
'--bind', 'tcp://0.0.0.0',
'--port', phase.port.to_s,
'--pidfile', './server.pid',
Expand All @@ -40,11 +40,15 @@ def apply_to(dockerfile)
end

DEFAULT_PORT = T.let(8080, Integer)
DEFAULT_WORKERS = T.let(4, Integer)
WEBSERVER_MAP = T.let({ puma: Puma }.freeze, T::Hash[Symbol, T.class_of(Webserver)])

T::Sig::WithoutRuntime.sig { params(port: Integer).returns(Integer) }
attr_writer :port

T::Sig::WithoutRuntime.sig { params(workers: Integer).returns(Integer) }
attr_writer :workers

T::Sig::WithoutRuntime.sig { returns(T.nilable(Symbol)) }
attr_reader :webserver

Expand All @@ -56,6 +60,7 @@ def initialize(environment)
super

@port = T.let(@port, T.nilable(Integer))
@workers = T.let(@workers, T.nilable(Integer))
@webserver = T.let(@webserver, T.nilable(Symbol))
end

Expand All @@ -73,6 +78,11 @@ def port
@port || DEFAULT_PORT
end

T::Sig::WithoutRuntime.sig { returns(Integer) }
def workers
@workers || DEFAULT_WORKERS
end

private

T::Sig::WithoutRuntime.sig { returns(T.nilable(Symbol)) }
Expand Down

0 comments on commit 2bf333d

Please sign in to comment.