Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
XDaemon is an automation tool.

## README
Coming Soon..
- Coming Soon..
- [Project Notion Page](https://cooperative-string-818.notion.site/XDaemon-532da9f667294484810bbc55667dc0a4)
28 changes: 28 additions & 0 deletions xdaemon/cli/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,34 @@ def setup(cls, job_id, schedule, user=None):

logger.info("Setup Successfully")

@classmethod
def enable(cls, job_id, user=None):

user = user or cls.get_current_user()

logger.info("Enabling the Job.")
logger.debug(f'job-details: {job_id}, {user}')

with CronTab(user) as tab:
for job in tab.find_command(cls.get_command(job_id)):
job.enable()

logger.info("Enabled successfully")

@classmethod
def disable(cls, job_id, user=None):

user = user or cls.get_current_user()

logger.info("Disabling the Job.")
logger.debug(f'job-details: {job_id}, {user}')

with CronTab(user) as tab:
for job in tab.find_command(cls.get_command(job_id)):
job.enable(False)

logger.info("Disabled successfully")

@classmethod
def remove(cls, job_id, user=None):

Expand Down
30 changes: 30 additions & 0 deletions xdaemon/cli/lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ def search_job_by_name(cls, job_name):
else:
return None

@classmethod
def enable_job_by_id(cls, job_id):

job_id = int(job_id)

logger.info("Setting the job enabled..")
logger.debug(f"id: {job_id}")

job_data = cls.read_job_data()

if job_id not in job_data[Keys.enabled]:
job_data[Keys.enabled].append(job_id)

cls.write_job_data(job_data)

@classmethod
def disable_job_by_id(cls, job_id):

job_id = int(job_id)

logger.info("Setting the job disabled..")
logger.debug(f"id: {job_id}")

job_data = cls.read_job_data()

if job_id in job_data[Keys.enabled]:
job_data[Keys.enabled].remove(job_id)

cls.write_job_data(job_data)

@classmethod
def remove_job_by_id(cls, job_id):

Expand Down
53 changes: 50 additions & 3 deletions xdaemon/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .lookup import JSONDataStore as DataStore
from .local_logging import setup_logging
from .utils import prettify
from .showbiz import ShowBiz, Keys as ShowBizKeys

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -84,12 +85,14 @@ class Command:
Used in conjunction with logging enabled.

Commands:
show Show the jobs.
show [--active | --inactive] Show the jobs.
setup [-f <jobfile>] Setup a Job.
[Default: ./job.yaml]
test [-f <jobfile>] Test a Job File by executing it.
[Default: ./job.yaml]
remove (--name <name> | --id <id>) Remove a job by name or id.
enable (--name <name> | --id <id>) Enable a job by name or id.
disable (--name <name> | --id <id>) Disable a job by name or id.
execute (--name <name> | --id <id>) Execute a job by name or id.
""" # NOQA: E501

Expand All @@ -99,10 +102,18 @@ def show(opts):
Show already setup jobs.

Usage:
show
show [--active | --inactive]

Options:
--active Show only active jobs.
--inactive Show only inactive jobs.
"""

DataStore.show_jobs()
jobs_type = opts["--active"] and ShowBizKeys.active
jobs_type = jobs_type or (opts["--inactive"] and ShowBizKeys.inactive)
jobs_type = jobs_type or ShowBizKeys.all

ShowBiz.show_jobs(jobs_type=jobs_type)

@staticmethod
def setup(opts):
Expand Down Expand Up @@ -139,6 +150,42 @@ def test(opts):
executor = JobExecutor(job)
executor.execute()

@staticmethod
def enable(opts):
"""
Enable a disabled job.

Usage:
enable (--name <name> | --id <id>)

Options:
--name <name> Name of the Job.
--id <id> ID of the Job.
"""

job_id = opts['--id'] or DataStore.get_id_from_name(opts['--name'])

DataStore.enable_job_by_id(job_id)
Cron.enable(job_id)

@staticmethod
def disable(opts):
"""
Disable an enabled job.

Usage:
disable (--name <name> | --id <id>)

Options:
--name <name> Name of the Job.
--id <id> ID of the Job.
"""

job_id = opts['--id'] or DataStore.get_id_from_name(opts['--name'])

DataStore.disable_job_by_id(job_id)
Cron.disable(job_id)

@staticmethod
def remove(opts):
"""
Expand Down
91 changes: 91 additions & 0 deletions xdaemon/cli/showbiz.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
from enum import Enum
from tabulate import tabulate

from .lookup import JSONDataStore, Keys as DataStoreKeys


class Keys(str, Enum):
all = "all"
active = "active"
inactive = "inactive"


class ShowBiz:

jobs_headers = [
"ID",
"FILE",
"NAME",
"USER",
"STATUS"
]

jobs_headers_filtered = [
"ID",
"FILE",
"NAME",
"USER"
]

@staticmethod
def _get_structured_jobs(jobs_type=Keys.all):
jobs_data = JSONDataStore.read_job_data()

jobs = []
enabled_jobs = jobs_data[DataStoreKeys.enabled]

if jobs_type == Keys.all:
for job_id, job in jobs_data[DataStoreKeys.jobs].items():
jobs.append([
job_id,
job[DataStoreKeys.file],
job[DataStoreKeys.name],
job[DataStoreKeys.user],
"ENABLED" if int(job_id) in enabled_jobs else "DISABLED"
])
else:

jobs_to_show = []

all_jobs = map(
lambda x: int(x),
jobs_data[DataStoreKeys.jobs].keys()
)

if jobs_type == Keys.active:
jobs_to_show = enabled_jobs
else:
jobs_to_show = set(all_jobs).difference(set(enabled_jobs))

for job_id in jobs_to_show:
job = jobs_data[DataStoreKeys.jobs][str(job_id)]
jobs.append([
job_id,
job[DataStoreKeys.file],
job[DataStoreKeys.name],
job[DataStoreKeys.user],
])

return jobs

@classmethod
def show_jobs(cls, jobs_type=Keys.all):

jobs = cls._get_structured_jobs(jobs_type=jobs_type)

if jobs_type == Keys.all:
print(
tabulate(
jobs,
headers=cls.jobs_headers,
tablefmt="fancy_grid"
)
)
else:
print(
tabulate(
jobs,
headers=cls.jobs_headers_filtered,
tablefmt="fancy_grid"
)
)