luci-app: cron.js fixes

This commit is contained in:
gSpot
2021-12-11 23:39:59 +03:00
parent 12afe2a532
commit 04f65fc218
2 changed files with 60 additions and 57 deletions
@@ -6,9 +6,9 @@
return view.extend({ return view.extend({
crontabRegexp: new RegExp( crontabRegexp: new RegExp(
`^(\\*?\\/?(\\d){0,2}\\s){5}${tools.execPath} update(\n)?`, 'gm'), `^(\\*?\\/?(\\d){0,2}\\s){5}${tools.execPath} update`),
currentCrontabContent: null, currentCrontabLines: [],
toDD: function(n){ toDD: function(n){
return String(n).replace(/^(\d)$/, "0$1"); return String(n).replace(/^(\d)$/, "0$1");
@@ -18,12 +18,9 @@ return view.extend({
return s || _('No Shedule'); return s || _('No Shedule');
}, },
pickCronTask: function(content) { stringifyRuabTasks: function(str_array) {
if(!content){ let current_tasks = str_array.filter(s => s.match(this.crontabRegexp));
return; return current_tasks.join('\n');
};
let current_tasks = content.match(this.crontabRegexp) || [];
return current_tasks.join('');
}, },
setCronStatus: function(value) { setCronStatus: function(value) {
@@ -35,16 +32,11 @@ return view.extend({
writeCronFile: function() { writeCronFile: function() {
let btn_cron_add = document.getElementById('btn_cron_add'); let btn_cron_add = document.getElementById('btn_cron_add');
let btn_cron_del = document.getElementById('btn_cron_del'); let btn_cron_del = document.getElementById('btn_cron_del');
let crontab_string = this.currentCrontabLines.join('\n');
if(!this.currentCrontabContent) { return fs.write(tools.crontabFile, crontab_string).then(rc => {
ui.addNotification(null, E('p', _('No changes to save.')));
btn_cron_add.disabled = false;
return;
};
return fs.write(tools.crontabFile, this.currentCrontabContent).then(rc => {
ui.addNotification(null, E('p',_('Changes have been saved.')), 'info'); ui.addNotification(null, E('p',_('Changes have been saved.')), 'info');
this.setCronStatus(this.pickCronTask(this.currentCrontabContent)); this.setCronStatus(this.stringifyRuabTasks(this.currentCrontabLines));
}).then(() => { }).then(() => {
return tools.getInitStatus('cron').then(res => { return tools.getInitStatus('cron').then(res => {
if(!res) { if(!res) {
@@ -61,11 +53,13 @@ return view.extend({
}); });
}, },
delRuabShedules: function() {
this.currentCrontabLines = this.currentCrontabLines.filter(
s => s.match(this.crontabRegexp) ? false : true);
},
delCronSchedule: function(ev) { delCronSchedule: function(ev) {
if(this.currentCrontabContent) { this.delRuabShedules();
this.currentCrontabContent = this.currentCrontabContent.replace(
this.crontabRegexp, "");
};
return this.writeCronFile(); return this.writeCronFile();
}, },
@@ -76,14 +70,21 @@ return view.extend({
let min = document.getElementById('cron_min').value; let min = document.getElementById('cron_min').value;
let task_string = '%s %s %s * * %s update\n'.format( let task_string = '%s %s %s * * %s update\n'.format(
min, min,
(!hour_interval) ? hour : (hour_interval == "1") ? '*' : '*/' + hour_interval, (!hour_interval) ? hour :
(hour_interval || day_interval == "1") ? '*' : '*/' + day_interval, (hour_interval == "1") ?
'*'
:
'*/' + hour_interval,
(hour_interval || day_interval == "1") ?
'*'
:
'*/' + day_interval,
tools.execPath tools.execPath
); );
if(this.currentCrontabContent) {
this.currentCrontabContent = this.currentCrontabContent.replace( this.delRuabShedules();
this.crontabRegexp, "") + task_string; this.currentCrontabLines.push(task_string);
};
return this.writeCronFile(); return this.writeCronFile();
}, },
@@ -106,7 +107,7 @@ return view.extend({
}, },
load: function() { load: function() {
return fs.read(tools.crontabFile).catch(e => { return fs.lines(tools.crontabFile).catch(e => {
ui.addNotification(null, E('p', _('Unable to read the contents') ui.addNotification(null, E('p', _('Unable to read the contents')
+ ': %s [ %s ]'.format( + ': %s [ %s ]'.format(
e.message, tools.crontabFile e.message, tools.crontabFile
@@ -115,22 +116,23 @@ return view.extend({
}, },
render: function(content) { render: function(content) {
this.currentCrontabContent = content; this.currentCrontabLines = content;
let current_task = this.pickCronTask(content); let current_task = this.stringifyRuabTasks(content);
let cron_status = E('textarea', { let cron_status = E('textarea', {
'id': 'cron_status', 'id' : 'cron_status',
'name': 'cron_status', 'name' : 'cron_status',
'style': 'width:100% !important; padding:5px 10px 5px 10px !important; resize:none !important;', 'style' : 'width:100% !important; padding:5px 10px 5px 10px !important; resize:none !important;',
'readonly': 'readonly', 'readonly' : 'readonly',
'wrap': 'off', 'wrap' : 'off',
'rows': 2, 'rows' : 2,
'spellcheck': 'false',
}, this.cronStatusString(current_task)); }, this.cronStatusString(current_task));
let btn_cron_del = E('button', { let btn_cron_del = E('button', {
'class': 'cbi-button btn cbi-button-reset', 'class': 'cbi-button btn cbi-button-reset',
'id': 'btn_cron_del', 'id' : 'btn_cron_del',
'name': 'btn_cron_del', 'name' : 'btn_cron_del',
}, _('Reset')); }, _('Reset'));
btn_cron_del.onclick = ui.createHandlerFn(this, this.delCronSchedule); btn_cron_del.onclick = ui.createHandlerFn(this, this.delCronSchedule);
btn_cron_del.style.visibility = (current_task) ? 'visible' : 'hidden'; btn_cron_del.style.visibility = (current_task) ? 'visible' : 'hidden';
@@ -165,8 +167,10 @@ return view.extend({
layout_append(E('b', {}, _('Interval'))); layout_append(E('b', {}, _('Interval')));
let cron_hour_interval = E('select', let cron_hour_interval = E('select', {
{ 'id': 'cron_hour_interval', 'style': 'width:60px !important; min-width:60px !important' }, [ 'id' : 'cron_hour_interval',
'style': 'width:60px !important; min-width:60px !important',
}, [
E('option', { 'value': '' }, ''), E('option', { 'value': '' }, ''),
E('option', { 'value': '1' }, '∗') E('option', { 'value': '1' }, '∗')
]); ]);
@@ -178,8 +182,10 @@ return view.extend({
layout_append(cron_hour_interval, _('Hour')); layout_append(cron_hour_interval, _('Hour'));
cron_hour_interval.onchange = this.onchangeHourInterval; cron_hour_interval.onchange = this.onchangeHourInterval;
let cron_day_interval = E('select', let cron_day_interval = E('select', {
{ 'id': 'cron_day_interval', 'style': 'width:60px !important; min-width:60px !important' }, 'id' : 'cron_day_interval',
'style': 'width:60px !important; min-width:60px !important',
},
E('option', { 'value': '1' }, '∗') E('option', { 'value': '1' }, '∗')
); );
for(let i = 2; i < 8 ; i++) { for(let i = 2; i < 8 ; i++) {
@@ -193,8 +199,10 @@ return view.extend({
layout_append(E('b', {}, _('Time'))); layout_append(E('b', {}, _('Time')));
let cron_hour = E('select', let cron_hour = E('select', {
{ 'id': 'cron_hour', 'style': 'width:60px !important; min-width:60px !important' }); 'id' : 'cron_hour',
'style': 'width:60px !important; min-width:60px !important',
});
for(let i = 0; i < 24 ; i++) { for(let i = 0; i < 24 ; i++) {
cron_hour.append( cron_hour.append(
E('option', { 'value': String(i) }, this.toDD(i)) E('option', { 'value': String(i) }, this.toDD(i))
@@ -202,8 +210,10 @@ return view.extend({
}; };
layout_append(cron_hour, _('Hour')); layout_append(cron_hour, _('Hour'));
let cron_min = E('select', let cron_min = E('select', {
{ 'id': 'cron_min', 'style': 'width:60px !important; min-width:60px !important' }); 'id' : 'cron_min',
'style': 'width:60px !important; min-width:60px !important',
});
for(let i = 0; i < 60 ; i++) { for(let i = 0; i < 60 ; i++) {
cron_min.append( cron_min.append(
E('option', { 'value': String(i) }, this.toDD(i)) E('option', { 'value': String(i) }, this.toDD(i))
@@ -213,8 +223,8 @@ return view.extend({
let btn_cron_add = E('button', { let btn_cron_add = E('button', {
'class': 'btn cbi-button-save', 'class': 'btn cbi-button-save',
'id': 'btn_cron_add', 'id' : 'btn_cron_add',
'name': 'btn_cron_add' 'name' : 'btn_cron_add',
}, _('Set')); }, _('Set'));
btn_cron_add.onclick = ui.createHandlerFn(this, this.setCronSchedule); btn_cron_add.onclick = ui.createHandlerFn(this, this.setCronSchedule);
layout_append(btn_cron_add); layout_append(btn_cron_add);
@@ -3,13 +3,6 @@
--[[ --[[
(с) 2020 gSpot (https://github.com/gSpotx2f/ruantiblock_openwrt) (с) 2020 gSpot (https://github.com/gSpotx2f/ruantiblock_openwrt)
Модуль поддерживает следующие источники:
https://reestr.rublacklist.net/api/v2/current/csv
https://reestr.rublacklist.net/api/v2/ips/csv
https://raw.githubusercontent.com/zapret-info/z-i/master/dump.csv
https://antifilter.download/list/domains.lst
https://antifilter.download/list/allyouneed.lst
lua == 5.1 lua == 5.1
--]] --]]