-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
checks.py
167 lines (127 loc) · 5.07 KB
/
checks.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import time
from typing import Union
from rich import print
from rich.console import Console
from rich.prompt import Confirm
from resolve_proxy_encoder.helpers import (
app_exit,
get_package_current_commit,
get_remote_latest_commit,
get_rich_logger,
install_rich_tracebacks,
)
from resolve_proxy_encoder.settings.app_settings import Settings
from resolve_proxy_encoder.worker.celery import app as celery_app
install_rich_tracebacks()
logger = get_rich_logger("WARNING")
settings = Settings()
config = settings.user_settings
def check_for_updates(github_url: str, package_name: str) -> Union[str, None]:
"""Compare git origin to local git or package dist for updates
Args:
- github_url(str): origin repo url
- package_name(str): offical package name
Returns:
- none
Raises:
- none
"""
console = Console()
with console.status("[cyan]Checking for updates...[/]\n"):
remote_latest_commit = get_remote_latest_commit(github_url)
package_latest_commit = get_package_current_commit(package_name)
if not remote_latest_commit or not package_latest_commit:
logger.warning("[red]Failed to check for updates[/]")
return
if remote_latest_commit != package_latest_commit:
logger.warning(
"[yellow]Update available.\n"
+ "Fully uninstall and reinstall when possible:[/]\n"
+ '"pip uninstall resolve-proxy-encoder"\n'
+ f'"pip install git+{github_url}"\n'
)
logger.info(f"Remote: {remote_latest_commit}")
logger.info(f"Current: {package_latest_commit}")
else:
# TODO: Fix too much newline padding when all checks pass
# Move the newline padding from these 'success prints' to
# the warning and error logs. Make sure newline padding is consistent.
# labels: bug
print("\n[green]Installation up-to-date :white_check_mark:[/]\n")
return
def check_worker_compatability():
if config["celery_settings"]["disable_worker_compatability_check"]:
logger.warning(
"[yellow]Worker compatability check disabled in user settings![/]\n"
)
time.sleep(2)
return
# TODO: Stop console status spinner from breaking prompts and console logging
# Maybe the spinner doesn't expect console output until we've exited the 'with'?
# labels: bug
console = Console()
with console.status(
"\n[cyan]Fetching online workers for compatability check...[/]\n"
):
# Get online workers and package current commit
online_workers = celery_app.control.inspect().active_queues()
git_full_sha = get_package_current_commit("resolve_proxy_encoder")
if git_full_sha is None:
logger.warning(
"[yellow]Couldn't get local package git commit SHA\n"
+ "Any incompatible workers will not be reported.\n"
+ "[red]CONTINUE AT OWN RISK![/]"
)
return
git_short_sha = git_full_sha[::8]
if online_workers is None:
logger.warning(
"[yellow]No workers found. Can't check compatability.\n"
+ "Jobs may not be received if no compatible workers are available!\n[/]"
+ "[red]CONTINUE AT OWN RISK![/]\n\n"
)
return None
logger.debug(f"Online workers: {online_workers}")
# Get incompatible workers
incompatible_workers = []
for worker, attributes in online_workers.items():
routing_key = attributes[0]["routing_key"]
# Strip whitespace
routing_key = "".join(routing_key.split())
git_short_sha = "".join(git_short_sha.split())
# Compare git sha
if not routing_key == git_short_sha:
incompatible_workers.append(
{
"name": worker,
"host": str(worker).split("@")[1],
"routing_key": routing_key,
}
)
incompatible_hosts = set()
for x in incompatible_workers:
incompatible_hosts.add(x["host"])
# Prompt incompatible workers
if incompatible_workers:
logger.warning(
f"[yellow]Incompatible workers detected!\n"
+ f"{len(incompatible_workers)}/{len(online_workers)} workers across "
+ f"{len(incompatible_hosts)} host(s) will NOT be able to process jobs queued here.\n"
+ f"[green]To fix, update Resolve Proxy Encoder on below hosts to match git commit SHA, [/]'{git_short_sha}':\n"
+ "', '".join(incompatible_hosts)
+ "\n"
)
# If no compatible workers are available
if len(online_workers) == len(incompatible_workers):
logger.error(
"[red]All online workers are incompatible!\n" + "Cannot continue[/]"
)
app_exit(1, -1)
else:
if Confirm.ask("[cyan]Do you wish to continue?[/]"):
print("\n")
return
print("[yellow]Exiting...[/]")
app_exit(1, -1)
print("\n[green]All workers compatible :white_check_mark:[/]\n")
return