Skip to content
This repository has been archived by the owner on Apr 11, 2019. It is now read-only.

Commit

Permalink
added basic tasks queue
Browse files Browse the repository at this point in the history
  • Loading branch information
bcho committed Oct 1, 2013
1 parent 85e04a0 commit d45dcf0
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
data/
logs/
*.log

server.nginx.conf
supervisord.conf
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@ itsdangerous==0.23
lxml==3.2.3
nose==1.3.0
pyquery==1.2.4
python-dateutil==2.1
pytz==2013d
redis==2.8.0
requests==1.2.3
rq==0.3.11
rq-scheduler==0.3.6
six==1.4.1
times==0.6.2
wsgiref==0.1.2
4 changes: 4 additions & 0 deletions tasks/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#coding: utf-8

from .book import *
from .queue import *
56 changes: 56 additions & 0 deletions tasks/book.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#coding: utf-8

from flask import current_app

import api
from api import LibraryNotFoundError

from server.app import build
from server.db import db
from server.models import Book

from .queue import book_queue


__all__ = ['update_book', 'update_books']


def update_book(ctrlno):
'''更新书籍信息
:param ctrlno: 书籍控制号
:param database: 数据库
TODO isolate location updating
'''
try:
book_api = api.Book()
book_infos = book_api.get(ctrlno)
except LibraryNotFoundError:
return

app = build()
with app.app_context():
book = Book.query.filter_by(ctrlno=ctrlno).first()
if not book:
return

locations = book_infos.get('locations')
book.location.available = len(
filter(lambda x: x['status'] == u'可供出借', locations))
book.location.total = len(locations)
db.session.commit()

return book_infos


def update_books():
'''更新所有书籍信息'''

app = build()
with app.app_context():
current_app.logger.debug('start updating all books...')
for book in Book.query.all():
book_queue.enqueue(update_book, book.ctrlno)

return True
12 changes: 12 additions & 0 deletions tasks/queue.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#coding: utf-8

from redis import Redis
from rq import Queue


__all__ = ['book_queue', 'user_queue']


_redis_connection = Redis()
book_queue = Queue(name='book', connection=_redis_connection)
user_queue = Queue(name='user', connection=_redis_connection)

0 comments on commit d45dcf0

Please sign in to comment.