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

Fix #1021 service status #1022

Merged
Merged
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
6 changes: 3 additions & 3 deletions front/src/config/demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -1124,7 +1124,7 @@
"selector": "telegram",
"version": "0.1.0",
"has_message_feature": true,
"status": "NOT_CONFIGURED",
"status": "RUNNING",
"created_at": "2020-04-11T18:41:40.053Z",
"updated_at": "2020-10-30T07:44:07.518Z"
},
Expand Down Expand Up @@ -1168,7 +1168,7 @@
"selector": "zwave",
"version": "0.1.0",
"has_message_feature": false,
"status": "NOT_CONFIGURED",
"status": "RUNNING",
"created_at": "2020-04-11T18:41:40.056Z",
"updated_at": "2020-10-30T07:44:07.594Z"
},
Expand Down Expand Up @@ -1223,7 +1223,7 @@
"selector": "openweather",
"version": "0.1.0",
"has_message_feature": false,
"status": "NOT_CONFIGURED",
"status": "RUNNING",
"created_at": "2020-08-19T13:04:57.309Z",
"updated_at": "2020-10-30T07:44:07.814Z"
},
Expand Down
4 changes: 0 additions & 4 deletions front/src/config/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1015,10 +1015,6 @@
"title": "Stopped",
"detail": "Service stopped by user"
},
"NOT_CONFIGURED": {
"title": "Not configured",
"detail": "Service is not configured"
},
"ERROR": {
"title": "Error",
"detail": "Service failed to start."
Expand Down
4 changes: 0 additions & 4 deletions front/src/config/i18n/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -1015,10 +1015,6 @@
"title": "Arrêté",
"detail": "Service arrêté par un utilisateur"
},
"NOT_CONFIGURED": {
"title": "Non configuré",
"detail": "Service non configuré"
},
"ERROR": {
"title": "En erreur",
"detail": "Service en erreur lors du démarrage."
Expand Down
9 changes: 2 additions & 7 deletions front/src/routes/settings/settings-service/ServiceItem.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { Component } from 'preact';
import { Text, Localizer } from 'preact-i18n';
import { Link } from 'preact-router';
import cx from 'classnames';
import get from 'get-value';

import { RequestStatus } from '../../../utils/consts';
import { SERVICE_STATUS } from '../../../../../server/utils/constants';

const STARTED_STATUS = [SERVICE_STATUS.RUNNING, SERVICE_STATUS.NOT_CONFIGURED];
const STARTED_STATUS = [SERVICE_STATUS.RUNNING];
const HIDDEN_ACTION_STATUS = [SERVICE_STATUS.UNKNOWN, SERVICE_STATUS.DISABLED];

class ServiceItem extends Component {
Expand Down Expand Up @@ -73,11 +72,7 @@ class ServiceItem extends Component {
checked={started}
onClick={this.changeState}
/>
<span
class={cx('custom-switch-indicator', {
'bg-secondary': service.status === SERVICE_STATUS.NOT_CONFIGURED
})}
/>
<span class="custom-switch-indicator" />
</label>
)}
</td>
Expand Down
2 changes: 1 addition & 1 deletion server/api/controllers/service.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module.exports = function ServiceController(gladys) {
* }
*/
async function stop(req, res) {
const service = await gladys.service.stop(req.params.service_name);
const service = await gladys.service.stop(req.params.service_name, null, true);
if (!service) {
res.status(404);
}
Expand Down
2 changes: 1 addition & 1 deletion server/lib/service/service.start.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async function start(name, pod_id = null) {
} catch (e) {
if (e instanceof ServiceNotConfiguredError) {
// If service fails to start due to configuration error, set service status to not configured
status = SERVICE_STATUS.NOT_CONFIGURED;
status = SERVICE_STATUS.RUNNING;
logger.info(`Service ${name} is not configured, so it was not started.`);
} else {
// If service fails to start, set service status to error
Expand Down
3 changes: 0 additions & 3 deletions server/lib/service/service.startAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ async function startAll() {
case SERVICE_STATUS.STOPPED:
logger.info(`Service ${serviceKey} was manually stopped, so it is ignored at startup`);
return false;
case SERVICE_STATUS.LOADING:
logger.warn(`Service ${serviceKey} was not correctly loaded at last startup, it will be avoid for now.`);
return false;
default:
return true;
}
Expand Down
19 changes: 9 additions & 10 deletions server/lib/service/service.stop.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ const db = require('../../models');
* @description Stops one service by name
* @param {string} name - The name of the service.
* @param {string} pod_id - ID of Gladys instance.
* @param {boolean} userAction - Manually stopped?
* @returns {Promise<Object>} Requested service.
* @example
* service.stop('telegram');
*/
async function stop(name, pod_id = null) {
async function stop(name, pod_id = null, userAction = false) {
// Load service from DB
const serviceInDb = await db.Service.findOne({
where: {
Expand All @@ -26,16 +27,14 @@ async function stop(name, pod_id = null) {
let status;
try {
await service.stop();
// Once service started, set service status to stopped
status = SERVICE_STATUS.STOPPED;
} catch (e) {
// If service fails to start, set service status to error
status = SERVICE_STATUS.ERROR;
throw e;
} finally {
// Store service status
serviceInDb.set({ status });
await serviceInDb.save();
// Once service manually stopped, set service status to stopped
if (userAction) {
status = SERVICE_STATUS.STOPPED;
// Store service status
serviceInDb.set({ status });
await serviceInDb.save();
}
}
} catch (e) {
logger.warn(`Unable to stop service ${name}`, e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const db = require('../models');

module.exports = {
up: async () => {
await db.Service.update(
{
status: 'RUNNING',
},
{
where: {
status: ['NOT_CONFIGURED', 'STOPPED'],
},
},
);
},
down: async () => {},
};
6 changes: 3 additions & 3 deletions server/test/lib/service/service.start.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('service.start', () => {
assert.calledOnce(serviceImpl.start);
});

it('should fail starting a service (dut to config), and set status to NOT_CONFIGURED', async () => {
it('should fail starting a service (dut to config), and set status to RUNNING', async () => {
serviceImpl.start = fake.rejects(new ServiceNotConfiguredError('error'));

const result = await service.start(serviceName);
Expand All @@ -86,8 +86,8 @@ describe('service.start', () => {
},
});

expect(serviceInDb.status).eq(SERVICE_STATUS.NOT_CONFIGURED);
expect(result.status).eq(SERVICE_STATUS.NOT_CONFIGURED);
expect(serviceInDb.status).eq(SERVICE_STATUS.RUNNING);
expect(result.status).eq(SERVICE_STATUS.RUNNING);
assert.calledOnce(serviceImpl.start);
});

Expand Down
4 changes: 2 additions & 2 deletions server/test/lib/service/service.startAll.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ describe('service.startAll', () => {
name: serviceName,
},
});
expect(serviceInDb.status).eq(SERVICE_STATUS.LOADING);
assert.notCalled(serviceImpl.start);
expect(serviceInDb.status).eq(SERVICE_STATUS.RUNNING);
assert.calledOnce(serviceImpl.start);
});
});
19 changes: 10 additions & 9 deletions server/test/lib/service/service.stop.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ describe('service.stop', () => {
selector: serviceName,
name: serviceName,
version: '0.1.0',
status: SERVICE_STATUS.UNKNOWN,
};

stateManager = new StateManager();
Expand All @@ -42,7 +43,7 @@ describe('service.stop', () => {
sinon.reset();
});

it('should stop a service, and set status to RUNNING', async () => {
it('should stop a service, and not change status', async () => {
serviceImpl.stop = fake.resolves(null);

const result = await service.stop(serviceName);
Expand All @@ -53,24 +54,24 @@ describe('service.stop', () => {
},
});

expect(serviceInDb.status).eq(SERVICE_STATUS.STOPPED);
expect(result.status).eq(SERVICE_STATUS.STOPPED);
expect(serviceInDb.status).eq(SERVICE_STATUS.UNKNOWN);
expect(result.status).eq(SERVICE_STATUS.UNKNOWN);
assert.calledOnce(serviceImpl.stop);
});

it('should fail stoping a service, and set status to ERROR', async () => {
it('should fail stoping a service, change status to STOPPED', async () => {
serviceImpl.stop = fake.rejects(null);

const result = await service.stop(serviceName);
const result = await service.stop(serviceName, null, true);

const serviceInDb = await db.Service.findOne({
where: {
name: serviceName,
},
});

expect(serviceInDb.status).eq(SERVICE_STATUS.ERROR);
expect(result.status).eq(SERVICE_STATUS.ERROR);
expect(serviceInDb.status).eq(SERVICE_STATUS.STOPPED);
expect(result.status).eq(SERVICE_STATUS.STOPPED);
assert.calledOnce(serviceImpl.stop);
});

Expand All @@ -92,8 +93,8 @@ describe('service.stop', () => {
},
});

expect(serviceInDb.status).eq(SERVICE_STATUS.STOPPED);
expect(result.status).eq(SERVICE_STATUS.STOPPED);
expect(serviceInDb.status).eq(SERVICE_STATUS.LOADING);
expect(result.status).eq(SERVICE_STATUS.LOADING);
assert.calledOnce(serviceImpl.stop);
});
});
1 change: 0 additions & 1 deletion server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const SERVICE_STATUS = {
RUNNING: 'RUNNING',
STOPPED: 'STOPPED',
ERROR: 'ERROR',
NOT_CONFIGURED: 'NOT_CONFIGURED',
};

const SYSTEM_VARIABLE_NAMES = {
Expand Down