Current section
8 Versions
Jump to
Current section
8 Versions
Compare versions
5
files changed
+45
additions
-9
deletions
| @@ -5,7 +5,7 @@ Conqueuer (pronounced like conquer) is a non-persistent Elixir work queue. | |
| 5 5 | |
| 6 6 | ### Documentation and Information |
| 7 7 | |
| 8 | - The [docs](http://hexdocs.pm/conqueuer/0.2.0/Conqueuer.html) can be found on the |
| 8 | + The [docs](http://hexdocs.pm/conqueuer/0.3.0/Conqueuer.html) can be found on the |
| 9 9 | hexdocs website and [further information](https://hex.pm/packages/conqueuer) on the |
| 10 10 | hex website. |
| 11 11 | |
| @@ -17,7 +17,7 @@ Conqueuer can be installed like: | |
| 17 17 | 1. Add test to your list of dependencies in `mix.exs`: |
| 18 18 | |
| 19 19 | def deps do |
| 20 | - [{:conqueuer, "~> 0.2.0"}] |
| 20 | + [{:conqueuer, "~> 0.3.0"}] |
| 21 21 | end |
| 22 22 | |
| 23 23 | 2. Ensure test is started before your application: |
| @@ -20,4 +20,4 @@ | |
| 20 20 | [{<<"app">>,<<"poolboy">>}, |
| 21 21 | {<<"optional">>,false}, |
| 22 22 | {<<"requirement">>,<<"~> 1.5">>}]}]}. |
| 23 | - {<<"version">>,<<"0.2.0">>}. |
| 23 | + {<<"version">>,<<"0.3.0">>}. |
| @@ -43,6 +43,10 @@ defmodule Conqueuer do | |
| 43 43 | |
| 44 44 | Define a [pool](Conqueuer.Pool.html): |
| 45 45 | |
| 46 | + Conqueuer.define_pool_supervisor( :resolvers, MyApp.ResolversPoolSupervisor, MyApp.ResolverWorker ) |
| 47 | + |
| 48 | + Or manually: |
| 49 | + |
| 46 50 | defmodule MyApp.ResolversPoolSupervisor do |
| 47 51 | use Conqueuer.Pool, name: :resolvers, |
| 48 52 | worker: MyApp.ResolverWorker, |
| @@ -70,7 +74,7 @@ defmodule Conqueuer do | |
| 70 74 | |
| 71 75 | children = Conqueuer.child_specs(:resolvers, MyApp.ResolversPoolSupervisor) |
| 72 76 | |
| 73 | - opts = [strategy: :one_for_one, name: Test.Supervisor] |
| 77 | + opts = [strategy: :one_for_one, name: MyApp.Supervisor] |
| 74 78 | Supervisor.start_link(children, opts) |
| 75 79 | end |
| 76 80 | end |
| @@ -94,12 +98,35 @@ defmodule Conqueuer do | |
| 94 98 | Conqueuer.Foreman.work_arrived(foreman_name) |
| 95 99 | end |
| 96 100 | |
| 101 | + @doc """ |
| 102 | + Dynamically define the pool supervisor module for the pool of workers. |
| 103 | + |
| 104 | + Conqueuer.define_pool_supervisor( :resolvers, MyApp.ResolversPoolSupervisor ) |
| 105 | + """ |
| 106 | + def define_pool_supervisor( pool_name, supervisor_module, worker_module, opts \\ [] ) do |
| 107 | + pool_size = Keyword.get( opts, :pool_size, 2 ) |
| 108 | + max_overflow = Keyword.get( opts, :max_overflow, 0 ) |
| 109 | + |
| 110 | + Code.eval_string(~s( |
| 111 | + defmodule #{supervisor_module} do |
| 112 | + |
| 113 | + use Conqueuer.Pool, name: :#{pool_name}, |
| 114 | + worker: #{worker_module}, |
| 115 | + size: #{pool_size}, |
| 116 | + max_overflow: #{max_overflow} |
| 117 | + |
| 118 | + end |
| 119 | + )) |
| 120 | + end |
| 121 | + |
| 97 122 | @doc """ |
| 98 123 | Generates the child process specs for a Conqueuer work queue. Expects the |
| 99 124 | name of the pool and module of the pool supervisor. |
| 100 125 | |
| 101 126 | Manual way: |
| 102 127 | |
| 128 | + Conqueuer.define_pool_supervisor(:resolvers, MyApp, pool_size: 10, max_overflow: 5) |
| 129 | + |
| 103 130 | children = [ |
| 104 131 | supervisor(MyApp.ResolversPoolSupervisor, [[], [name: :ResolversPoolSupervisor]]), |
| 105 132 | worker(Conqueuer.Queue, [[], [name: :ResolversQueue]]), |
| @@ -111,12 +138,14 @@ defmodule Conqueuer do | |
| 111 138 | |
| 112 139 | Using the helper: |
| 113 140 | |
| 114 | - children = Conqueuer.child_specs( :resolvers, Test.PoolSupervisor ) |
| 141 | + Conqueuer.define_pool_supervisor( :workers, MyApp.ResolversPoolSupervisor, MyApp.ResolverWorker ) |
| 115 142 | |
| 116 | - opts = [strategy: :one_for_one, name: Test.Supervisor] |
| 143 | + children = Conqueuer.child_specs(:resolvers, MyApp.ResolversPoolSupervisor ) |
| 144 | + |
| 145 | + opts = [strategy: :one_for_one, name: MyApp.Supervisor] |
| 117 146 | Supervisor.start_link(children, opts) |
| 118 147 | """ |
| 119 | - def child_specs(pool_name, pool_supervisor_module) do |
| 148 | + def child_specs(pool_name, pool_supervisor_module, opts \\ []) do |
| 120 149 | import Supervisor.Spec, warn: false |
| 121 150 | |
| 122 151 | {foreman, pool, pool_supervisor, queue} = Util.infer_collaborator_names(pool_name) |
| @@ -124,7 +153,7 @@ defmodule Conqueuer do | |
| 124 153 | [ |
| 125 154 | supervisor(pool_supervisor_module, [[], [name: pool_supervisor]]), |
| 126 155 | worker(Conqueuer.Queue, [[], [name: queue]]), |
| 127 | - worker(Conqueuer.Foreman, [[name: pool_name], [name: foreman]]) |
| 156 | + worker(Conqueuer.Foreman, [[name: pool], [name: foreman]]) |
| 128 157 | ] |
| 129 158 | end |
| @@ -28,6 +28,13 @@ defmodule Conqueuer.Util do | |
| 28 28 | (infer_base_name( name ) <> "Queue") |> String.to_atom |
| 29 29 | end |
| 30 30 | |
| 31 | + def infer_worker_name( name ) do |
| 32 | + name |
| 33 | + |> Inflex.singularize |
| 34 | + |> String.capitalize |
| 35 | + |> String.to_atom |
| 36 | + end |
| 37 | + |
| 31 38 | defp infer_base_name( name ) do |
| 32 39 | name |
| 33 40 | |> Atom.to_string |
| @@ -3,7 +3,7 @@ defmodule Conqueuer.Mixfile do | |
| 3 3 | |
| 4 4 | def project do |
| 5 5 | [app: :conqueuer, |
| 6 | - version: "0.2.0", |
| 6 | + version: "0.3.0", |
| 7 7 | elixir: "~> 1.1", |
| 8 8 | build_embedded: Mix.env == :prod, |
| 9 9 | preferred_cli_env: [espec: :test], |