-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpa_update_scheduled_task.py
More file actions
executable file
·114 lines (89 loc) · 3.99 KB
/
Copy pathpa_update_scheduled_task.py
File metadata and controls
executable file
·114 lines (89 loc) · 3.99 KB
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
#!/usr/bin/python3.8
"""Update a scheduled task using id and proper specs.
Note that logfile name will change after updating the task but it won't be
created until first execution of the task.
To change interval from hourly to daily use --daily flag and provide --hour.
When --daily flag is not accompanied with --hour, new hour for the task
will be automatically set to current hour.
When changing interval from daily to hourly --hour flag is ignored.
Usage:
pa_update_scheduled_task.py <id> [--command COMMAND]
[--hour HOUR] [--minute MINUTE]
[--disable | --enable | --toggle-enabled]
[--daily | --hourly]
[--quiet | --porcelain]
Options:
-h, --help Print this message
-c, --command COMMAND Changes command to COMMAND (multiword commands
should be quoted)
-o, --hour HOUR Changes hour to HOUR (in 24h format)
-m, --minute MINUTE Changes minute to MINUTE
-d, --disable Disables task
-e, --enable Enables task
-t, --toggle-enabled Toggles enable/disable state
-a, --daily Switches interval to daily (when --hour is not
provided, sets it automatically to current hour)
-u, --hourly Switches interval to hourly (takes precedence
over --hour, i.e. sets hour to None)
-q, --quiet Turns off messages
-p, --porcelain Prints message in easy-to-parse format
Example:
Change command for a scheduled task 42:
pa_update_scheduled_task 42 --command "echo new command"
Change interval of the task 42 from hourly to daily to be run at 10 am:
pa_update_scheduled_task 42 --hour 10
Change interval of the task 42 from daily to hourly and set new minute:
pa_update_scheduled_task 42 --minute 13 --hourly"""
import logging
from datetime import datetime
import os
from docopt import docopt
from snakesay import snakesay
from pythonanywhere import __version__
from pythonanywhere.scripts_commons import ScriptSchema, get_logger, get_task_from_id
os.environ["PYTHONANYWHERE_CLIENT"] = f"helper-scripts/{__version__}"
def main(*, task_id, **kwargs):
logger = get_logger()
if kwargs.pop("hourly"):
kwargs["interval"] = "hourly"
if kwargs.pop("daily"):
kwargs["hour"] = kwargs["hour"] if kwargs["hour"] else datetime.now().hour
kwargs["interval"] = "daily"
def parse_opts(*opts):
candidates = [key for key in opts if kwargs.pop(key, None)]
return candidates[0] if candidates else None
if not parse_opts("quiet"):
logger.setLevel(logging.INFO)
porcelain = parse_opts("porcelain")
enable_opt = parse_opts("toggle_enabled", "disable", "enable")
task = get_task_from_id(task_id)
params = {key: val for key, val in kwargs.items() if val}
if enable_opt:
enabled = {"toggle_enabled": not task.enabled, "disable": False, "enable": True}[
enable_opt
]
params.update({"enabled": enabled})
try:
task.update_schedule(params, porcelain=porcelain)
except Exception as e:
logger.warning(snakesay(str(e)))
if __name__ == "__main__":
schema = ScriptSchema(
{
"<id>": ScriptSchema.id_required,
"--command": ScriptSchema.string,
"--daily": ScriptSchema.boolean,
"--disable": ScriptSchema.boolean,
"--enable": ScriptSchema.boolean,
"--hour": ScriptSchema.hour,
"--hourly": ScriptSchema.boolean,
"--minute": ScriptSchema.minute,
"--porcelain": ScriptSchema.boolean,
"--quiet": ScriptSchema.boolean,
"--toggle-enabled": ScriptSchema.boolean,
}
)
arguments = schema.validate_user_input(
docopt(__doc__), conversions={"id": "task_id", "toggle-": "toggle_"}
)
main(**arguments)