Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# Imports 

2 

3from .library import Job 

4 

5# Exports 

6 

7__all__ = ( 

8 "Registry", 

9 "schedule", 

10) 

11 

12# Classes 

13 

14 

15class Registry(object): 

16 """Register jobs to be executed on a schedule.""" 

17 

18 def __init__(self, label="global"): 

19 self.label = label 

20 self._registry = dict() 

21 

22 def get_jobs(self, app_name=None): 

23 """Get the jobs that have been registered. 

24 

25 :param app_name: Get the jobs only for the given app name. 

26 :type app_name: str 

27 

28 :rtype: list[superdjango.contrib.scheduler.library.Job] 

29 

30 """ 

31 a = list() 

32 for _app_name, jobs in self._registry.items(): 

33 if app_name and app_name != _app_name: 

34 continue 

35 

36 a += jobs 

37 

38 return a 

39 

40 def register(self, app_name, callback, active=False, at=None, description=None, frequency=None, interval=None, 

41 label=None, **kwargs): 

42 """Register a job. 

43 

44 :param app_name: The name of the app. 

45 :type app_name: str 

46 

47 :param callback: The callable to be executed. 

48 

49 :param active: Indicates the job is to be executed. 

50 :type active: bool 

51 

52 :param at: The specific time at which job should run. 

53 :type at: str 

54 

55 :param description: Optional, additional description of the job. 

56 :type description: str 

57 

58 :param frequency: The frequency upon which the job runs. 

59 :type frequency: str 

60 

61 :param interval: The interval upon which the job runs. 

62 :type interval: int 

63 

64 :param label: The label of the job. Defaults to the name of the callback. 

65 :type label: str 

66 

67 ``kwargs`` are passed as parameters to the callback. 

68 

69 """ 

70 if app_name not in self._registry: 

71 self._registry[app_name] = list() 

72 

73 if not callable(callback): 

74 raise TypeError("Can't register job for %s; it must be callable: %s" % (app_name, callback)) 

75 

76 _label = label or callback.__name__.replace("_", " ") 

77 

78 if description is None and callback.__doc__: 

79 description = callback.__doc__ 

80 

81 job = Job( 

82 _label, 

83 active=active, 

84 app_name=app_name, 

85 at=at, 

86 callback=callback, 

87 description=description, 

88 frequency=frequency, 

89 interval=interval, 

90 **kwargs 

91 ) 

92 

93 self._registry[app_name].append(job) 

94 

95 

96schedule = Registry()