forked from mlrun/mlrun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule.py
138 lines (111 loc) · 4.09 KB
/
schedule.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Copyright 2018 Iguazio
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from datetime import datetime
from typing import Any, List, Optional, Union
from pydantic import BaseModel
import mlrun.api.utils.helpers
from mlrun.api.schemas.auth import Credentials
from mlrun.api.schemas.object import LabelRecord
class ScheduleCronTrigger(BaseModel):
"""
See this link for help
https://apscheduler.readthedocs.io/en/3.x/modules/triggers/cron.html#module-apscheduler.triggers.cron
"""
year: Optional[Union[int, str]]
month: Optional[Union[int, str]]
day: Optional[Union[int, str]]
week: Optional[Union[int, str]]
day_of_week: Optional[Union[int, str]]
hour: Optional[Union[int, str]]
minute: Optional[Union[int, str]]
second: Optional[Union[int, str]]
start_date: Union[datetime, str] = None
end_date: Union[datetime, str] = None
# APScheduler also supports datetime.tzinfo type, but Pydantic doesn't - so we don't
timezone: Optional[str]
jitter: Optional[int]
@classmethod
def from_crontab(cls, expr, timezone=None):
"""
Create a :class:`~ScheduleCronTrigger` from a standard crontab expression.
See https://en.wikipedia.org/wiki/Cron for more information on the format accepted here.
:param expr: minute, hour, day of month, month, day of week
:param datetime.tzinfo|str timezone: time zone to use for the date/time calculations (
defaults to scheduler timezone)
:return: a :class:`~ScheduleCronTrigger` instance
"""
values = expr.split()
if len(values) != 5:
raise ValueError(
f"Wrong number of fields in crontab expression; got {len(values)}, expected 5"
)
return cls(
minute=values[0],
hour=values[1],
day=values[2],
month=values[3],
day_of_week=values[4],
timezone=timezone,
)
def to_crontab(self) -> str:
"""
Convert the trigger to a crontab expression.
"""
return f"{self.minute} {self.hour} {self.day} {self.month} {self.day_of_week}"
class ScheduleKinds(mlrun.api.utils.helpers.StrEnum):
job = "job"
pipeline = "pipeline"
# this is mainly for testing purposes
local_function = "local_function"
@staticmethod
def local_kinds():
return [
ScheduleKinds.local_function,
]
class ScheduleUpdate(BaseModel):
scheduled_object: Optional[Any]
cron_trigger: Optional[Union[str, ScheduleCronTrigger]]
desired_state: Optional[str]
labels: Optional[dict] = {}
concurrency_limit: Optional[int]
credentials: Credentials = Credentials()
# Properties to receive via API on creation
class ScheduleInput(BaseModel):
name: str
kind: ScheduleKinds
scheduled_object: Any
cron_trigger: Union[str, ScheduleCronTrigger]
desired_state: Optional[str]
labels: Optional[dict] = {}
concurrency_limit: Optional[int]
credentials: Credentials = Credentials()
# the schedule object returned from the db layer
class ScheduleRecord(ScheduleInput):
creation_time: datetime
project: str
last_run_uri: Optional[str]
state: Optional[str]
labels: Optional[List[LabelRecord]]
next_run_time: Optional[datetime]
class Config:
orm_mode = True
# Additional properties to return via API
class ScheduleOutput(ScheduleRecord):
next_run_time: Optional[datetime]
last_run: Optional[dict] = {}
labels: Optional[dict] = {}
credentials: Credentials = Credentials()
class SchedulesOutput(BaseModel):
schedules: List[ScheduleOutput]