Skip to content

Commit

Permalink
Add taskChanged API subscription and utilize it in UI
Browse files Browse the repository at this point in the history
  • Loading branch information
rocketeerbkw committed Dec 10, 2018
1 parent 8ab8b83 commit 3597c24
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
2 changes: 2 additions & 0 deletions services/api/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const {
addTask,
deleteTask,
updateTask,
taskSubscriber,
} = require('./resources/task/resolvers');

const {
Expand Down Expand Up @@ -266,6 +267,7 @@ const resolvers /* : { [string]: ResolversObj | typeof GraphQLDate } */ = {
Subscription: {
backupChanged: backupSubscriber,
deploymentChanged: deploymentSubscriber,
taskChanged: taskSubscriber,
},
Date: GraphQLDate,
};
Expand Down
6 changes: 6 additions & 0 deletions services/api/src/resources/task/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
TASK: {
ADDED: 'api.event.task.added',
UPDATED: 'api.event.task.updated',
}
};
27 changes: 22 additions & 5 deletions services/api/src/resources/task/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const R = require('ramda');
const { sendToLagoonLogs } = require('@lagoon/commons/src/logs');
const { createTaskTask } = require('@lagoon/commons/src/tasks');
const { pubSub, createEnvironmentFilteredSubscriber } = require('../../clients/pubSub');
const esClient = require('../../clients/esClient');
const sqlClient = require('../../clients/sqlClient');
const {
Expand All @@ -16,6 +17,7 @@ const {
const Sql = require('./sql');
const projectSql = require('../project/sql');
const environmentSql = require('../environment/sql');
const EVENTS = require('./events');

/* ::
Expand Down Expand Up @@ -93,7 +95,9 @@ const getTasksByEnvironmentId = async (

const rows = await query(sqlClient, prep({ eid }));

return rows.map(row => injectLogs(row));
const newestFirst = R.sort(R.descend(R.prop('created')), rows);

return newestFirst.map(row => injectLogs(row));
};

const getTaskByRemoteId = async (
Expand Down Expand Up @@ -190,11 +194,13 @@ const addTask = async (
);

let rows = await query(sqlClient, Sql.selectTask(insertId));
const taskData = R.prop(0, rows);
const taskData = await injectLogs(R.prop(0, rows));

pubSub.publish(EVENTS.TASK.ADDED, taskData);

// Allow creating task data w/o executing the task
if (role === 'admin' && execute === false) {
return injectLogs(taskData);
return taskData;
}

rows = await query(sqlClient, environmentSql.selectEnvironmentById(taskData.environment));
Expand All @@ -216,7 +222,7 @@ const addTask = async (
);
}

return injectLogs(taskData);
return taskData;
};

const deleteTask = async (
Expand Down Expand Up @@ -321,16 +327,27 @@ const updateTask = async (
);

const rows = await query(sqlClient, Sql.selectTask(id));
const taskData = await injectLogs(R.prop(0, rows));

return injectLogs(R.prop(0, rows));
pubSub.publish(EVENTS.TASK.UPDATED, taskData);

return taskData;
};

const taskSubscriber = createEnvironmentFilteredSubscriber(
[
EVENTS.TASK.ADDED,
EVENTS.TASK.UPDATED,
]
);

const Resolvers /* : ResolversObj */ = {
getTasksByEnvironmentId,
getTaskByRemoteId,
addTask,
deleteTask,
updateTask,
taskSubscriber,
};

module.exports = Resolvers;
1 change: 1 addition & 0 deletions services/api/src/typeDefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -916,6 +916,7 @@ const typeDefs = gql`
type Subscription {
backupChanged(environment: Int!): Backup
deploymentChanged(environment: Int!): Deployment
taskChanged(environment: Int!): Task
}
`;

Expand Down
53 changes: 52 additions & 1 deletion services/ui/src/pages/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,31 @@ const query = gql`
}
`;

const subscribe = gql`
subscription subscribeToTasks($environment: Int!) {
taskChanged(environment: $environment) {
id
name
status
created
started
completed
remoteId
command
service
logs
}
}
`;

const PageTasks = withRouter(props => {
return (
<Page>
<Query
query={query}
variables={{ openshiftProjectName: props.router.query.name }}
>
{({ loading, error, data }) => {
{({ loading, error, data, subscribeToMore }) => {
if (loading) return null;
if (error) return `Error!: ${error}`;
const environment = data.environmentByOpenshiftProjectName;
Expand All @@ -69,6 +86,40 @@ const PageTasks = withRouter(props => {
}
];

subscribeToMore({
document: subscribe,
variables: { environment: environment.id},
updateQuery: (prevStore, { subscriptionData }) => {
if (!subscriptionData.data) return prevStore;
const prevTasks = prevStore.environmentByOpenshiftProjectName.tasks;
const incomingTask = subscriptionData.data.taskChanged;
const existingIndex = prevTasks.findIndex(prevTask => prevTask.id === incomingTask.id);
let newTasks;

// New task.
if (existingIndex === -1) {
newTasks = [
incomingTask,
...prevTasks,
];
}
// Updated task
else {
newTasks = Object.assign([...prevTasks], {[existingIndex]: incomingTask});
}

const newStore = {
...prevStore,
environmentByOpenshiftProjectName: {
...prevStore.environmentByOpenshiftProjectName,
tasks: newTasks,
},
};

return newStore;
}
});

const tasks = environment.tasks.map(task => {

const taskStart = task.started || task.created;
Expand Down

0 comments on commit 3597c24

Please sign in to comment.