From 2cf3eeb616b3197fdeda1056f0a15512b72aade1 Mon Sep 17 00:00:00 2001 From: Alex Antonov Date: Sat, 1 Oct 2016 13:09:34 +0300 Subject: [PATCH] Refactoring by placing default options in the one place --- README.md | 7 +++++-- lib/std_json_io.ex | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 36 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index f78dec2..1f238d6 100644 --- a/README.md +++ b/README.md @@ -59,7 +59,9 @@ 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 @@ -67,4 +69,5 @@ config :my_app, MyApp.ReactIo, 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 diff --git a/lib/std_json_io.ex b/lib/std_json_io.ex index 619b16d..4fac2dc 100644 --- a/lib/std_json_io.ex +++ b/lib/std_json_io.ex @@ -1,5 +1,4 @@ defmodule StdJsonIo do - defmacro __using__(opts) do otp_app = Keyword.get(opts, :otp_app) @@ -10,28 +9,53 @@ 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( @@ -39,11 +63,7 @@ defmodule StdJsonIo do [__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