Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,12 @@ See [the nextcloud admin docs](https://docs.nextcloud.com/server/latest/admin_ma
<image-tag>2.6.0</image-tag>
</docker-install>
<system>false</system>
<environment-variables>
<variable>
<name>TASK_POLLING_INTERVAL</name>
<display-name>Task polling interval</display-name>
<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>
</variable>
</environment-variables>
</external-app>
</info>
23 changes: 20 additions & 3 deletions lib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,20 @@ def log(nc, level, content):
}
app_enabled = Event()
trigger = Event()
CHECK_INTERVAL = 5

try:
CHECK_INTERVAL = float(os.getenv('TASK_POLLING_INTERVAL', '5'))
if CHECK_INTERVAL <= 0:
logger.warning("Invalid TASK_POLLING_INTERVAL env variable, falling back to default 5 seconds")
CHECK_INTERVAL = 5
except (TypeError, ValueError):
logger.warning("Invalid TASK_POLLING_INTERVAL env variable, falling back to default 5 seconds")
CHECK_INTERVAL = 5

CHECK_INTERVAL_WITH_TRIGGER = 5 * 60
CHECK_INTERVAL_ON_ERROR = 10
SHUTDOWN_EVENT_RECEIVED = Event()
SHUTDOWN_CLEAR = Event()

@asynccontextmanager
async def lifespan(_app: FastAPI):
Expand All @@ -56,6 +67,10 @@ async def lifespan(_app: FastAPI):
app_enabled.set()
start_bg_task()
yield
print("\nSIGTERM received. Processing last task and stopping to fetch and process new tasks..")
SHUTDOWN_EVENT_RECEIVED.set()
trigger.set()
SHUTDOWN_CLEAR.wait()


APP = FastAPI(lifespan=lifespan)
Expand All @@ -80,6 +95,9 @@ def background_thread_task():
sleep(30)
continue

if SHUTDOWN_EVENT_RECEIVED.is_set():
break

current_minute = int(strftime("%M"))
if current_minute % 5 == 0:
# scan dir and load new models every 5 minutes
Expand Down Expand Up @@ -136,7 +154,7 @@ def background_thread_task():
nc.providers.task_processing.report_result(task["id"], error_message=str(e))
except (NextcloudException, RequestException) as net_err:
log(nc, LogLvl.INFO, f"Network error in reporting the error: {net_err}")

SHUTDOWN_CLEAR.set()

def start_bg_task():
t = Thread(target=background_thread_task, args=())
Expand Down Expand Up @@ -202,6 +220,5 @@ def wait_for_tasks(interval = None):
CHECK_INTERVAL = CHECK_INTERVAL_WITH_TRIGGER
trigger.clear()


if __name__ == "__main__":
run_app("main:APP", log_level="trace")
Loading