Coverage for scheduler/prioritization.py: 100%
22 statements
« prev ^ index » next coverage.py v7.0.4, created at 2024-06-09 19:18 +0000
« prev ^ index » next coverage.py v7.0.4, created at 2024-06-09 19:18 +0000
1"""
2Collection of prioritization functions.
4For compatibility with the |Scheduler|, the prioritization
5functions have to be of type ``Callable[[float, Job, int, int], float]``.
7Author: Jendrik A. Potyka, Fabian A. Preiss
8"""
10import random
12from scheduler.base.job import BaseJobType
13from scheduler.threading.job import Job
16def constant_weight_prioritization(
17 time_delta: float, job: Job, max_exec: int, job_count: int
18) -> float:
19 r"""
20 Interprets the `Job`'s weight as its priority.
22 Return the |Job|'s weight for overdue
23 |Job|\ s, otherwise return zero:
25 .. math::
26 \left(\mathtt{time\_delta},\mathtt{weight}\right)\ {\mapsto}\begin{cases}
27 0 & :\ \mathtt{time\_delta}<0\\
28 \mathtt{weight} & :\ \mathtt{time\_delta}\geq0
29 \end{cases}
31 Parameters
32 ----------
33 time_delta : float
34 The time in seconds that a |Job| is overdue.
35 job : Job
36 The |Job| instance
37 max_exec : int
38 Limits the number of overdue |Job|\ s that can be executed
39 by calling function `Scheduler.exec_jobs()`.
40 job_count : int
41 Number of scheduled |Job|\ s
43 Returns
44 -------
45 float
46 The weight of a |Job| as priority.
47 """
48 _ = max_exec
49 _ = job_count
50 if time_delta < 0:
51 return 0
52 return job.weight
55def linear_priority_function(time_delta: float, job: Job, max_exec: int, job_count: int) -> float:
56 r"""
57 Compute the |Job|\ s default linear priority.
59 Linear |Job| prioritization such that the priority increases
60 linearly with the amount of time that a |Job| is overdue.
61 At the exact time of the scheduled execution, the priority is equal to the
62 |Job|\ s weight.
64 The function is defined as
66 .. math::
67 \left(\mathtt{time\_delta},\mathtt{weight}\right)\ {\mapsto}\begin{cases}
68 0 & :\ \mathtt{time\_delta}<0\\
69 {\left(\mathtt{time\_delta}+1\right)}\cdot\mathtt{weight} & :\ \mathtt{time\_delta}\geq0
70 \end{cases}
72 Parameters
73 ----------
74 time_delta : float
75 The time in seconds that a |Job| is overdue.
76 job : Job
77 The |Job| instance
78 max_exec : int
79 Limits the number of overdue |Job|\ s that can be executed
80 by calling function `Scheduler.exec_jobs()`.
81 job_count : int
82 Number of scheduled |Job|\ s
84 Returns
85 -------
86 float
87 The time dependant priority for a |Job|
88 """
89 _ = max_exec
90 _ = job_count
92 if time_delta < 0:
93 return 0
94 return (time_delta + 1) * job.weight
97def random_priority_function(time: float, job: Job, max_exec: int, job_count: int) -> float:
98 """
99 Generate random priority values from weights.
101 .. warning:: Not suitable for security relevant purposes.
103 The priority generator will return 1 if the random number
104 is lower then the |Job|'s weight, otherwise it will return 0.
105 """
106 _ = time
107 _ = max_exec
108 _ = job_count
109 if random.random() < job.weight: # nosec
110 return 1
111 return 0