Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Justin committed Jun 23, 2020
1 parent 58a7541 commit f971e65
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 21 deletions.
10 changes: 8 additions & 2 deletions jupyterlab_slurm/slurm.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ async def run_command(self, command, stdin=None, cwd=None):
class ScancelHandler(ShellExecutionHandler):
# Add `-H "Authorization: token <token>"` to the curl command for any DELETE request
async def delete(self):
jobID = self.get_body_arguments('jobID')[0]
if self.request.headers['Content-Type'] == 'application/json':
jobID = json.loads(self.request.body)["jobID"]
else:
jobID = self.get_body_arguments('jobID')[0]
stdout, stderr, returncode = await self.run_command("scancel " + jobID)
if stderr:
responseMessage = stderr
Expand All @@ -47,7 +50,10 @@ async def delete(self):
class ScontrolHandler(ShellExecutionHandler):
# Add `-H "Authorization: token <token>"` to the curl command for any PATCH request
async def patch(self, command):
jobID = self.get_body_arguments('jobID')[0]
if self.request.headers['Content-Type'] == 'application/json':
jobID = json.loads(self.request.body)["jobID"]
else:
jobID = self.get_body_arguments('jobID')[0]
stdout, stderr, returncode = await self.run_command("scontrol " + command + " " + jobID)
if stderr:
responseMessage = stderr
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"react": "^16.9.0",
"react-bootstrap": "^1.0.1",
"react-dom": "^16.9.0",
"uuid": "^3.3.3",
"uuid": "^8.1.0",
"yarn-deduplicate": "^1.1.1"
},
"devDependencies": {
Expand Down
14 changes: 8 additions & 6 deletions src/components/JobSubmitModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import {
Form,
Button,
} from 'react-bootstrap';
// Local
// import Select from './Select';

namespace types {
export type Props = {
show: boolean,
onHide: () => void;
submitJob: (input: string, inputType: string) => void;
error?: string;
disabled?: boolean;
};

export type State = {
Expand Down Expand Up @@ -46,7 +45,6 @@ export default class JobSubmitModal extends Component<types.Props, types.State>

handleSubmit() {
const { inputType, filepath, inlineScript } = this.state;
console.log(inputType, filepath, inlineScript);
const input = inputType === 'path' ? filepath : inlineScript;
this.props.submitJob(input, inputType);
}
Expand All @@ -62,7 +60,9 @@ export default class JobSubmitModal extends Component<types.Props, types.State>
<Modal.Body>
<Form.Group controlId="mode-selector">
<Form.Label>Script type</Form.Label>
<Form.Control as="select" onChange={e => this.changeInputType(e.target.value)}>
<Form.Control as="select"
onChange={e => this.changeInputType(e.target.value)} value={this.state.inputType}
disabled={this.props.disabled}>
<option value="path">File</option>
<option value="contents">Text input</option>
</Form.Control>
Expand All @@ -74,6 +74,7 @@ export default class JobSubmitModal extends Component<types.Props, types.State>
type="text"
placeholder="path relative to filebrowser"
onChange={(e) => this.updateFilepath(e.target.value)}
disabled={this.props.disabled}
/>
</Form.Group>
}
Expand All @@ -84,14 +85,15 @@ export default class JobSubmitModal extends Component<types.Props, types.State>
as="textarea"
rows={10}
onChange={e => this.updateInlineScript(e.target.value)}
disabled={this.props.disabled}
/>
</Form.Group>
}
<div>{this.props.error}</div>
</Modal.Body>
<Modal.Footer>
<Button variant="secondary" onClick={onHide}>Close</Button>
<Button variant="primary" onClick={this.handleSubmit.bind(this)}>Submit Job</Button>
<Button variant="secondary" onClick={onHide} disabled={this.props.disabled}>Close</Button>
<Button variant="primary" onClick={this.handleSubmit.bind(this)} disabled={this.props.disabled}>Submit Job</Button>
</Modal.Footer>
</Modal>
);
Expand Down
34 changes: 22 additions & 12 deletions src/components/SlurmManager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import {
} from '@jupyterlab/filebrowser';

import {
uuidv4,
} from 'uuid/v4';
v4 as uuidv4
} from 'uuid';

// Local
import { makeRequest } from '../utils';
Expand Down Expand Up @@ -39,6 +39,7 @@ namespace types {
alerts: Alert[];
jobSubmitModalVisible: boolean;
jobSubmitError?: string;
jobSubmitDisabled?: boolean;
userOnly: boolean
};
}
Expand All @@ -53,6 +54,7 @@ export default class SlurmManager extends Component<types.Props, types.State> {

constructor(props: types.Props) {
super(props);
this.requestStatusTable = new Map<string, types.JobStatus>();
this.state = {
alerts: [],
jobSubmitModalVisible: false,
Expand Down Expand Up @@ -81,24 +83,24 @@ export default class SlurmManager extends Component<types.Props, types.State> {
private async makeJobRequest(route: string, method: string, body: string) {
const beforeResponse = () => {
const requestID = uuidv4();
this.requestStatusTable[requestID] = 'sent';
this.requestStatusTable.set(requestID, 'sent');
return [requestID];
}
const afterResponse = async (response: Response, requestID: string) => {
if (response.status !== 200) {
this.requestStatusTable[requestID] = 'error';
this.requestStatusTable.set(requestID, 'error');
throw Error(response.statusText);
}
else {
const json = await response.json();
let alert: types.Alert = { message: json.responseMessage };
if (json.returncode === 0) {
alert.variant = 'success';
this.requestStatusTable[requestID] = 'received';
this.requestStatusTable.set(requestID, 'received');
}
else {
alert.variant = 'danger';
this.requestStatusTable[requestID] = 'error';
this.requestStatusTable.set(requestID, 'error');
console.log(json.errorMessage);
}
this.addAlert(alert);
Expand Down Expand Up @@ -141,11 +143,11 @@ export default class SlurmManager extends Component<types.Props, types.State> {
case 'release':
return { route: 'scontrol/release', method: 'PATCH' };
}
})();
})(action);
// TODO: Change backend and do all of this in a single request
rows.map(row => {
const jobID = row[this.JOBID_IDX];
this.makeJobRequest(route, method, `jobID=${jobID}`);
this.makeJobRequest(route, method, JSON.stringify({ jobID }));
});
}

Expand All @@ -169,6 +171,7 @@ export default class SlurmManager extends Component<types.Props, types.State> {
}

private submitJob(input: string, inputType: string) {
this.setState({ jobSubmitDisabled: true });
let { serverRoot, filebrowser } = this.props;
const fileBrowserRelativePath = filebrowser.model.path;
if (serverRoot !== '/') { // Add trailing slash, but not to '/'
Expand All @@ -190,15 +193,18 @@ export default class SlurmManager extends Component<types.Props, types.State> {
}).then((result) => {
if (result === null) {
this.setState({
jobSubmitError: "Unknown error encountered while submitting the script. Try again later."
jobSubmitError: "Unknown error encountered while submitting the script. Try again later.",
jobSubmitDisabled: false
})
}
if (result["errorMessage"] !== "") {
if (result["returncode"] !== 0) {
this.setState({
jobSubmitError: result["errorMessage"]
jobSubmitError: result["errorMessage"] == "" ? result["responseMessage"] : result["errorMessage"],
jobSubmitDisabled: false
});
} else {
this.hideJobSubmitModal();
this.setState({ jobSubmitDisabled: false });
}
});
}
Expand Down Expand Up @@ -264,7 +270,10 @@ export default class SlurmManager extends Component<types.Props, types.State> {
</div>
<div id="alertContainer" className="container alert-container">
{alerts.map((alert, index) => (
<Alert variant={alert.variant} key={`alert-${index}`} dismissible>
<Alert variant={alert.variant} key={`alert-${index}`} dismissible onClose={() => {
this.state.alerts.splice(index, 1);
this.setState({});
}}>
{alert.message}
</Alert>
))}
Expand All @@ -274,6 +283,7 @@ export default class SlurmManager extends Component<types.Props, types.State> {
error={this.state.jobSubmitError}
onHide={this.hideJobSubmitModal.bind(this)}
submitJob={this.submitJob.bind(this)}
disabled={this.state.jobSubmitDisabled}
/>
</>
);
Expand Down
1 change: 1 addition & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function makeRequest(request: types.Request) {
// Add Jupyter authorization (XRSF) token to request header
'Authorization': 'token ' + PageConfig.getToken(),
// Prevent it from enconding as plain-text UTF-8
'Content-Type': 'application/x-www-form-urlencoded'
},
}
if (body) {
Expand Down

0 comments on commit f971e65

Please sign in to comment.