Skip to content

Commit c341df1

Browse files
committed
more options for the EventHandler, updated documentation
1 parent d6ad85c commit c341df1

File tree

4 files changed

+38
-20
lines changed

4 files changed

+38
-20
lines changed

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,11 @@
55
* added support for retryable reads and writes
66
* refactored the test cases
77
* now using mtools for a MongoDB deployment in the travis ci environment
8-
* travis ci uses only the latest MongoDB version [The failCommand](https://github.com/mongodb/mongo/wiki/The-%22failCommand%22-fail-point)
9-
8+
* travis ci uses only the latest MongoDB version [The failCommand](https://github.com/mongodb/mongo/wiki/The-%22failCommand%22-fail-point)
9+
10+
* Bugfixes
11+
* Using `max_staleness_ms` > 0 results in a crash
12+
1013
## 0.6.5
1114

1215
* Enhancements

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,18 @@ and have a look at the test units as well.
371371

372372
### Command Monitoring
373373

374-
You can watch all events that are triggered while the driver send requests and processes responses.
374+
You can watch all events that are triggered while the driver send requests and processes responses. You can use the
375+
`Mongo.EventHandler` as a starting point. It logs the events from the topic `:commands` (by ignoring the `:isMaster` command)
376+
to `Logger.info`:
377+
378+
```elixir
379+
iex> Mongo.EventHandler.start()
380+
iex> {:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017/test")
381+
{:ok, #PID<0.226.0>}
382+
iex> Mongo.find_one(conn, "test", %{})
383+
[info] Received command: %Mongo.Events.CommandStartedEvent{command: [find: "test", ...
384+
[info] Received command: %Mongo.Events.CommandSucceededEvent{command_name: :find, ...
385+
```
375386

376387
## Testing and Travis CI
377388

@@ -389,10 +400,9 @@ mlaunch init --setParameter enableTestCommands=1 --replicaset --name "rs_1"
389400
mix test --exclude ssl --exclude socket
390401
```
391402

392-
The SSL test suite is enabled by default. You have two options. Either exclude
393-
the SSL tests or enable SSL on your Mongo server.
403+
The SSL test suite is disabled by default.
394404

395-
### Disable the SSL tests
405+
### Enable the SSL tests
396406

397407
`mix test --exclude ssl`
398408

@@ -407,7 +417,6 @@ $ mongod --sslMode allowSSL --sslPEMKeyFile /path/to/mongodb.pem
407417
* For `--sslMode` you can use one of `allowSSL` or `preferSSL`
408418
* You can enable any other options you want when starting `mongod`
409419

410-
411420
## Special thanks
412421

413422
Special thanks to [JetBrains](https://www.jetbrains.com/?from=elixir-mongodb-driver) for providing a free JetBrains Open Source license for their complete toolbox.

lib/mongo/event_handler.ex

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,39 @@ defmodule Mongo.EventHandler do
55

66
@all [:commands, :topology]
77

8-
def start(opts \\ [:commands]) do
9-
Logger.info("Starting EventHandler")
8+
def start(opts \\ [topics: [:commands]]) do
109
spawn(__MODULE__, :register, [opts])
1110
end
1211

13-
def register([]) do
14-
register(@all)
15-
end
16-
1712
def register(opts) do
18-
with true <- opts
13+
with true <- (opts[:topics] || @all)
1914
|> Enum.map(fn topic -> Registry.register(:events_registry, topic, []) end)
2015
|> Enum.all?(fn
2116
{:ok, _} -> true
2217
_other -> false
2318
end) do
24-
listen()
19+
listen(opts)
2520
:ok
2621
end
2722
end
2823

29-
def listen() do
30-
# fun.(topic, message)
24+
def listen(opts) do
3125
receive do
26+
{:broadcast, :commands, %{command_name: cmd} = message} when cmd != :isMaster ->
27+
Logger.info("Received command: " <> (inspect message))
28+
listen(opts)
29+
30+
{:broadcast, :commands, is_master} ->
31+
case opts[:is_master] do
32+
true -> Logger.info("Received is master:" <> (inspect is_master))
33+
_ -> []
34+
end
35+
listen(opts)
36+
3237
{:broadcast, topic, message} ->
33-
Logger.info("Received #{topic}-message:" <> (inspect message))
34-
listen()
38+
Logger.info("Received #{topic}: " <> (inspect message))
39+
listen(opts)
40+
3541
other ->
3642
Logger.info("Stopping EventHandler received unknown message:" <> inspect other)
3743
end

test/test_helper.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ version =
1212
|> List.to_tuple
1313

1414
options = [ssl: true, socket: true]
15-
options = if System.get_env("CI") do [tag_sets: true] ++ options else options end
15+
options = if System.get_env("CI") do [tag_set: true] ++ options else options end
1616
options = if version < {3, 4, 0} do [mongo_3_4: true] ++ options else options end
1717
options = if version < {3, 6, 0} do [mongo_3_6: true] ++ options else options end
1818
options = if version < {4, 2, 0} do [mongo_4_2: true] ++ options else options end

0 commit comments

Comments
 (0)