-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
MONGO_HOST = "mongo" | ||
MONGO_PORT = 27017 | ||
MONGO_DATABASE = "core" | ||
REDIS_HOST = "redis" | ||
REDIS_PORT = 6379 | ||
REDIS_DB = 0 | ||
TASK_QUEUE_NAME = "tasks" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import mistune | ||
|
||
from backend.storage.card import CardDAO | ||
from backend.tasks.task import task | ||
|
||
|
||
@task | ||
def parse_card_markup(card_dao: CardDAO, card_id: str): | ||
card = card_dao.get_by_id(card_id) | ||
card.html = _parse_markdown(card.markdown) | ||
card_dao.update(card) | ||
|
||
|
||
_parse_markdown = mistune.Markdown(escape=True, hard_wrap=False) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import functools | ||
from typing import Callable | ||
|
||
from backend.wiring import Wiring | ||
|
||
|
||
def task(func: Callable): | ||
varnames = func.__code__.co_varnames | ||
|
||
@functools.wraps(func) | ||
def result(*args, **kwargs): | ||
wiring: Wiring = kwargs.pop("wiring") | ||
wired_objects_by_name = wiring.__dict__ | ||
for arg_name in varnames: | ||
if arg_name in wired_objects_by_name: | ||
kwargs[arg_name] = wired_objects_by_name[arg_name] | ||
return func(*args, **kwargs) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,7 @@ flask | |
flask-cors | ||
gevent | ||
gunicorn | ||
mistune | ||
pymongo | ||
redis | ||
rq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
from backend.storage.card import Card | ||
from backend.storage.card import Card, CardNotFound | ||
from backend.tasks.parse import parse_card_markup | ||
from backend.wiring import Wiring | ||
|
||
wiring = Wiring() | ||
|
||
wiring.card_dao.create(Card( | ||
slug="helloworld", | ||
name="Hello, world!", | ||
markdown=""" | ||
try: | ||
card = wiring.card_dao.get_by_slug("helloworld") | ||
except CardNotFound: | ||
card = wiring.card_dao.create(Card( | ||
slug="helloworld", | ||
name="Hello, world!", | ||
markdown=""" | ||
This is a hello-world page. | ||
""")) | ||
""")) | ||
|
||
wiring.task_queue.enqueue_call( | ||
parse_card_markup, kwargs={"card_id": card.id}) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import argparse | ||
import uuid | ||
|
||
import rq | ||
from rq.job import Job | ||
|
||
import backend.wiring | ||
|
||
parser = argparse.ArgumentParser(description="Run worker.") | ||
parser.add_argument( | ||
"--burst", | ||
action="store_const", | ||
const=True, | ||
default=False, | ||
help="enable burst mode") | ||
args = parser.parse_args() | ||
|
||
wiring = backend.wiring.Wiring() | ||
|
||
|
||
class JobWithWiring(Job): | ||
|
||
@property | ||
def kwargs(self): | ||
result = dict(super().kwargs) | ||
result["wiring"] = backend.wiring.Wiring() | ||
return result | ||
|
||
@kwargs.setter | ||
def kwargs(self, value): | ||
super().kwargs = value | ||
|
||
|
||
with rq.Connection(wiring.redis): | ||
w = rq.Worker( | ||
queues=[wiring.settings.TASK_QUEUE_NAME], | ||
name=uuid.uuid4().hex, | ||
job_class=JobWithWiring) | ||
w.work(burst=args.burst) |