Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task scheduler and backup #39

Closed
wants to merge 4 commits into from
Closed
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
104 changes: 63 additions & 41 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions wilfred/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Server(Base):
status = Column(String)

environment_variables = relationship("EnvironmentVariable")
scheduled_tasks = relationship("ScheduledTask")

def __repr__(self):
return f"<Server(id='{self.id}', name='{self.name}', image_uid='{self.image_uid}', port='{self.port}')>"
Expand All @@ -51,6 +52,22 @@ class EnvironmentVariable(Base):
value = Column(String)


class ScheduledTask(Base):
__tablename__ = "scheduled_task"

id = Column(Integer, primary_key=True)
server_id = Column(String, ForeignKey("servers.id"), unique=False)

minute = Column(Integer, nullable=True)
hour = Column(Integer, nullable=True)
day_of_the_month = Column(Integer, nullable=True)
month = Column(Integer, nullable=True)
day_of_the_week = Column(Integer, nullable=True)

task_type = Column(String)
value = Column(String)


Base.metadata.create_all(engine)

Session = sessionmaker()
Expand Down
56 changes: 56 additions & 0 deletions wilfred/scheduler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#################################################################
# #
# Wilfred #
# Copyright (C) 2020, Vilhelm Prytz, <[email protected]> #
# #
# Licensed under the terms of the MIT license, see LICENSE. #
# https://github.com/wilfred-dev/wilfred #
# #
#################################################################

import click

from tabulate import tabulate

from wilfred.database import session, ScheduledTask


class TaskScheduler(object):
def __init__(self, server):
self._server = server

def pretty(self):
data = [u.__dict__ for u in self._server.scheduled_tasks]

headers = {
"minute": click.style("Minute", bold=True),
"hour": click.style("Hour", bold=True),
"day_of_the_month": click.style("Day of the Month", bold=True),
"month": click.style("Month", bold=True),
"day_of_the_week": click.style("Hour", bold=True),
"task_type": click.style("Task Type", bold=True),
"value": click.style("Value", bold=True),
}

return tabulate(data, headers=headers, tablefmt="fancy_grid",)

def create(
self, minute, hour, day_of_the_month, month, day_of_the_week, task_type, value
):
# create
new_task = ScheduledTask(
server_id=self._server.id,
minute=minute,
hour=hour,
day_of_the_month=day_of_the_month,
month=month,
day_of_the_week=day_of_the_week,
task_type=task_type,
value=value,
)
session.add(new_task)

try:
session.commit()
except Exception as e:
raise Exception(f"could not create task, {str(e)}")
20 changes: 20 additions & 0 deletions wilfred/wilfred.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from wilfred.core import is_integer, random_string, check_for_new_releases
from wilfred.migrate import Migrate
from wilfred.server_config import ServerConfig
from wilfred.scheduler import TaskScheduler

if sys.platform.startswith("win"):
click.echo("Wilfred does not support Windows")
Expand Down Expand Up @@ -717,5 +718,24 @@ def _get_variable_occurrences(variable, raw):
click.echo(server_conf.pretty())


@cli.command(short_help="Schedule tasks/scripts for a specific server.")
@click.argument("name")
@click.argument("action", required=False)
def tasks(name, action):
server = session.query(Server).filter_by(name=name.lower()).first()

if not server:
error("Server does not exist", exit_code=1)

_tasks = TaskScheduler(server)

if not action:
click.echo(_tasks.pretty())
exit(0)

if action.lower() == "create":
click.echo("todo")


if __name__ == "__main__":
main()