Skip to content

Ability to prevent server & reloader running for some environments #8

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,15 @@ config :my_app, MyApp.ReactIo,
script: "path/to/script", # for react-io use "react-stdio"
watch_files: [
Path.join([__DIR__, "../priv/server/js/component.js"]) # do not watch files in dev
]
],
worker: false, # default true
reloader: false # default true
```

* `script` - the script to run for the IO server
* `watch_files` - A list of files to watch for changes. When the file changes,
kill the IO worker and restart, picking up any changes. Use only in dev.
* `pool_size` - The size for the pool of workers - See poolboy `size`
* `max_overflow` - The poolboy `max_overflow`

* `worker` - Worker will start if `true`. You probably want to set this option to `false` in `dev` or `test` environments
* `reloader - Reloader will start if worker has been started and this option is set `true`. You probably want to set this option to `false` in `test or `prod` environments
42 changes: 31 additions & 11 deletions lib/std_json_io.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
defmodule StdJsonIo do

defmacro __using__(opts) do
otp_app = Keyword.get(opts, :otp_app)

Expand All @@ -10,40 +9,61 @@ defmodule StdJsonIo do
quote do
use Supervisor
@pool_name Module.concat(__MODULE__, Pool)
@options Keyword.merge(unquote(opts), (Application.get_env(unquote(otp_app), __MODULE__) || []))

@default_options [
pool_size: 5,
max_overflow: 10,
watch_files: [],
worker: true,
reloader: true
]
@options @default_options
|> Keyword.merge(unquote(opts))
|> Keyword.merge(Application.get_env(unquote(otp_app), __MODULE__, []))

def start_link(opts \\ []) do
Supervisor.start_link(__MODULE__, :ok, name: {:local, __MODULE__})
end

def init(:ok) do
worker = start_worker(@options.worker)
reloader = start_reloader(worker and @options.reloader)

supervise([worker, reloader], strategy: :one_for_one, name: __MODULE__)
end

#
# Worker starts only if opposite wasn't specified in options
#
defp start_worker(false), do: false
defp start_worker(true) do
pool_options = [
name: {:local, @pool_name},
worker_module: StdJsonIo.Worker,
size: Keyword.get(@options, :pool_size, 5),
max_overflow: Keyword.get(@options, :max_overflow, 10)
size: Keyword.get(@options, :pool_size),
max_overflow: Keyword.get(@options, :max_overflow)
]

script = Keyword.get(@options, :script)

children = [:poolboy.child_spec(@pool_name, pool_options, [script: script])]
:poolboy.child_spec(@pool_name, pool_options, [script: script])
end

#
# Reloader starts only if worker started and it was specified in options
#
defp start_reloader(false)
defp start_reloader() do
files = Keyword.get(@options, :watch_files)

if files && length(files) > 0 do
if length(files) > 0 do
Application.ensure_started(:fs, :permanent)

reloader_spec = worker(
StdJsonIo.Reloader,
[__MODULE__, Enum.map(files, &Path.expand/1)],
[]
)

children = [reloader_spec | children]
end

supervise(children, strategy: :one_for_one, name: __MODULE__)
end

def restart_io_workers! do
Expand Down