Skip to content

Commit 65674f4

Browse files
authored
Merge pull request #188 from nextcloud/feat/k8s
feat(k8s)
2 parents 8ce98aa + 3201ed3 commit 65674f4

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

appinfo/info.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,12 @@ See [the nextcloud admin docs](https://docs.nextcloud.com/server/latest/admin_ma
3333
<image-tag>2.6.0</image-tag>
3434
</docker-install>
3535
<system>false</system>
36+
<environment-variables>
37+
<variable>
38+
<name>TASK_POLLING_INTERVAL</name>
39+
<display-name>Task polling interval</display-name>
40+
<description>The interval in which the app will poll for new tasks, in seconds (can be floating point numbers). This will only be used when running in Kubernetes or with Nextcloud below v33. This value defaults to 5 seconds.</description>
41+
</variable>
42+
</environment-variables>
3643
</external-app>
3744
</info>

lib/main.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,20 @@ def log(nc, level, content):
3939
}
4040
app_enabled = Event()
4141
trigger = Event()
42-
CHECK_INTERVAL = 5
42+
43+
try:
44+
CHECK_INTERVAL = float(os.getenv('TASK_POLLING_INTERVAL', '5'))
45+
if CHECK_INTERVAL <= 0:
46+
logger.warning("Invalid TASK_POLLING_INTERVAL env variable, falling back to default 5 seconds")
47+
CHECK_INTERVAL = 5
48+
except (TypeError, ValueError):
49+
logger.warning("Invalid TASK_POLLING_INTERVAL env variable, falling back to default 5 seconds")
50+
CHECK_INTERVAL = 5
51+
4352
CHECK_INTERVAL_WITH_TRIGGER = 5 * 60
4453
CHECK_INTERVAL_ON_ERROR = 10
54+
SHUTDOWN_EVENT_RECEIVED = Event()
55+
SHUTDOWN_CLEAR = Event()
4556

4657
@asynccontextmanager
4758
async def lifespan(_app: FastAPI):
@@ -56,6 +67,10 @@ async def lifespan(_app: FastAPI):
5667
app_enabled.set()
5768
start_bg_task()
5869
yield
70+
print("\nSIGTERM received. Processing last task and stopping to fetch and process new tasks..")
71+
SHUTDOWN_EVENT_RECEIVED.set()
72+
trigger.set()
73+
SHUTDOWN_CLEAR.wait()
5974

6075

6176
APP = FastAPI(lifespan=lifespan)
@@ -80,6 +95,9 @@ def background_thread_task():
8095
sleep(30)
8196
continue
8297

98+
if SHUTDOWN_EVENT_RECEIVED.is_set():
99+
break
100+
83101
current_minute = int(strftime("%M"))
84102
if current_minute % 5 == 0:
85103
# scan dir and load new models every 5 minutes
@@ -136,7 +154,7 @@ def background_thread_task():
136154
nc.providers.task_processing.report_result(task["id"], error_message=str(e))
137155
except (NextcloudException, RequestException) as net_err:
138156
log(nc, LogLvl.INFO, f"Network error in reporting the error: {net_err}")
139-
157+
SHUTDOWN_CLEAR.set()
140158

141159
def start_bg_task():
142160
t = Thread(target=background_thread_task, args=())
@@ -202,6 +220,5 @@ def wait_for_tasks(interval = None):
202220
CHECK_INTERVAL = CHECK_INTERVAL_WITH_TRIGGER
203221
trigger.clear()
204222

205-
206223
if __name__ == "__main__":
207224
run_app("main:APP", log_level="trace")

0 commit comments

Comments
 (0)