Skip to content

Commit 44f0897

Browse files
committed
#63: added the reader example
1 parent 7809a34 commit 44f0897

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# An Alternative Elixir Driver for MongoDB
22
[![Build Status](https://travis-ci.org/zookzook/elixir-mongodb-driver.svg?branch=master)](https://travis-ci.org/zookzook/elixir-mongodb-driver)
3-
[![Coverage Status](https://coveralls.io/repos/github/zookzook/elixir-mongodb-driver/badge.svg?branch=master)](https://coveralls.io/github/zookzook/elixir-mongodb-driver?branch=master)
43
[![Hex.pm](https://img.shields.io/hexpm/v/mongodb_driver.svg)](https://hex.pm/packages/mongodb_driver)
54
[![Hex.pm](https://img.shields.io/hexpm/dt/mongodb_driver.svg)](https://hex.pm/packages/mongodb_driver)
65
[![Hex.pm](https://img.shields.io/hexpm/dw/mongodb_driver.svg)](https://hex.pm/packages/mongodb_driver)
76
[![Hex.pm](https://img.shields.io/hexpm/dd/mongodb_driver.svg)](https://hex.pm/packages/mongodb_driver)
87

98
## Features
109

11-
* Supports MongoDB versions 3.2, 3.4, 3.6, 4.0, 4.2, 4.4
10+
* Supports MongoDB versions 3.2, 3.4, 3.6, 4.x, 5.x
1211
* Connection pooling ([through DBConnection 2.x](https://github.com/elixir-ecto/db_connection))
1312
* Streaming cursors
1413
* Performant ObjectID generation

examples/reader.ex

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
defmodule Reader do
2+
3+
require Logger
4+
5+
##
6+
# see https://github.com/zookzook/elixir-mongodb-driver/issues/63 for more information
7+
#
8+
# 1. start a replica set and call the Reader.test()
9+
# 2. go to the primary db and call db.adminCommand({replSetStepDown: 30})
10+
# 3. check the log to see the error message only one time
11+
##
12+
def start_link(conn) do
13+
Logger.info("starting reader")
14+
15+
Task.start_link(fn -> read(conn, false) end)
16+
end
17+
18+
defp read(conn, error) do
19+
20+
if error do
21+
Logger.info("Called with error")
22+
end
23+
24+
# Gets an enumerable cursor for the results
25+
cursor = Mongo.find(conn, "data", %{})
26+
27+
error = case cursor do
28+
{:error, error} ->
29+
Logger.info("Error: #{inspect error}")
30+
true
31+
32+
_ ->
33+
cursor
34+
|> Enum.to_list()
35+
|> Enum.count()
36+
false
37+
end
38+
39+
read(conn, error)
40+
end
41+
42+
def test() do
43+
{:ok, conn} = Mongo.start_link(url: "mongodb://localhost:27017,localhost:27018,localhost:27019/load?replicaSet=rs_1")
44+
45+
Enum.map(1..10_000, fn counter -> Mongo.insert_one(conn, "data", %{counter: counter}) end)
46+
Reader.start_link(conn)
47+
end
48+
end

0 commit comments

Comments
 (0)