Skip to content

Commit 263fe35

Browse files
authored
Fix: Don't instantiate KartonProducer when Karton is not enabled (#833)
1 parent 3e37ea9 commit 263fe35

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

mwdb/core/karton.py

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import logging
2+
from typing import Optional
23

34
from flask import g
45
from karton.core import Config as KartonConfig
@@ -18,17 +19,26 @@ class KartonProducer(Producer):
1819
with_service_info = True
1920

2021

21-
karton_producer = KartonProducer(config=KartonConfig(app_config.karton.config_path))
22+
if app_config.mwdb.enable_karton:
23+
karton_producer = KartonProducer(config=KartonConfig(app_config.karton.config_path))
24+
else:
25+
karton_producer = None
2226

2327

24-
def get_karton_producer() -> Producer:
28+
def get_karton_producer() -> Optional[Producer]:
2529
return karton_producer
2630

2731

2832
def send_file_to_karton(file) -> str:
33+
producer = get_karton_producer()
34+
35+
if producer is None:
36+
raise RuntimeError(
37+
"This method should not be called when Karton is not enabled"
38+
)
39+
2940
file_stream = file.open()
3041
try:
31-
producer = get_karton_producer()
3242
feed_quality = g.auth_user.feed_quality
3343
task_priority = (
3444
TaskPriority.NORMAL if feed_quality == "high" else TaskPriority.LOW
@@ -58,6 +68,12 @@ def send_file_to_karton(file) -> str:
5868

5969
def send_config_to_karton(config) -> str:
6070
producer = get_karton_producer()
71+
72+
if producer is None:
73+
raise RuntimeError(
74+
"This method should not be called when Karton is not enabled"
75+
)
76+
6177
task = Task(
6278
headers={
6379
"type": "config",
@@ -79,6 +95,12 @@ def send_config_to_karton(config) -> str:
7995

8096
def send_blob_to_karton(blob) -> str:
8197
producer = get_karton_producer()
98+
99+
if producer is None:
100+
raise RuntimeError(
101+
"This method should not be called when Karton is not enabled"
102+
)
103+
82104
task = Task(
83105
headers={
84106
"type": "blob",
@@ -98,5 +120,12 @@ def send_blob_to_karton(blob) -> str:
98120

99121

100122
def get_karton_state():
101-
karton_state = KartonState(karton_producer.backend)
123+
producer = get_karton_producer()
124+
125+
if producer is None:
126+
raise RuntimeError(
127+
"This method should not be called when Karton is not enabled"
128+
)
129+
130+
karton_state = KartonState(producer.backend)
102131
return karton_state

0 commit comments

Comments
 (0)