From 7e87aad73c953f2b347be6a3a6cba6b4a6b0686f Mon Sep 17 00:00:00 2001 From: James Greenaway Date: Tue, 29 Jan 2019 10:08:55 +0000 Subject: [PATCH] Add workload service --- package.json | 1 + src/state/workloads/index.ts | 1 + src/state/workloads/services.ts | 67 +++++++++++++++++++++++++++++++++ yarn.lock | 5 +++ 4 files changed, 74 insertions(+) create mode 100644 src/state/workloads/services.ts diff --git a/package.json b/package.json index 6bf00a4..17170fe 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.1.0", "private": true, "dependencies": { + "moment": "^2.24.0", "react": "^16.7.0", "react-dom": "^16.7.0", "react-redux": "^6.0.0", diff --git a/src/state/workloads/index.ts b/src/state/workloads/index.ts index 4398836..97ebce8 100644 --- a/src/state/workloads/index.ts +++ b/src/state/workloads/index.ts @@ -1,2 +1,3 @@ export * from './actions'; export * from './reducers'; +export * from './services'; diff --git a/src/state/workloads/services.ts b/src/state/workloads/services.ts new file mode 100644 index 0000000..303f339 --- /dev/null +++ b/src/state/workloads/services.ts @@ -0,0 +1,67 @@ +import moment from 'moment'; + +import { Status } from './types'; + + +export class WorkloadService { + + private workLoads: { [key in number]: Work } = {}; + private counter = 0; + + + private getWorkload(id: number): Work | undefined { + return this.workLoads[id]; + } + + private completeWorkload(work: Work) { + work.status = this.counter % 2 + ? 'SUCCESS' + : 'FAILURE'; + } + + + public create({ complexity }: { complexity: number }) { + const id = this.counter++; + const status = 'WORKING'; + + const seconds = complexity; + const completeDate = moment().add(complexity, 'second').toDate(); + const timer = setTimeout(() => this.completeWorkload(work), seconds); + + const work: Work = { + id, + complexity, + status, + completeDate, + timer, + } + this.workLoads[id] = work; + + return Promise.resolve({ id, complexity, completeDate }); + } + + public checkStatus({ id }: { id: number }) { + const work = this.getWorkload(id); + if (!work) return Promise.reject('Workload not found'); + return Promise.resolve(work); + } + + public cancel({ id }: { id: number }) { + const work = this.getWorkload(id); + if (!work) return Promise.reject('Workload not found'); + if (work.status !== 'WORKING') return Promise.reject('Workload cannot be canceled'); + + clearTimeout(work.timer) + work.status = 'CANCELED'; + + return Promise.resolve(work); + } +} + +interface Work { + id: number; + complexity: number; + completeDate: Date; + status: Status; + timer: NodeJS.Timeout; +} diff --git a/yarn.lock b/yarn.lock index 7a3e750..c00aee0 100755 --- a/yarn.lock +++ b/yarn.lock @@ -6297,6 +6297,11 @@ mkdirp@0.5.1, mkdirp@0.5.x, mkdirp@^0.5.0, mkdirp@^0.5.1, mkdirp@~0.5.0, mkdirp@ dependencies: minimist "0.0.8" +moment@^2.24.0: + version "2.24.0" + resolved "https://npm.nutterlogic.com/moment/-/moment-2.24.0.tgz#0d055d53f5052aa653c9f6eb68bb5d12bf5c2b5b" + integrity sha512-bV7f+6l2QigeBBZSM/6yTNq4P2fNpSWj/0e7jQcy87A8e7o2nAfP/34/2ky5Vw4B9S446EtIhodAzkFCcR4dQg== + move-concurrently@^1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/move-concurrently/-/move-concurrently-1.0.1.tgz#be2c005fda32e0b29af1f05d7c4b33214c701f92"