Skip to content

Commit

Permalink
user propagation fix for system crontab
Browse files Browse the repository at this point in the history
added toggle for jobs
  • Loading branch information
witlox committed Jun 19, 2019
1 parent 48e62e4 commit 308db0a
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 16 deletions.
27 changes: 23 additions & 4 deletions dcron/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from dcron.cron.crontab import CronTab, CronItem
from dcron.datagram.client import broadcast
from dcron.protocols import Packet, group
from dcron.protocols.messages import Kill, ReBalance, Run, Status
from dcron.protocols.messages import Kill, ReBalance, Run, Status, Toggle
from dcron.protocols.udpserializer import UdpSerializer
from dcron.utils import get_ip, check_process, kill_proc_tree

Expand Down Expand Up @@ -58,7 +58,7 @@ def update_status(self, status_message):
self.storage.cluster_status.append(status_message)

def remove_job(self, job):
self.logger.debug("got full remove in buffer ({0}".format(job))
self.logger.debug("got full remove in buffer {0}".format(job))
if job in self.storage.cluster_jobs:
self.logger.debug("removing existing job {0}".format(job))
self.storage.cluster_jobs.remove(job)
Expand All @@ -77,7 +77,7 @@ def remove_job(self, job):
self.logger.warning("defined job {0} not found in cron, but assigned to me!".format(job))

def add_job(self, new_job):
self.logger.debug("got full job in buffer ({0}".format(new_job))
self.logger.debug("got full job in buffer {0}".format(new_job))
job = next(iter([j for j in self.storage.cluster_jobs if j == new_job]), None)
if not job:
if new_job.assigned_to == get_ip():
Expand All @@ -97,8 +97,24 @@ def add_job(self, new_job):
del (self.storage.cluster_jobs[idx])
self.storage.cluster_jobs.append(new_job)

def toggle_job(self, toggle):
self.logger.debug("got full toggle in buffer {0}".format(toggle.job))
job = next(iter([j for j in self.storage.cluster_jobs if j == toggle.job]), None)
if job:
if job.assigned_to == get_ip():
self.logger.info("am owner for job {0}, toggling it".format(job))
job.enable(not job.is_enabled())
if self.user and not job.user:
job.user = self.user
if self.cron and not job.cron:
job.cron = self.cron
self.cron.write()
idx = self.storage.cluster_jobs.index(job)
del (self.storage.cluster_jobs[idx])
self.storage.cluster_jobs.append(job)

async def run(self, run, uuid):
self.logger.debug("got full run in buffer ({0}".format(run.job))
self.logger.debug("got full run in buffer {0}".format(run.job))
job = next(iter([j for j in self.storage.cluster_jobs if j == run.job]), None)
if job and job.assigned_to == get_ip():
self.logger.info("am owner for job {0}".format(job))
Expand Down Expand Up @@ -172,6 +188,9 @@ async def process(self):
elif isinstance(obj, Kill):
self.kill(obj)
self.clean_buffer(uuid)
elif isinstance(obj, Toggle):
self.toggle_job(obj)
self.clean_buffer(uuid)
self.storage.prune()
self.queue.task_done()
if not self.queue.empty():
Expand Down
6 changes: 6 additions & 0 deletions dcron/protocols/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def __init__(self, job):
self.job = job


class Toggle(object):

def __init__(self, job):
self.job = job


class Status(object):

def __init__(self, ip=None, system_load=None):
Expand Down
29 changes: 27 additions & 2 deletions dcron/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

from dcron.cron.cronitem import CronItem
from dcron.datagram.client import broadcast
from dcron.protocols.messages import Kill, Run
from dcron.protocols.messages import Kill, Run, Toggle
from dcron.protocols.udpserializer import UdpSerializer


Expand All @@ -60,6 +60,7 @@ def __init__(self, storage, udp_port):
self.app.add_routes([web.post('/get_job_log', self.get_job_log)])
self.app.add_routes([web.post('/kill_job', self.kill_job)])
self.app.add_routes([web.post('/run_job', self.run_job)])
self.app.add_routes([web.post('/toggle_job', self.toggle_job)])

@aiohttp_jinja2.template('index.html')
async def get(self, request):
Expand Down Expand Up @@ -134,6 +135,25 @@ async def run_job(self, request):

raise web.HTTPFound('/')

async def toggle_job(self, request):
data = await request.post()

self.logger.debug("received toggle request {0}".format(data))

if 'command' not in data or \
'minute' not in data or \
'hour' not in data or \
'dom' not in data or \
'month' not in data or \
'dow' not in data:
return web.Response(status=500, text='not all mandatory fields submitted')

self.logger.debug("broadcasting run result")

broadcast(self.udp_port, UdpSerializer.dump(Toggle(self.generate_cron_item(data))))

raise web.HTTPFound('/')

async def add_job(self, request):
data = await request.post()

Expand All @@ -147,9 +167,14 @@ async def add_job(self, request):
'dow' not in data:
return web.Response(status=500, text='not all mandatory fields submitted via form')

cron_item = self.generate_cron_item(data)

if 'disabled' in data:
cron_item.enable(False)

self.logger.debug("broadcasting add result")

broadcast(self.udp_port, UdpSerializer.dump(self.generate_cron_item(data)))
broadcast(self.udp_port, UdpSerializer.dump(cron_item))

raise web.HTTPFound('/')

Expand Down
19 changes: 19 additions & 0 deletions dcron/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@
elem.style.display = "none";
}
}
function toggleJob(elem) {
let kve = $(elem).data("cmd").split(',');
let request_data = {};
for (let i = 0; i < kve.length; i++) {
request_data[kve[i].split(':')[0]] = kve[i].split(':')[1];
}
$.ajax({
url: '/toggle_job',
data: request_data,
type: 'POST',
success: function (result) {
console.log(result);
}
});
}
function removeJob(elem) {
let kve = $(elem).data("cmd").split(',');
let request_data = {};
Expand Down Expand Up @@ -138,6 +153,10 @@
<td><label for="command">Command</label></td>
<td><input id="command" name="command" /></td>
</tr>
<tr>
<td><label for="disabled">Disabled</label></td>
<td><input id="disabled" type="checkbox" name="disabled" checked value="disabled"></td>
</tr>
</table>
<br/>
<div align="center">
Expand Down
24 changes: 14 additions & 10 deletions dcron/templates/jobstable.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<table id="jobs" align="center">
<thead>
<tr>
<th width="7%">minute</th>
<th width="7%">hour</th>
<th width="7%">day of month</th>
<th width="7%">month</th>
<th width="7%">day of week</th>
<th width="3%">enabled</th>
<th width="6%">minute</th>
<th width="6%">hour</th>
<th width="6%">day of month</th>
<th width="6%">month</th>
<th width="6%">day of week</th>
<th width="50%">command</th>
<th width="10%">assigned to</th>
<th width="2%">log</th>
<th width="2%">run</th>
<th width="2%">tog</th>
<th width="2%">del</th>
</tr>
</thead>
Expand All @@ -21,15 +23,17 @@
{% else %}
<tr>
{% endif %}
<td width="7%">{% if job.minute %}{{ job.minute }}{% else %} * {% endif %}</td>
<td width="7%">{% if job.hour %}{{ job.hour }}{% else %} * {% endif %}</td>
<td width="7%">{% if job.dom %}{{ job.dom }}{% else %} * {% endif %}</td>
<td width="7%">{% if job.month %}{{ job.month }}{% else %} * {% endif %}</td>
<td width="7%">{% if job.dow %}{{ job.dow }}{% else %} * {% endif %}</td>
<td width="3%">{{ job.is_enabled() }}</td>
<td width="6%">{% if job.minute %}{{ job.minute }}{% else %} * {% endif %}</td>
<td width="6%">{% if job.hour %}{{ job.hour }}{% else %} * {% endif %}</td>
<td width="6%">{% if job.dom %}{{ job.dom }}{% else %} * {% endif %}</td>
<td width="6%">{% if job.month %}{{ job.month }}{% else %} * {% endif %}</td>
<td width="6%">{% if job.dow %}{{ job.dow }}{% else %} * {% endif %}</td>
<td width="50%" align="left">{% if job.command %}{{ job.command }}{% else %} * {% endif %}</td>
<td width="10%">{% if job.assigned_to %}{{ job.assigned_to }}{% else %} * {% endif %}</td>
<td width="2%"><button type="button" onclick="jobLog(this);" data-cmd="minute:{{ job.minute }},hour:{{job.hour}},dom:{{ job.dom }},month:{{ job.month }},dow:{{ job.dow }},command:{{ job.command }}">?</button></td>
<td width="2%"><button type="button" onclick="runJob(this);" data-cmd="minute:{{ job.minute }},hour:{{job.hour}},dom:{{ job.dom }},month:{{ job.month }},dow:{{ job.dow }},command:{{ job.command }}">></button></td>
<td width="2%"><button type="button" onclick="toggleJob(this);" data-cmd="minute:{{ job.minute }},hour:{{job.hour}},dom:{{ job.dom }},month:{{ job.month }},dow:{{ job.dow }},command:{{ job.command }}">#</button></td>
<td width="2%"><button type="button" onclick="removeJob(this);" data-cmd="minute:{{ job.minute }},hour:{{job.hour}},dom:{{ job.dom }},month:{{ job.month }},dow:{{ job.dow }},command:{{ job.command }}">-</button></td>
</tr>
{% endfor %}
Expand Down

0 comments on commit 308db0a

Please sign in to comment.