Skip to content

Commit

Permalink
first
Browse files Browse the repository at this point in the history
  • Loading branch information
Sushil Singh committed Aug 2, 2019
0 parents commit dc52748
Show file tree
Hide file tree
Showing 7 changed files with 168 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
__pycache__
.idea
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2018 The Python Packaging Authority

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file added README.md
Empty file.
1 change: 1 addition & 0 deletions __init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
name = "py_mongo_tail"
81 changes: 81 additions & 0 deletions mongo_tail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import time
import json
import sys
import pymongo
import socket
from pymongo.errors import AutoReconnect


class MongoTail(object):
MONGOURL = ''
def __init__(self):

if not self.MONGOURL:
raise Exception('Database url should not empty.')

print(self.MONGOURL)
client = pymongo.MongoClient(self.MONGOURL)
self.oplog = client.local.oplog.rs
self.last_ts = self.oplog.find().sort('$natural', -1)[0]['ts']

def start(self, all, update, insert, delete):
while True:

print("Runnig ..")

# For a regular capped collection CursorType.TAILABLE_AWAIT is the
# only option required to create a tailable cursor. When querying the
# oplog the oplog_replay option enables an optimization to quickly
# find the 'ts' value we're looking for. The oplog_replay option
# can only be used when querying the oplog.

try:
cursor = self.oplog.find({'ts': {'$gt': self.last_ts}, 'op': {
'$in': ['i', 'u', 'd']
}},
cursor_type=pymongo.CursorType.TAILABLE_AWAIT,
oplog_replay=True)

try:

while cursor.alive:
try:
# grab a document if available
doc = cursor.next()

all(doc)

if doc["op"] == "u":
update(doc)

if doc["op"] == "i":
insert(doc)

if doc["op"] == "d":
delete(doc)

# do something interesting with "doc"

except StopIteration:
# thrown when the cursor is out of data, so wait
# for a period for some more data
time.sleep(2)
finally:
cursor.close()

except AutoReconnect:
print("Try to reconnect...")

def all(self):
pass

def update(self):
pass

def insert(self):
pass

def delete(self):
pass


44 changes: 44 additions & 0 deletions mongo_x_tail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
from mongo_tail import MongoTail


class MongoXTail(MongoTail):

MONGOURL = 'mongodb://localhost:27017'


def all(self, doc):
pass

def update(self, doc):
print(doc, 'u')

def insert(self, doc):
print(doc, 'i')

def delete(self, doc):
print(doc, 'd')


#
# def sendmsg(msg):
# try:
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# except socket.error as msg:
# sys.stderr.write("[ERROR] %s\n" % msg[1])
# sys.exit(1)
#
# try:
# sock.connect((HOST, PORT))
# except socket.error as msg:
# sys.stderr.write("[ERROR] %s\n" % msg[1])
# sys.exit(2)
#
# sock.send(str(json.dumps(msg)).encode('utf-8'))
#
# sock.close()


if __name__ == "__main__":

tail = MongoXTail()
tail.start(tail.all, tail.update, tail.insert, tail.delete)
21 changes: 21 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import setuptools

with open("README.md", "r") as fh:
long_description = fh.read()

setuptools.setup(
name="py-mongo-tail-irfan",
version="0.0.1",
author="Irfan",
author_email="[email protected]",
description="Mongo DB tailable cursor",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/mdirfan40/py-mongo-tail",
packages=setuptools.find_packages(),
classifiers=[
"Programming Language :: Python :: 3",
"License :: OSI Approved :: MIT License",
"Operating System :: OS Independent",
],
)

0 comments on commit dc52748

Please sign in to comment.