Skip to content

Commit

Permalink
Bunch of streaming work
Browse files Browse the repository at this point in the history
  • Loading branch information
philrenaud committed Oct 20, 2023
1 parent f28d0b9 commit 10a21d1
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 33 deletions.
1 change: 0 additions & 1 deletion client/alloc_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,6 @@ func (a *Allocations) execImpl(encoder *codec.Encoder, decoder *codec.Decoder, e

err = h(ctx, req.Cmd, req.Tty, newExecStream(decoder, encoder))
if err != nil {
a.c.logger.Info("task exec session ended with an error in newStreamExec call", "error", err)
code := pointer.Of(int64(500))
return code, err
}
Expand Down
52 changes: 34 additions & 18 deletions ui/app/adapters/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,6 @@ export default class JobAdapter extends WatchableNamespaceIDs {
}

runAction(job, action, allocID) {
console.log('runAction from job adapter', job, action, allocID);

let messageBuffer = '';

const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
Expand All @@ -186,7 +184,24 @@ export default class JobAdapter extends WatchableNamespaceIDs {

const socket = new WebSocket(wsUrl);

socket.addEventListener('open', function (event) {
let notification;

socket.addEventListener('open', (event) => {
notification = this.notifications
.add({
title: `Action ${action.name} Message Received`,
color: 'success',
code: true,
sticky: true,
customAction: {
label: 'Stop Action',
action: () => {
socket.close();
},
},
})
.getFlashObject();

console.log('WebSocket connection opened:', event);
socket.send(
JSON.stringify({ version: 1, auth_token: this.token?.secret || '' })
Expand All @@ -199,19 +214,20 @@ export default class JobAdapter extends WatchableNamespaceIDs {
});

socket.addEventListener('message', (event) => {
if (!this.notifications.queue.includes(notification)) {
// User has manually closed the notification;
// explicitly close the socket and return;
socket.close();
return;
}

console.log('WebSocket message received:', event);
let jsonData = JSON.parse(event.data);
console.log('jsonData', jsonData);
if (jsonData.stdout && jsonData.stdout.data) {
messageBuffer = base64DecodeString(jsonData.stdout.data);
messageBuffer += base64DecodeString(jsonData.stdout.data);
console.log('msgbuf', messageBuffer);
messageBuffer += '\n';
this.notifications.add({
title: `Action ${action.name} Message Received`,
message: messageBuffer,
color: 'success',
code: true,
sticky: true,
});
notification.set('message', messageBuffer);
} else if (jsonData.stderr && jsonData.stderr.data) {
messageBuffer = base64DecodeString(jsonData.stderr.data);
messageBuffer += '\n';
Expand All @@ -227,16 +243,16 @@ export default class JobAdapter extends WatchableNamespaceIDs {

socket.addEventListener('close', (event) => {
console.log('WebSocket connection closed:', event);
this.notifications.add({
title: `Action ${action.name} Completed`,
message: messageBuffer || event.reason,
color: 'success',
});
messageBuffer = '';
});

socket.addEventListener('error', function (event) {
console.error('WebSocket encountered an error:', event);
this.notifications.add({
title: `Error received from ${action.name}`,
message: event,
color: 'critical',
sticky: true,
});
});

return socket;
Expand Down
2 changes: 1 addition & 1 deletion ui/app/components/task-sub-row.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
{{/if}}
{{/if}}
</td>
{{#if this.task.task.taskGroup.job.actions.length}}
{{#if @jobHasActions}}
<td data-test-actions>
{{#if this.task.task.actions.length}}
<Hds::Dropdown class="actions-dropdown" as |dd|>
Expand Down
6 changes: 6 additions & 0 deletions ui/app/models/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@ export default class ActionModel extends Fragment {
@attr('string') command;
@attr() args;
@fragmentOwner() task;

get allocations() {
return this.task.taskGroup.allocations.filter((a) => {
return a.clientStatus === 'running';
});
}
}
2 changes: 1 addition & 1 deletion ui/app/models/job.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ export default class Job extends Model {
return acc.concat(
taskGroup.tasks
.map((task) => {
return task.get('actions').toArray();
return task.get('actions')?.toArray() || [];
})
.reduce((taskAcc, taskActions) => taskAcc.concat(taskActions), [])
);
Expand Down
4 changes: 2 additions & 2 deletions ui/app/models/task.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export default class Task extends Fragment {
@attr('string') name;
@attr('string') driver;
@attr('string') kind;

@fragmentArray('action') actions;
@fragmentArray('action', { defaultValue: () => [] })
actions;

@attr() meta;

Expand Down
5 changes: 4 additions & 1 deletion ui/app/styles/core/notifications.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ section.notifications {
color: white;
display: block;
padding: 0.25rem;
max-height: 400px;
overflow: auto;
pre {
display: inline-block;
background: inherit;
color: inherit;
border: none;
white-space: normal;
white-space: pre-wrap;
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ui/app/templates/clients/client/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@
/>
{{#if this.showSubTasks}}
{{#each row.model.states as |task|}}
<TaskSubRow @namespan="8" @taskState={{task}} @active={{eq this.activeTask (concat task.allocation.id "-" task.name)}} @onSetActiveTask={{action 'setActiveTaskQueryParam'}} />
<TaskSubRow @namespan="8" @taskState={{task}} @active={{eq this.activeTask (concat task.allocation.id "-" task.name)}} @onSetActiveTask={{action 'setActiveTaskQueryParam'}} @jobHasActions={{true}} /> {{!-- TODO: make this last column conditional --}}
{{/each}}
{{/if}}
</t.body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@

{{#if this.showSubTasks}}
{{#each row.model.states as |task|}}
<TaskSubRow @namespan="9" @taskState={{task}} @active={{eq @activeTask (concat task.allocation.id "-" task.name)}} @onSetActiveTask={{@setActiveTaskQueryParam}} />
<TaskSubRow @namespan="9" @taskState={{task}} @active={{eq @activeTask (concat task.allocation.id "-" task.name)}} @onSetActiveTask={{@setActiveTaskQueryParam}} @jobHasActions={{this.job.actions.length}} />
{{/each}}
{{/if}}
</t.body>
Expand Down
9 changes: 4 additions & 5 deletions ui/app/templates/components/job-page/parts/title.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,17 @@
<div>
{{#if (not (eq this.job.status "dead"))}}
{{!-- ACTIONS --}}
{{log "job actions" this.job.actions}}
{{#if this.job.actions.length}}
<Hds::Dropdown class="actions-dropdown" as |dd|>
<dd.ToggleButton @color="secondary" @text="Actions" />
{{#each this.job.actions as |action|}}
{{#if (gt this.job.allocations.length 1)}}
{{#if (gt action.allocations.length 1)}}
<dd.Separator />
<dd.Title @text={{action.name}} />
<dd.Interactive {{on "click" (perform this.runAction action (get (object-at 0 this.job.allocations) "id"))}} @text="Run on a random alloc" />
<dd.Interactive {{on "click" (perform this.runAction action "random")}} @text="Run on all {{this.job.allocations.length}} allocs" />
<dd.Interactive {{on "click" (perform this.runAction action (get (object-at 0 action.allocations) "id"))}} @text="Run on a random alloc" />
<dd.Interactive {{on "click" (perform this.runAction action "random")}} @text="Run on all {{action.allocations.length}} allocs" />
{{else}}
<dd.Interactive {{on "click" (perform this.runAction action (get (object-at 0 this.job.allocations) "id"))}} @text="{{action.name}}" />
<dd.Interactive {{on "click" (perform this.runAction action (get (object-at 0 action.allocations) "id"))}} @text="{{action.name}}" />
{{/if}}
{{/each}}
{{!-- <dd.ToggleIcon @color="secondary" @icon="user-circle" @text="user menu" @size="small" data-test-header-profile-dropdown />
Expand Down
2 changes: 1 addition & 1 deletion ui/app/templates/jobs/job/allocations.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
@context="job"
@onClick={{action "gotoAllocation" row.model}} />
{{#each row.model.states as |task|}}
<TaskSubRow @active={{eq this.activeTask (concat task.allocation.id "-" task.name)}} @onSetActiveTask={{action 'setActiveTaskQueryParam'}} @namespan="9" @taskState={{task}} />
<TaskSubRow @active={{eq this.activeTask (concat task.allocation.id "-" task.name)}} @onSetActiveTask={{action 'setActiveTaskQueryParam'}} @namespan="9" @taskState={{task}} @jobHasActions={{this.job.actions.length}} />
{{/each}}

</t.body>
Expand Down
2 changes: 1 addition & 1 deletion ui/app/templates/jobs/job/task-group.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@
/>
{{#if this.showSubTasks}}
{{#each row.model.states as |task|}}
<TaskSubRow @namespan="8" @taskState={{task}} @active={{eq this.activeTask (concat task.allocation.id "-" task.name)}} @onSetActiveTask={{action 'setActiveTaskQueryParam'}} />
<TaskSubRow @namespan="8" @taskState={{task}} @active={{eq this.activeTask (concat task.allocation.id "-" task.name)}} @onSetActiveTask={{action 'setActiveTaskQueryParam'}} @jobHasActions={{this.model.job.actions.length}} />
{{/each}}
{{/if}}
</t.body>
Expand Down

0 comments on commit 10a21d1

Please sign in to comment.