From 21e18aced4a1392053407dfab0fca997bdb50bf3 Mon Sep 17 00:00:00 2001 From: Johan Haals Date: Wed, 20 Jan 2021 17:18:07 +0100 Subject: [PATCH] Create inital class structure --- .../src/scaffolder/tasks/Database.ts | 35 +++++++++++++++++++ .../src/scaffolder/tasks/TaskObserver.ts | 15 ++++++++ .../src/scaffolder/tasks/taskWorker.ts | 34 ++++++++++++++++++ .../src/scaffolder/tasks/tasksDispatcher.ts | 29 +++++++++++++++ .../src/scaffolder/tasks/types.ts | 30 ++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 plugins/scaffolder-backend/src/scaffolder/tasks/Database.ts create mode 100644 plugins/scaffolder-backend/src/scaffolder/tasks/TaskObserver.ts create mode 100644 plugins/scaffolder-backend/src/scaffolder/tasks/taskWorker.ts create mode 100644 plugins/scaffolder-backend/src/scaffolder/tasks/tasksDispatcher.ts create mode 100644 plugins/scaffolder-backend/src/scaffolder/tasks/types.ts diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/Database.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/Database.ts new file mode 100644 index 0000000000..f446f1e90a --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/Database.ts @@ -0,0 +1,35 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { Task, Status } from './types'; + +export class Database { + private readonly store = new Map(); + + get(taskId: string) { + return this.store.get(taskId); + } + + write(task: Task) { + return task; + } + + writeStatus(taskId: string, status: Status) { + const task = this.store.get(taskId); + if (task) { + this.store.set(taskId, { ...task, status }); + } + } +} diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/TaskObserver.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/TaskObserver.ts new file mode 100644 index 0000000000..863d6e76e1 --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/TaskObserver.ts @@ -0,0 +1,15 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/taskWorker.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/taskWorker.ts new file mode 100644 index 0000000000..4b1754523c --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/taskWorker.ts @@ -0,0 +1,34 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Status, ClaimResponse } from './types'; +import { Database } from './Database'; + +export class TaskWorker { + static async fromConfig() {} + + constructor(private readonly database: Database) {} + + claim(): Promise { + return Promise.resolve(undefined); + } + + setStatus(taskId: string, status: Status) { + this.database.writeStatus(taskId, status); + } + + heartbeat(runId: number) {} +} diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/tasksDispatcher.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/tasksDispatcher.ts new file mode 100644 index 0000000000..c3dfaa3c88 --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/tasksDispatcher.ts @@ -0,0 +1,29 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { Task } from './types'; +import { Database } from './Database'; + +export class taskDispatcher { + static async fromConfig(config: { database: Database }) {} + + constructor(private readonly db: Database) {} + + dispatch(task: Task): Promise { + this.db.write(task); + return Promise.resolve('uuid'); + } +} diff --git a/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts b/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts new file mode 100644 index 0000000000..000e292e16 --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/tasks/types.ts @@ -0,0 +1,30 @@ +/* + * Copyright 2021 Spotify AB + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +export type Status = 'OPEN' | 'PROCESSING' | 'FAILED' | 'CANCELLED'; +export type Task = { + taskId: string; + metadata: string; + status: Status; + lastHeartbeat: string; + retryCount: number; + createdAt: string; +}; + +export type ClaimResponse = { + runId: number; + task: Task; +};