Skip to content

Commit

Permalink
Merge pull request #26 from UnownHash/fix-job-all-devices
Browse files Browse the repository at this point in the history
Fix sending job to all devices
  • Loading branch information
Fabio1988 authored Dec 16, 2023
2 parents a481c58 + ebd9890 commit 4093626
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"dev:server": "nx run server:dev",
"lint": "nx run-many --target=lint --all",
"start": "node dist/packages/server/main.js",
"dev:mock": "NODE_ENV=development NX_ENABLE_MOCKS=1 nx run-many --target=dev --all",
"clear-cache": "nx clear-cache"
},
"volta": {
Expand Down
18 changes: 11 additions & 7 deletions packages/client/src/app/jobs/executeJobModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ export const ExecuteJobModal: React.FC<ExecuteJobModalProps> = ({ closeModal, de

const executeJob = useCallback(
async ({ deviceIds }: { deviceIds: string[] | number[] }) => {
const promise = fetch(`/api/job/execute/${jobId}/${deviceIds.join()}`, { method: 'POST' }).then(
async (response) => {
if (response.status !== 200) {
throw new Error();
}
closeModal();
const promise = fetch(`/api/job/execute/${jobId}`, {
method: 'POST',
headers: {
'Content-Type': 'application/json', // fastify is only able to parse the body to a project with this header
},
);
body: JSON.stringify({ deviceIdsOrOrigins: deviceIds }),
}).then(async (response) => {
if (response.status !== 200) {
throw new Error();
}
closeModal();
});

toast.promise(promise, {
pending: `Running ${jobId}...`,
Expand Down
31 changes: 22 additions & 9 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,24 +533,35 @@ const routes = async (fastifyInstance: FastifyInstance) => {
});

interface JobExecuteParams {
deviceIdsOrOrigins: string;
deviceIdsOrOrigins?: string;
jobId: string;
}

fastifyInstance.post<{ Params: JobExecuteParams }>(
'/api/job/execute/:jobId/:deviceIdsOrOrigins',
interface DeviceIdsOrOriginsBody {
deviceIdsOrOrigins?: string[] | number[];
}

fastifyInstance.post<{ Params: JobExecuteParams; Body: DeviceIdsOrOriginsBody }>(
'/api/job/execute/:jobId/:deviceIdsOrOrigins?',
async (req, reply) => {
const deviceIdsOrOrigins = req.params.deviceIdsOrOrigins.split(',');
const jobId = req.params.jobId;
const { deviceIdsOrOrigins, jobId } = req.params;
const requestBody = req.body;
const job = jobs.getById(jobId);

if (!job) {
reply.code(404).send({ status: 'error', error: 'Job not found' });

return;
}

const devices: DeviceControlConnection[] = deviceIdsOrOrigins
// If deviceIdsOrOrigins is not set in URL params, use the value from the request body
const deviceIdsOrOriginsList =
deviceIdsOrOrigins !== undefined && deviceIdsOrOrigins.trim() !== ''
? deviceIdsOrOrigins.split(',')
: requestBody.deviceIdsOrOrigins !== undefined
? requestBody.deviceIdsOrOrigins
: [];

const devices: DeviceControlConnection[] = deviceIdsOrOriginsList
.map((deviceIdOrOrigin) => {
if (deviceIdOrOrigin in controlConnections) {
return controlConnections[deviceIdOrOrigin];
Expand All @@ -561,12 +572,14 @@ const routes = async (fastifyInstance: FastifyInstance) => {
.filter((device): device is DeviceControlConnection => !!device);

if (devices.length === 0) {
reply.code(404).send({ status: 'error', error: `No device found with IDS or origins ${deviceIdsOrOrigins}` });
reply
.code(404)
.send({ status: 'error', error: `No device found with IDS or origins ${deviceIdsOrOriginsList}` });

return;
}

log.info(`Execute job on device ${deviceIdsOrOrigins} job id ${jobId}`);
log.info(`Execute job on device ${deviceIdsOrOriginsList} job id ${jobId}`);

const jobNumbers = devices.map((controlConnection) => {
return jobExecutor.runJob(controlConnection, job);
Expand Down

0 comments on commit 4093626

Please sign in to comment.