-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinytaskmanager.py
More file actions
executable file
·1311 lines (1113 loc) · 41.7 KB
/
Copy pathtinytaskmanager.py
File metadata and controls
executable file
·1311 lines (1113 loc) · 41.7 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# tinytaskmanager
# Tiny task manager for Linux, MacOS and Unix-like systems.
# Written as a single Python script.
# MIT License
# Copyright (c) 2022 Yuri Escalianti <yuriescl@gmail.com>
# Homepage: https://github.com/yuriescl/tinytaskmanager
from datetime import datetime
from fcntl import LOCK_EX, LOCK_UN, lockf
import io
from io import SEEK_END, SEEK_SET
import json
from multiprocessing.dummy import Pool as ThreadPool
import os
from os.path import abspath, join
from pathlib import Path
import re
import shlex
from shutil import get_terminal_size, rmtree
from signal import SIGINT, SIGKILL, SIGTERM, Signals, signal
from subprocess import DEVNULL, Popen, check_output
from sys import argv, exit, stderr, stdout, version_info
import tempfile
import time
from time import sleep
from typing import Dict, List, Optional, Tuple, Union
if version_info[0] < 3 or version_info[1] < 8:
raise Exception("Python >=3.8 is required to run this program")
LOCK_FILE_NAME = "lock"
CACHE_DIR = Path.home() / ".tinytaskmanager"
LOCK_PATH = Path(CACHE_DIR / LOCK_FILE_NAME)
RESERVED_FILE_NAMES = [LOCK_FILE_NAME]
VERSION = "0.14.0"
BUSY_LOOP_INTERVAL = 0.1 # seconds
TIMESTAMP_FMT = "%Y%m%d%H%M%S"
TERMINATE = False
Task = dict
class TtmException(Exception):
pass
class Tailer(object):
"""
Code obtained from https://github.com/GreatFruitOmsk/tailhead/blob/master/tailhead/__init__.py
Copyright (c) 2012 Mike Thornton
Implements tailing and heading functionality like GNU tail and head
commands.
"""
LINE_TERMINATORS = (b"\r\n", b"\n", b"\r")
def __init__(self, file, read_size=1024, end=False):
if not isinstance(file, io.IOBase) or isinstance(file, io.TextIOBase):
raise ValueError("io object must be in the binary mode")
self.read_size = read_size
self.file = file
if end:
self.file.seek(0, SEEK_END)
def splitlines(self, data):
return re.split(b"|".join(self.LINE_TERMINATORS), data)
def read(self, read_size=-1):
read_str = self.file.read(read_size)
return len(read_str), read_str
def prefix_line_terminator(self, data):
for t in self.LINE_TERMINATORS:
if data.startswith(t):
return t
return None
def suffix_line_terminator(self, data):
for t in self.LINE_TERMINATORS:
if data.endswith(t):
return t
return None
def seek_next_line(self):
where = self.file.tell()
offset = 0
while True:
data_len, data = self.read(self.read_size)
data_where = 0
if not data_len:
break
# Consider the following example: Foo\r | \nBar where " | " denotes current position,
# 'Foo\r' is the read part and '\nBar' is the remaining part.
# We should completely consume terminator "\r\n" by reading one extra byte.
if b"\r\n" in self.LINE_TERMINATORS and data[-1] == b"\r"[0]:
terminator_where = self.file.tell()
terminator_len, terminator_data = self.read(1)
if terminator_len and terminator_data[0] == b"\n"[0]:
data_len += 1
data += b"\n"
else:
self.file.seek(terminator_where)
while data_where < data_len:
terminator = self.prefix_line_terminator(data[data_where:])
if terminator:
self.file.seek(where + offset + data_where + len(terminator))
return self.file.tell()
else:
data_where += 1
offset += data_len
self.file.seek(where + offset)
return -1
def seek_previous_line(self):
where = self.file.tell()
offset = 0
while True:
if offset == where:
break
read_size = self.read_size if self.read_size <= where else where
self.file.seek(where - offset - read_size, SEEK_SET)
data_len, data = self.read(read_size)
# Consider the following example: Foo\r | \nBar where " | " denotes current position,
# '\nBar' is the read part and 'Foo\r' is the remaining part.
# We should completely consume terminator "\r\n" by reading one extra byte.
if b"\r\n" in self.LINE_TERMINATORS and data[0] == b"\n"[0]:
terminator_where = self.file.tell()
if terminator_where > data_len + 1:
self.file.seek(where - offset - data_len - 1, SEEK_SET)
terminator_len, terminator_data = self.read(1)
if terminator_data[0] == b"\r"[0]:
data_len += 1
data = b"\r" + data
self.file.seek(terminator_where)
data_where = data_len
while data_where > 0:
terminator = self.suffix_line_terminator(data[:data_where])
if terminator and offset == 0 and data_where == data_len:
# The last character is a line terminator that finishes current line. Ignore it.
data_where -= len(terminator)
elif terminator:
self.file.seek(where - offset - (data_len - data_where))
return self.file.tell()
else:
data_where -= 1
offset += data_len
if where == 0:
# Nothing more to read.
return -1
else:
# Very first line.
self.file.seek(0)
return 0
def tail(self, lines=10):
self.file.seek(0, SEEK_END)
for i in range(lines):
if self.seek_previous_line() == -1:
break
data = self.file.read()
for t in self.LINE_TERMINATORS:
if data.endswith(t):
# Only terminators _between_ lines should be preserved.
# Otherwise terminator of the last line will be treated as separtaing line and empty line.
data = data[: -len(t)]
break
if data:
return self.splitlines(data)
else:
return []
def head(self, lines=10):
if lines < 0:
self.file.seek(0, SEEK_END)
for i in range(-lines):
if self.seek_previous_line() == -1:
break
else:
self.file.seek(0)
for i in range(lines):
if self.seek_next_line() == -1:
break
end_pos = self.file.tell()
self.file.seek(0)
data = self.file.read(end_pos)
for t in self.LINE_TERMINATORS:
if data.endswith(t):
# Only terminators _between_ lines should be preserved.
# Otherwise terminator of the last line will be treated as separtaing line and empty line.
data = data[: -len(t)]
break
if data:
return self.splitlines(data)
else:
return []
def follow(self):
trailing = True
while True:
where = self.file.tell()
if where > os.fstat(self.file.fileno()).st_size:
# File was truncated.
where = 0
self.file.seek(where)
line = self.file.readline()
if line:
if trailing and line in self.LINE_TERMINATORS:
# This is just the line terminator added to the end of the file
# before a new line, ignore.
trailing = False
continue
terminator = self.suffix_line_terminator(line)
if terminator:
line = line[: -len(terminator)]
trailing = False
yield line
else:
trailing = True
self.file.seek(where)
yield None
class AtomicOpen:
"""https://stackoverflow.com/a/46407326/3705710"""
def __init__(self, path, *args, noop=False, **kwargs):
if noop is False:
self.file = open(path, *args, **kwargs)
self.lock_file(self.file)
self.noop = noop
@staticmethod
def lock_file(f):
if f.writable():
lockf(f, LOCK_EX)
@staticmethod
def unlock_file(f):
if f.writable():
lockf(f, LOCK_UN)
def __enter__(self, *args, **kwargs):
if self.noop is False:
return self.file
def __exit__(self, exc_type=None, exc_value=None, traceback=None):
if self.noop is False:
self.file.flush()
os.fsync(self.file.fileno())
self.unlock_file(self.file)
self.file.close()
if exc_type is not None:
return False
else:
return True
class bcolors:
"""https://stackoverflow.com/a/287944/3705710"""
HEADER = "\033[95m"
OKBLUE = "\033[94m"
OKCYAN = "\033[96m"
OKGREEN = "\033[92m"
WARNING = "\033[93m"
LIGHTGREY = "\033[0;37m"
FAIL = "\033[91m"
ENDC = "\033[0m"
BOLD = "\033[1m"
UNDERLINE = "\033[4m"
##############
# ARG PARSING
def arg_requires_value(arg: str, option: Optional[str] = None) -> bool:
def dashes(a: str):
return "-" if len(a) == 1 else "--"
if arg in ["cache-dir"]:
return True
if arg in ["h", "help"]:
return False
if option is None:
if arg in ["version"]:
return False
elif option == "run":
if arg in ["s", "shell", "split-output"]:
return False
if arg in ["n", "name"]:
return True
elif option == "start":
pass
elif option == "stop":
if arg in ["k", "kill"] + signals_list():
return False
elif option == "rm":
if arg in ["a", "all"]:
return False
elif option == "ls":
if arg in ["a", "all"]:
return False
elif option == "logs":
if arg in ["f", "follow", "head"]:
return False
raise TtmException(f"Unrecognized argument {dashes(arg)}{arg}")
def is_value_next(args: List[str], pos: int) -> bool:
return pos + 1 < len(args) and not args[pos + 1].startswith("-")
def parse_args(
args_to_parse: List[str],
) -> Tuple[Dict, Optional[str], Dict, Optional[List[str]]]:
args = args_to_parse[1:]
global_args: Dict[str, Union[str, bool]] = {}
option = None
pos = 0
while True:
if pos >= len(args):
break
current_arg = args[pos]
if current_arg in ["run", "start", "stop", "rm", "ls", "logs"]:
option = current_arg
pos += 1
break
elif current_arg.startswith("--"):
current_arg = current_arg[2:]
if arg_requires_value(current_arg, option):
if not is_value_next(args, pos):
raise TtmException(f"Argument --{current_arg} requires a value")
global_args[current_arg] = args[pos + 1]
pos += 2
continue
else:
global_args[current_arg] = True
pos += 1
continue
elif current_arg.startswith("-"):
current_arg = current_arg[1:]
if len(current_arg) == 1:
if arg_requires_value(current_arg, option):
if not is_value_next(args, pos):
raise TtmException(f"Argument -{current_arg} requires a value")
global_args[current_arg] = args[pos + 1]
pos += 2
continue
else:
global_args[current_arg] = True
pos += 1
continue
else:
for letter in current_arg:
if arg_requires_value(letter, option):
raise TtmException(
f"Argument -{letter} cannot be grouped with other arguments"
)
global_args[letter] = True
pos += 1
continue
else:
raise TtmException(f"Unrecognized option {current_arg}")
pos += 1
option_args: Dict[str, Union[str, bool]] = {}
command = None
if option is not None:
if pos >= len(args) and option not in ["ls"]:
raise TtmException(f"Missing arguments for option '{option}'")
while True:
if pos >= len(args):
break
current_arg = args[pos]
if current_arg.startswith("--"):
current_arg = current_arg[2:]
if arg_requires_value(current_arg, option):
if not is_value_next(args, pos):
raise TtmException(f"Argument --{current_arg} requires a value")
option_args[current_arg] = args[pos + 1]
pos += 2
continue
else:
option_args[current_arg] = True
pos += 1
continue
elif current_arg.startswith("-"):
current_arg = current_arg[1:]
if len(current_arg) == 1:
if arg_requires_value(current_arg, option):
if not is_value_next(args, pos):
raise TtmException(
f"Argument -{current_arg} requires a value"
)
option_args[current_arg] = args[pos + 1]
pos += 2
continue
else:
option_args[current_arg] = True
pos += 1
continue
else:
for letter in current_arg:
if arg_requires_value(letter, option) and not is_value_next(
args, pos
):
raise TtmException(
f"Argument -{letter} cannot be grouped with other arguments"
)
option_args[letter] = True
pos += 1
continue
else:
command = args[pos:]
break
return global_args, option, option_args, command
##################
# FILE OPERATIONS
def init_cache_dir(cache_dir: Optional[Union[str, Path]]):
global CACHE_DIR
global LOCK_PATH
if cache_dir is not None:
CACHE_DIR = Path(cache_dir)
os.makedirs(CACHE_DIR, exist_ok=True)
LOCK_FILE_NAME = "lock"
LOCK_PATH = Path(CACHE_DIR / LOCK_FILE_NAME)
LOCK_PATH.touch(exist_ok=True)
def get_task_label(task: Task):
if task["name"] is not None:
return f"{task['name']}-{task['id']}"
else:
return task["id"]
def parse_task_id_or_name(task_name_or_id: str) -> Tuple[Optional[str], Optional[str]]:
try:
task_id = str(int(task_name_or_id))
name = None
except (ValueError, TypeError):
task_id = None
name = task_name_or_id
return task_id, name
def create_pidfile(task: Task):
if task.get("pid") is not None:
if task.get("pidfile"):
Path(task["pidfile"]).unlink(missing_ok=True)
fh, task["pidfile"] = tempfile.mkstemp()
with os.fdopen(fh, "w") as f:
f.write(task["pid"])
def create_task_cache(task: Task, split_output=False) -> Task:
dir_name = get_task_label(task)
dir_path = CACHE_DIR / dir_name
os.makedirs(dir_path, exist_ok=True)
filepath = dir_path / "task.json"
timestamp = datetime.now().strftime(TIMESTAMP_FMT)
if split_output:
stdout_path = dir_path / f"{dir_name}-{timestamp}.out"
stderr_path = dir_path / f"{dir_name}-{timestamp}.err"
task.update(
{
"stdout": str(stdout_path),
"stderr": str(stderr_path),
"started_at": timestamp,
}
)
else:
logs_path = dir_path / f"{dir_name}-{timestamp}.log"
task.update(
{
"logs": str(logs_path),
"started_at": timestamp,
}
)
create_pidfile(task)
with open(filepath, "w") as f:
task_to_dump = dict(task)
task_to_dump.pop("pid", None)
json.dump(task_to_dump, f)
return task
def update_task_cache(task: Task):
dir_name = get_task_label(task)
dir_path = CACHE_DIR / dir_name
filepath = dir_path / "task.json"
create_pidfile(task)
with open(filepath, "w") as f:
task_to_dump = dict(task)
task_to_dump.pop("pid", None)
json.dump(task_to_dump, f)
def is_task_running(task: Task) -> bool:
output = check_output(["ps", "-ax", "-o", "pid,args"], start_new_session=True)
for line in output.splitlines():
decoded = line.decode().strip()
ps_pid, cmdline = decoded.split(" ", 1)
if task.get("pid") is not None and ps_pid == task["pid"]:
return True
return False
def get_task_from_cache_file(cache_file_path: str):
with open(cache_file_path) as f:
task = json.load(f)
if task.get("pidfile") and Path(task["pidfile"]).exists():
with open(task["pidfile"], "r") as f:
task["pid"] = f.read()
return task
def find_task_by_name(name: str) -> Optional[Task]:
for filename in os.listdir(CACHE_DIR):
if filename not in RESERVED_FILE_NAMES and filename.split("-")[0] == name:
path = abspath(join(CACHE_DIR, filename, "task.json"))
return get_task_from_cache_file(path)
return None
def find_task_by_id(task_id: str) -> Optional[Dict]:
for filename in os.listdir(CACHE_DIR):
if filename in RESERVED_FILE_NAMES:
continue
try:
filename_split = filename.split("-")
if len(filename_split) == 1:
filename_task_id = filename_split[0]
else:
filename_task_id = filename_split[1]
if filename_task_id == task_id:
path = abspath(join(CACHE_DIR, filename, "task.json"))
return get_task_from_cache_file(path)
except IndexError:
pass
return None
def delete_pidfile(task: Task):
if task.get("pidfile"):
Path(task["pidfile"]).unlink(missing_ok=True)
def remove_task_by_name(name: str):
with AtomicOpen(LOCK_PATH):
for filename in os.listdir(CACHE_DIR):
filename_split = filename.split("-")
if len(filename_split) == 1:
continue
else:
filename_task_name = filename_split[0]
if filename_task_name == name:
task = find_task_by_name(name)
if task is None:
raise TtmException("Failed to find task by name")
if is_task_running(task):
raise TtmException(
"Cannot remove task while it's running.\n"
"To stop it, run:\n"
f"tinytaskmanager stop {name}"
)
dir_path = abspath(join(CACHE_DIR, filename))
rmtree(dir_path)
delete_pidfile(task)
return
raise TtmException(f"No task with name {name}")
def remove_task_by_id(task_id: str):
with AtomicOpen(LOCK_PATH):
for filename in os.listdir(CACHE_DIR):
try:
filename_split = filename.split("-")
if len(filename_split) == 1:
filename_task_id = filename_split[0]
else:
filename_task_id = filename_split[1]
if filename_task_id == task_id:
task = find_task_by_id(task_id)
if task is None:
raise TtmException("Failed to find task by id")
if is_task_running(task):
raise TtmException(
"Cannot remove task while it's running.\n"
"To stop it, run:\n"
f"tinytaskmanager stop {task_id}"
)
dir_path = abspath(join(CACHE_DIR, filename))
rmtree(dir_path)
delete_pidfile(task)
return
except IndexError:
pass
raise TtmException(f"No task with ID {task_id}")
def generate_id():
existing_ids = []
for filename in os.listdir(CACHE_DIR):
try:
existing_ids.append(str(int(filename)))
except ValueError:
try:
existing_ids.append(str(int(filename.split("-")[1])))
except (ValueError, IndexError):
pass
for i in range(1, 10000):
str_i = str(i)
if str_i not in existing_ids:
return str_i
raise TtmException("Failed to generated task ID")
def get_child_pids(parent_pid: int):
output = check_output(["ps", "-ax", "-o", "pid,ppid"], start_new_session=True)
ppid = str(parent_pid)
child_pids = []
for line in output.splitlines():
decoded = line.decode().strip()
ps_child_pid, ps_ppid = decoded.split(None, 1)
if ps_ppid == ppid:
child_pids.append(int(ps_child_pid))
return child_pids
def kill_recursively(pid: int, sig: int):
for child_pid in get_child_pids(pid):
kill_recursively(child_pid, sig)
os.kill(pid, sig)
#############
# OPERATIONS
def run(
command: List[str],
name: Optional[str] = None,
split_output=False,
shell=False,
) -> Task:
with AtomicOpen(LOCK_PATH):
if name is not None:
task = find_task_by_name(name)
if task:
if is_task_running(task):
raise TtmException(
f"Task {name} is already running with PID {task['pid']}"
)
raise TtmException(
f"Task {name} already exists and it's not running.\n"
"To remove it, run:\n"
f"tinytaskmanager rm {name}"
)
task = {
"id": generate_id(),
"name": name,
"cwd": os.getcwd(),
"command": command,
"shell": shell,
}
if split_output:
task = create_task_cache(task, split_output=split_output)
stdout_path = task["stdout"]
stderr_path = task["stderr"]
logs_path = ""
else:
task = create_task_cache(task, split_output=split_output)
stdout_path = ""
stderr_path = ""
logs_path = task["logs"]
if split_output:
with open(stdout_path, "wb") as out:
with open(stderr_path, "wb") as err:
proc = Popen(
build_cmd(command, shell),
shell=shell,
cwd=task["cwd"],
stdin=DEVNULL,
stdout=out,
stderr=err,
start_new_session=True,
)
else:
with open(logs_path, "wb") as output:
proc = Popen(
build_cmd(command, shell),
shell=shell,
cwd=task["cwd"],
stdin=DEVNULL,
stdout=output,
stderr=output,
start_new_session=True,
)
task["pid"] = str(proc.pid)
update_task_cache(task)
return task
def start_task(task_id: Optional[str] = None, name: Optional[str] = None):
with AtomicOpen(LOCK_PATH):
if name is not None:
task = find_task_by_name(name)
if task is not None:
if is_task_running(task):
raise TtmException(
f"Task {name} is already running with PID {task['pid']}"
)
else:
raise TtmException(f"No task with name {name}")
elif task_id is not None:
task = find_task_by_id(task_id)
if task is not None:
if is_task_running(task):
raise TtmException(
f"Task with ID {task_id} is already running with PID {task['pid']}"
)
else:
raise TtmException(f"No task with ID {task_id}")
else:
raise ValueError("Either task_id or name must be set")
if task["name"] is not None:
dir_name = f"{task['name']}-{task['id']}"
else:
dir_name = task["id"]
dir_path = CACHE_DIR / dir_name
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
if task.get("stdout") is not None:
task["stdout"] = str(dir_path / f"{dir_name}-{timestamp}.out")
task["stderr"] = str(dir_path / f"{dir_name}-{timestamp}.err")
with open(task["stdout"], "wb") as out:
with open(task["stderr"], "wb") as err:
proc = Popen(
build_cmd(task["command"], task["shell"]),
shell=task["shell"],
cwd=task["cwd"],
stdin=DEVNULL,
stdout=out,
stderr=err,
start_new_session=True,
)
else:
task["logs"] = str(dir_path / f"{dir_name}-{timestamp}.log")
with open(task["logs"], "wb") as output:
proc = Popen(
build_cmd(task["command"], task["shell"]),
shell=task["shell"],
cwd=task["cwd"],
stdin=DEVNULL,
stdout=output,
stderr=output,
start_new_session=True,
)
task["pid"] = str(proc.pid)
task["started_at"] = timestamp
update_task_cache(task)
def stop_task(
task_id: Optional[str] = None, name: Optional[str] = None, sig: int = SIGTERM
):
with AtomicOpen(LOCK_PATH):
if name is not None:
task = find_task_by_name(name)
if task is not None:
if not is_task_running(task):
raise TtmException(f"Task {name} is not running")
else:
raise TtmException(f"No task with name {name}")
elif task_id is not None:
task = find_task_by_id(task_id)
if task is not None:
if not is_task_running(task):
raise TtmException(f"Task with ID {task_id} is not running")
else:
raise TtmException(f"No task with ID {task_id}")
else:
raise ValueError("Either task_id or name must be set")
# We kill and busy wait outside the above file lock for better parallel performance
kill_recursively(int(task["pid"]), sig)
while True:
# TODO add timeout
with AtomicOpen(LOCK_PATH):
if not is_task_running(task):
delete_pidfile(task)
break
sleep(BUSY_LOOP_INTERVAL)
def remove_all_tasks():
with AtomicOpen(LOCK_PATH):
for filename in os.listdir(CACHE_DIR):
if TERMINATE:
return
if filename in RESERVED_FILE_NAMES:
continue
path = abspath(join(CACHE_DIR, filename, "task.json"))
try:
task = get_task_from_cache_file(path)
if is_task_running(task):
print_error(f"Task {task['id']}: cannot remove while it's running")
else:
dir_path = abspath(join(CACHE_DIR, filename))
rmtree(dir_path)
except (NotADirectoryError, FileNotFoundError, ValueError):
pass
def rm(task_name_or_id: Optional[str], rm_all=False) -> bool:
try:
if rm_all:
remove_all_tasks()
else:
if task_name_or_id is None:
raise ValueError("task_name_or_id is None")
task_id, name = parse_task_id_or_name(task_name_or_id)
if task_id is not None:
remove_task_by_id(task_id)
elif name is not None:
remove_task_by_name(name)
except TtmException as e:
print_error(str(e))
return False
return True
def logs(task_name_or_id: str, follow=False, head=False):
def print_lines(lines):
if isinstance(lines, list):
for line in lines:
stdout.buffer.write(line)
stdout.buffer.write("\n".encode())
elif isinstance(lines, bytes):
stdout.buffer.write(lines)
stdout.buffer.write("\n".encode())
stdout.buffer.flush()
if follow and head:
raise TtmException("--follow and --head cannot be used together")
task_id, name = parse_task_id_or_name(task_name_or_id)
if task_id is not None:
task = find_task_by_id(task_id)
if task is None:
raise TtmException(f"No task with ID {task_id}")
elif name is not None:
task = find_task_by_name(name)
if task is None:
raise TtmException(f"No task with name {name}")
else:
raise ValueError("task_id and name are None")
logs_path = task.get("logs")
if logs_path is None:
raise TtmException(
"Task was created using --split-output, use 'stdout' or 'stderr' instead of 'logs'"
)
line_count = 15
if not head and follow:
with open(logs_path, "rb") as file:
print_grey(f"{logs_path} last {line_count} lines:")
print_lines(Tailer(file).tail(lines=line_count))
with open(logs_path, "rb") as file:
if head:
print_grey(f"{logs_path} first {line_count} lines:")
print_lines(Tailer(file).head(lines=line_count))
elif follow:
print_grey(f"{logs_path} followed tail:")
for line in Tailer(file, end=True).follow():
if line is None:
time.sleep(0.01)
continue
print_lines(line)
else:
print_grey(f"{logs_path} last {line_count} lines:")
print_lines(Tailer(file).tail(lines=line_count))
print_lines([])
def start(task_name_or_id: str) -> bool:
task_id, name = parse_task_id_or_name(task_name_or_id)
try:
start_task(task_id=task_id, name=name)
return True
except TtmException as e:
print_error(str(e))
return False
def stop(task_name_or_id: str, sig: int):
task_id, name = parse_task_id_or_name(task_name_or_id)
try:
stop_task(task_id=task_id, name=name, sig=sig)
return True
except TtmException as e:
print_error(str(e))
return False
def ls(ls_all=False, command: Optional[List[str]] = None):
tasks = []
with AtomicOpen(LOCK_PATH):
for filename in os.listdir(CACHE_DIR):
if filename in RESERVED_FILE_NAMES:
continue
path = abspath(join(CACHE_DIR, filename, "task.json"))
force_list = False
if command:
for task_name_or_id in command:
task_id, name = parse_task_id_or_name(task_name_or_id)
filename_split = filename.split("-")
if task_id in filename_split or name in filename_split:
force_list = True
if not force_list:
continue
try:
task = get_task_from_cache_file(path)
task["started_at"] = datetime.strptime(
task["started_at"], TIMESTAMP_FMT
)
if is_task_running(task):
diff = datetime.now() - task["started_at"]
task["uptime"] = format_seconds(int(diff.total_seconds()))
tasks.append(task)
elif ls_all or force_list:
task["pid"] = "-"
task["uptime"] = "-"
tasks.append(task)
except (NotADirectoryError, FileNotFoundError, ValueError):
pass
name_len_max = 4
for task in tasks:
if task["name"] is not None and len(task["name"]) > name_len_max:
name_len_max = len(task["name"])
tasks = sorted(tasks, key=lambda d: d["started_at"])