Coverage for gws-app/gws/server/spool/runner.py: 46%

26 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-05 13:35 +0200

1"""Queue and run background jobs with the uWSGI spooler.""" 

2 

3import gws 

4import gws.server.uwsgi_module 

5 

6# from uwsgi 

7OK = -2 

8RETRY = -1 

9IGNORE = 0 

10 

11 

12def is_active(): 

13 """Check whether the uWSGI spooler is available. 

14 

15 Returns: 

16 True if the code runs in a uWSGI process. 

17 """ 

18 try: 

19 gws.server.uwsgi_module.load() 

20 return True 

21 except ModuleNotFoundError: 

22 return False 

23 

24 

25def add(job: gws.Job): 

26 """Queue a job for the spooler. 

27 

28 Args: 

29 job: The job to queue. Only its uid is passed to the spooler. 

30 """ 

31 uwsgi = gws.server.uwsgi_module.load() 

32 gws.log.info(f'SPOOL: {job.uid=} added') 

33 d = {b'job_uid': gws.u.to_bytes(job.uid)} 

34 getattr(uwsgi, 'spool')(d) 

35 

36 

37def run(root: gws.Root, env: dict): 

38 """Run a queued job. 

39 

40 Logs an error if the job uid is missing or the job is not found. 

41 

42 Args: 

43 root: Root object. 

44 env: Spooler arguments, as passed by uWSGI, with the job uid in ``b'job_uid'``. 

45 """ 

46 job_uid = env.get(b'job_uid') 

47 if not job_uid: 

48 gws.log.error(f'no "job_uid"') 

49 return 

50 job = root.app.jobMgr.get_job(gws.u.to_str(job_uid)) 

51 if not job: 

52 gws.log.error(f'{job_uid=} not found') 

53 return 

54 root.app.jobMgr.run_job(job)