Skip to content

Commit

Permalink
Fix #2052: Prevent crash with scheduled trigger (#2054)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Gilles authored Apr 19, 2024
1 parent 8f13438 commit 1154325
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
20 changes: 19 additions & 1 deletion front/src/routes/scene/edit-scene/triggers/ScheduledTrigger.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,28 @@ class TurnOnLight extends Component {
this.resetForm();
if (schedulerType === 'every-month') {
this.props.updateTriggerProperty(this.props.index, 'day_of_the_month', 1);
this.props.updateTriggerProperty(this.props.index, 'time', '12:00');
} else if (schedulerType === 'every-week') {
this.props.updateTriggerProperty(this.props.index, 'days_of_the_week', []);
this.props.updateTriggerProperty(this.props.index, 'days_of_the_week', [
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
'sunday'
]);
this.props.updateTriggerProperty(this.props.index, 'time', '12:00');
} else if (schedulerType === 'every-day') {
this.props.updateTriggerProperty(this.props.index, 'time', '12:00');
} else if (schedulerType === 'custom-time') {
let tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
this.props.updateTriggerProperty(this.props.index, 'time', '12:00');
this.props.updateTriggerProperty(this.props.index, 'date', format(tomorrow, 'yyyy-MM-dd'));
} else if (schedulerType === 'interval') {
this.props.updateTriggerProperty(this.props.index, 'unit', 'second');
this.props.updateTriggerProperty(this.props.index, 'interval', 30);
}
};
handleDateChange = date => {
Expand Down
1 change: 1 addition & 0 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ process.on('SIGINT', () => shutdown('SIGINT'));
// create Gladys object
const gladys = Gladys({
jwtSecret: process.env.JWT_SECRET,
disableDeviceStateAggregation: true,
});

// start Gladys
Expand Down
11 changes: 11 additions & 0 deletions server/lib/scene/scene.addScene.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ function addScene(sceneRaw) {
break;
case 'every-week':
rule.dayOfWeek = trigger.days_of_the_week.map((day) => nodeScheduleDaysOfWeek[day]);
if (rule.dayOfWeek.length === 0) {
rule.dayOfWeek = [
nodeScheduleDaysOfWeek.monday,
nodeScheduleDaysOfWeek.tuesday,
nodeScheduleDaysOfWeek.wednesday,
nodeScheduleDaysOfWeek.thursday,
nodeScheduleDaysOfWeek.friday,
nodeScheduleDaysOfWeek.saturday,
nodeScheduleDaysOfWeek.sunday,
];
}
rule.hour = parseInt(trigger.time.substr(0, 2), 10);
rule.minute = parseInt(trigger.time.substr(3, 2), 10);
rule.second = 0;
Expand Down
12 changes: 10 additions & 2 deletions server/lib/scene/scene.init.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ async function init() {
const scenes = await db.Scene.findAll();
const plainScenes = scenes.map((scene) => {
const plainScene = scene.get({ plain: true });
this.addScene(plainScene);
this.brain.addNamedEntity('scene', plainScene.selector, plainScene.name);
logger.debug(`Loading scene ${plainScene.name}`);
try {
this.addScene(plainScene);
this.brain.addNamedEntity('scene', plainScene.selector, plainScene.name);
logger.debug(`Scene loaded with success`);
} catch (e) {
logger.warn(`Unable to load scene ${plainScene.name}`);
logger.warn(e);
}

return plainScene;
});

Expand Down
32 changes: 32 additions & 0 deletions server/test/lib/scene/scene.init.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const { expect } = require('chai');
const sinon = require('sinon');
const db = require('../../../models');
const SceneManager = require('../../../lib/scene');

const { fake, assert } = sinon;
Expand Down Expand Up @@ -58,4 +59,35 @@ describe('scene.init', () => {
scheduler.scheduleJob.getCall(3).callback();
assert.calledOnceWithExactly(event.emit, 'calendar.check-if-event-is-coming');
});
it('should init scene with failure but not crash', async () => {
await db.Scene.create({
name: 'broken-scene',
icon: 'activity',
triggers: [
{
type: 'time.changed',
scheduler_type: 'every-month',
day_of_the_month: 1,
},
],
actions: [[]],
});
await sceneManager.init();
});
it('should init scheduled scene with with no days of the week specified, but prevent node-schedule from blocking forever', async () => {
await db.Scene.create({
name: 'broken-scene',
icon: 'activity',
triggers: [
{
type: 'time.changed',
scheduler_type: 'every-week',
days_of_the_week: [],
time: '12:00',
},
],
actions: [[]],
});
await sceneManager.init();
});
});

0 comments on commit 1154325

Please sign in to comment.