Skip to content

Commit

Permalink
Merge pull request #324 from angrykoala/dev
Browse files Browse the repository at this point in the history
0.5.4
  • Loading branch information
angrykoala authored Dec 28, 2018
2 parents ced8923 + df1cf89 commit 5d1e917
Show file tree
Hide file tree
Showing 19 changed files with 2,053 additions and 1,398 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
"no-mixed-requires": "warn",
"semi": ["error", "always"],
"arrow-spacing": "warn",
"no-case-declarations": "off",
"vue/jsx-uses-vars": "error",
"vue/valid-v-if": "error",
"vue/require-v-for-key": "off",
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
0.5.4 / 2018-12-28
==================

* Basic Task Scheduler
* Dependencies updated
* Minor refactor and improvement on task timers

0.5.3 / 2018-10-28
==================

Expand Down
66 changes: 41 additions & 25 deletions app/common/task.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
"use strict";

const EventEmitter = require('events');
const os = require('os');
const yerbamate = require('yerbamate');

const TaskStatus = require('../common/task_status');
const taskTimer = require('../common/utils').timer;
const {TaskTimer, InverseTaskTimer} = require('./task_timer');

const TaskEvents = new EventEmitter();
taskTimer(TaskEvents, 1000);
const outputMaxSize = 6000;

class Task {
Expand All @@ -19,20 +16,26 @@ class Task {
this.status = TaskStatus.idle;

this.output = null;
this.beginTime = null;
this.finishTime = null;
this.elapsedTime = null;
this.onTimeUpdate = null;
this.timer = null;

this._scheduleTimeout = null;
}

get elapsedTime() {
if(!this.timer) return null;
return this.timer.elapsedSeconds;
}


run(stdout, done) {
this._clearSchedulerTimeout();
if (this.isRunning()) {
throw new Error("Trying to run task without stopping it first");
}
this.timer = new TaskTimer();
this.output = "";
this.status = TaskStatus.running;
this.beginTime = Date.now();
this.finishTime = null;
this.timer.start();
let executionPath = this.path;
if (!executionPath) executionPath = this._generateDefaultPath();
const onOutput = (out) => {
Expand All @@ -48,29 +51,30 @@ class Task {
(code) => {
if (this.status !== TaskStatus.stopped) this.status = yerbamate.successCode(code) ? TaskStatus.ok : TaskStatus.error;

this.finishTime = Date.now();
this._updateElapsedTime();
TaskEvents.removeListener("time-update", this.onTimeUpdate);
this.timer.stop();
done();
});
this.onTimeUpdate = () => {
this._updateElapsedTime();
};
TaskEvents.on("time-update", this.onTimeUpdate);
this._updateElapsedTime();
}

stop(cb) {
this._clearSchedulerTimeout();
if (this.isRunning()) {
yerbamate.stop(this.proc, cb);
this.status = TaskStatus.stopped;
} else if (cb) cb();
} else{
this.status = TaskStatus.stopped;
if(cb) cb();
}
}

isRunning() {
return this.status === TaskStatus.running;
}

isScheduled() {
return this.status === TaskStatus.scheduled;
}

getData() {
let res = {
title: this.title,
Expand All @@ -80,12 +84,16 @@ class Task {
return res;
}

_updateElapsedTime() {
if (this.beginTime === null) throw new Error("Error, cant update time");
let finishTime = this.finishTime;
if (finishTime === null) finishTime = Date.now();

this.elapsedTime = Math.trunc((finishTime - this.beginTime) / 1000);
schedule(seconds, stdout, onRun, done) {
this.stop(() => {
this.status = TaskStatus.scheduled;
this.timer = new InverseTaskTimer(seconds);
this.timer.start();
this._scheduleTimeout = setTimeout(() => {
onRun();
this.run(stdout, done);
}, seconds * 1000);
});
}

_processCommand() {
Expand All @@ -95,6 +103,14 @@ class Task {
_generateDefaultPath() {
return os.homedir();
}

_clearSchedulerTimeout() {
if(this._scheduleTimeout) {
clearTimeout(this._scheduleTimeout);
this.timer = null;
}
this._scheduleTimeout = null;
}
}

Task.TaskStatus = TaskStatus;
Expand Down
3 changes: 2 additions & 1 deletion app/common/task_status.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ module.exports = {
error: "error",
running: "running",
stopped: "stopped",
ok: "ok"
ok: "ok",
scheduled: "scheduled"
};
71 changes: 71 additions & 0 deletions app/common/task_timer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
"use strict";

const EventEmitter = require('events');


function setTimer(eventEmitter, intervalTime) {
eventEmitter.setMaxListeners(91);
setInterval(() => {
eventEmitter.emit("time-update");
}, intervalTime);
}

const TaskEvents = new EventEmitter();
setTimer(TaskEvents, 1000);

const TIMER_STATE = {
RUNNING: "running",
STOPPED: "stopped"
};

class TaskTimer {
constructor() {
this.elapsedSeconds = null;
this.state = TIMER_STATE.STOPPED;
this._startingTime = null;
this._timerCallback = null;
}

start() {
if(this.state === TIMER_STATE.STARTED) throw new Error("Cannot Start task timer twice");
this._startingTime = new Date();
this._timerCallback = () => { // Required to register and remove listeners
this._updateElapsedTime();
};
TaskEvents.on("time-update", this._timerCallback);
this.state = TIMER_STATE.RUNNING;
this._updateElapsedTime();
}

stop() {
if(this.state === TIMER_STATE.STOPPED) throw new Error("Cannot Stop task timer twice");
TaskEvents.removeListener("time-update", this._timerCallback);
this._updateElapsedTime();
this.state = TIMER_STATE.STOPPED;
}

_updateElapsedTime() {
if (this._startingTime === null) throw new Error("Error, cant update time");
const currentTime = Date.now();
this.elapsedSeconds = Math.trunc((currentTime - this._startingTime) / 1000);
}
}

class InverseTaskTimer extends TaskTimer {
constructor(secondsToFinish) {
super();
this._startingSecondsToFinish = secondsToFinish;
}

_updateElapsedTime() {
if (this._startingTime === null) throw new Error("Error, cant update time");
const currentTime = Date.now();
const elapsedSeconds = Math.trunc((currentTime - this._startingTime) / 1000);
this.elapsedSeconds = Math.max(this._startingSecondsToFinish - elapsedSeconds, 0);
}
}

module.exports = {
TaskTimer: TaskTimer,
InverseTaskTimer: InverseTaskTimer
};
6 changes: 0 additions & 6 deletions app/common/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,5 @@ module.exports = {
},
isTestEnv() {
return process.env.NODE_ENV === "test";
},
timer(eventEmitter, intervalTime) {
eventEmitter.setMaxListeners(91);
setInterval(() => {
eventEmitter.emit("time-update");
}, intervalTime);
}
};
5 changes: 4 additions & 1 deletion app/main/main_window.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ module.exports = class MainWindow {
fullscreenable: false,
webgl: false,
icon: this.iconPath,
frame: true
frame: true,
webPreferences: {
nodeIntegration: true
}
};
if (devWindow) {
winConfig.width += this.userConfig.get(AppConfig.FIELDS.DEVTOOLS_SIZE);
Expand Down
11 changes: 11 additions & 0 deletions app/renderer/api/app_alerts.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,21 @@ class InputAlert extends InteractiveAlert {
}
}

class SchedulerAlert extends InputAlert {
constructor(title, options = {}) {
super(title, "", Object.assign({
confirmButtonText: 'Schedule',
input: 'number',
inputPlaceholder: 'Seconds'
}, options));
}
}

module.exports = {
AppAlert: AppAlert,
DeleteConfirmationAlert: DeleteConfirmationAlert,
InputAlert: InputAlert,
SchedulerAlert: SchedulerAlert,
init: init,
isVisible: swal.isVisible
};
23 changes: 14 additions & 9 deletions app/renderer/api/context_menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,22 +68,27 @@ class TabMenu extends DefaultContextMenu {

class CardMenu extends DefaultContextMenu {
constructor(task) {
let runStopItem = {
label: "Run",
event: "run"
};
const items = [];
if(task.isRunning()) {
runStopItem = {
items.push({
label: "Stop",
event: "stop"
};
});
} else {
items.push({
label: "Run",
event: "run"
});
items.push({
label: "Schedule",
event: "schedule"
});
}
super([
runStopItem,
super(items.concat([
{label: "Delete", event: "delete"},
{label: "Duplicate", event: "duplicate"},
{type: "separator"}
]);
]));
}
}

Expand Down
7 changes: 7 additions & 0 deletions app/renderer/components/navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"use strict";
const EventHandler = require('../utils/event_handler');
const {SchedulerAlert} = require('../api/app_alerts');
const aboutModal = require('../api/about_modal');
const components = {
Expand Down Expand Up @@ -64,6 +65,12 @@ module.exports = {
case "stopSuite":
this.$store.dispatch("stopSuite", this.$store.state.tasks.selectedSuite);
break;
case "scheduleSuite":
const schedulerAlert = new SchedulerAlert("Schedule Task Execution");
schedulerAlert.toggle().then((res) => {
this.$store.dispatch("scheduleSuite", res);
}, () => {});
break;
case "settings":
this.$store.commit("toggleSettings");
break;
Expand Down
3 changes: 1 addition & 2 deletions app/renderer/components/navbar_menu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,11 @@ module.exports = {
const defaultOptions = [{name: "Settings", value: "settings"}, {name: "About", value: "about"}];
if(!this.$store.state.editMode) {
const runModeOptions = [{name: "Run Suite", value: "runSuite"}, {name: "Stop Suite", value: "stopSuite"}];
const runModeOptions = [{name: "Run Suite", value: "runSuite"}, {name: "Stop Suite", value: "stopSuite"}, {name: "Schedule Suite", value: "scheduleSuite"}];
return runModeOptions.concat([{value: "divider"}], defaultOptions);
} else{
return defaultOptions;
}
}
},
methods: {
Expand Down
Loading

0 comments on commit 5d1e917

Please sign in to comment.