Skip to content

Commit 9d44820

Browse files
committed
Added show, enable, disable support.
1 parent 38c9227 commit 9d44820

4 files changed

Lines changed: 199 additions & 3 deletions

File tree

xdaemon/cli/cron.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,34 @@ def setup(cls, job_id, schedule, user=None):
3333

3434
logger.info("Setup Successfully")
3535

36+
@classmethod
37+
def enable(cls, job_id, user=None):
38+
39+
user = user or cls.get_current_user()
40+
41+
logger.info("Enabling the Job.")
42+
logger.debug(f'job-details: {job_id}, {user}')
43+
44+
with CronTab(user) as tab:
45+
for job in tab.find_command(cls.get_command(job_id)):
46+
job.enable()
47+
48+
logger.info("Enabled successfully")
49+
50+
@classmethod
51+
def disable(cls, job_id, user=None):
52+
53+
user = user or cls.get_current_user()
54+
55+
logger.info("Disabling the Job.")
56+
logger.debug(f'job-details: {job_id}, {user}')
57+
58+
with CronTab(user) as tab:
59+
for job in tab.find_command(cls.get_command(job_id)):
60+
job.enable(False)
61+
62+
logger.info("Disabled successfully")
63+
3664
@classmethod
3765
def remove(cls, job_id, user=None):
3866

xdaemon/cli/lookup.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,36 @@ def search_job_by_name(cls, job_name):
131131
else:
132132
return None
133133

134+
@classmethod
135+
def enable_job_by_id(cls, job_id):
136+
137+
job_id = int(job_id)
138+
139+
logger.info("Setting the job enabled..")
140+
logger.debug(f"id: {job_id}")
141+
142+
job_data = cls.read_job_data()
143+
144+
if job_id not in job_data[Keys.enabled]:
145+
job_data[Keys.enabled].append(job_id)
146+
147+
cls.write_job_data(job_data)
148+
149+
@classmethod
150+
def disable_job_by_id(cls, job_id):
151+
152+
job_id = int(job_id)
153+
154+
logger.info("Setting the job disabled..")
155+
logger.debug(f"id: {job_id}")
156+
157+
job_data = cls.read_job_data()
158+
159+
if job_id in job_data[Keys.enabled]:
160+
job_data[Keys.enabled].remove(job_id)
161+
162+
cls.write_job_data(job_data)
163+
134164
@classmethod
135165
def remove_job_by_id(cls, job_id):
136166

xdaemon/cli/main.py

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .lookup import JSONDataStore as DataStore
1010
from .local_logging import setup_logging
1111
from .utils import prettify
12+
from .showbiz import ShowBiz, Keys as ShowBizKeys
1213

1314
logger = logging.getLogger(__name__)
1415

@@ -84,12 +85,14 @@ class Command:
8485
Used in conjunction with logging enabled.
8586
8687
Commands:
87-
show Show the jobs.
88+
show [--active | --inactive] Show the jobs.
8889
setup [-f <jobfile>] Setup a Job.
8990
[Default: ./job.yaml]
9091
test [-f <jobfile>] Test a Job File by executing it.
9192
[Default: ./job.yaml]
9293
remove (--name <name> | --id <id>) Remove a job by name or id.
94+
enable (--name <name> | --id <id>) Enable a job by name or id.
95+
disable (--name <name> | --id <id>) Disable a job by name or id.
9396
execute (--name <name> | --id <id>) Execute a job by name or id.
9497
""" # NOQA: E501
9598

@@ -99,10 +102,18 @@ def show(opts):
99102
Show already setup jobs.
100103
101104
Usage:
102-
show
105+
show [--active | --inactive]
106+
107+
Options:
108+
--active Show only active jobs.
109+
--inactive Show only inactive jobs.
103110
"""
104111

105-
DataStore.show_jobs()
112+
jobs_type = opts["--active"] and ShowBizKeys.active
113+
jobs_type = jobs_type or (opts["--inactive"] and ShowBizKeys.inactive)
114+
jobs_type = jobs_type or ShowBizKeys.all
115+
116+
ShowBiz.show_jobs(jobs_type=jobs_type)
106117

107118
@staticmethod
108119
def setup(opts):
@@ -139,6 +150,42 @@ def test(opts):
139150
executor = JobExecutor(job)
140151
executor.execute()
141152

153+
@staticmethod
154+
def enable(opts):
155+
"""
156+
Enable a disabled job.
157+
158+
Usage:
159+
enable (--name <name> | --id <id>)
160+
161+
Options:
162+
--name <name> Name of the Job.
163+
--id <id> ID of the Job.
164+
"""
165+
166+
job_id = opts['--id'] or DataStore.get_id_from_name(opts['--name'])
167+
168+
DataStore.enable_job_by_id(job_id)
169+
Cron.enable(job_id)
170+
171+
@staticmethod
172+
def disable(opts):
173+
"""
174+
Disable an enabled job.
175+
176+
Usage:
177+
disable (--name <name> | --id <id>)
178+
179+
Options:
180+
--name <name> Name of the Job.
181+
--id <id> ID of the Job.
182+
"""
183+
184+
job_id = opts['--id'] or DataStore.get_id_from_name(opts['--name'])
185+
186+
DataStore.disable_job_by_id(job_id)
187+
Cron.disable(job_id)
188+
142189
@staticmethod
143190
def remove(opts):
144191
"""

xdaemon/cli/showbiz.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
from enum import Enum
2+
from tabulate import tabulate
3+
4+
from .lookup import JSONDataStore, Keys as DataStoreKeys
5+
6+
7+
class Keys(str, Enum):
8+
all = "all"
9+
active = "active"
10+
inactive = "inactive"
11+
12+
13+
class ShowBiz:
14+
15+
jobs_headers = [
16+
"ID",
17+
"FILE",
18+
"NAME",
19+
"USER",
20+
"STATUS"
21+
]
22+
23+
jobs_headers_filtered = [
24+
"ID",
25+
"FILE",
26+
"NAME",
27+
"USER"
28+
]
29+
30+
@staticmethod
31+
def _get_structured_jobs(jobs_type=Keys.all):
32+
jobs_data = JSONDataStore.read_job_data()
33+
34+
jobs = []
35+
enabled_jobs = jobs_data[DataStoreKeys.enabled]
36+
37+
if jobs_type == Keys.all:
38+
for job_id, job in jobs_data[DataStoreKeys.jobs].items():
39+
jobs.append([
40+
job_id,
41+
job[DataStoreKeys.file],
42+
job[DataStoreKeys.name],
43+
job[DataStoreKeys.user],
44+
"ENABLED" if int(job_id) in enabled_jobs else "DISABLED"
45+
])
46+
else:
47+
48+
jobs_to_show = []
49+
50+
all_jobs = map(
51+
lambda x: int(x),
52+
jobs_data[DataStoreKeys.jobs].keys()
53+
)
54+
55+
if jobs_type == Keys.active:
56+
jobs_to_show = enabled_jobs
57+
else:
58+
jobs_to_show = set(all_jobs).difference(set(enabled_jobs))
59+
60+
for job_id in jobs_to_show:
61+
job = jobs_data[DataStoreKeys.jobs][str(job_id)]
62+
jobs.append([
63+
job_id,
64+
job[DataStoreKeys.file],
65+
job[DataStoreKeys.name],
66+
job[DataStoreKeys.user],
67+
])
68+
69+
return jobs
70+
71+
@classmethod
72+
def show_jobs(cls, jobs_type=Keys.all):
73+
74+
jobs = cls._get_structured_jobs(jobs_type=jobs_type)
75+
76+
if jobs_type == Keys.all:
77+
print(
78+
tabulate(
79+
jobs,
80+
headers=cls.jobs_headers,
81+
tablefmt="fancy_grid"
82+
)
83+
)
84+
else:
85+
print(
86+
tabulate(
87+
jobs,
88+
headers=cls.jobs_headers_filtered,
89+
tablefmt="fancy_grid"
90+
)
91+
)

0 commit comments

Comments
 (0)