Skip to content

Commit

Permalink
Add state backed by storage bucket
Browse files Browse the repository at this point in the history
[changelog:added]
  • Loading branch information
cdupuis committed Jul 14, 2020
1 parent 9aeb579 commit 1d2cd72
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export {
SlackFileMessage,
} from "./lib/message";
export {} from "./lib/payload";
export * as state from "./lib/state";
export * as status from "./lib/status";
export { runSteps, Step, StepListener } from "./lib/steps";
export { StorageProvider } from "./lib/storage";
Expand Down
48 changes: 48 additions & 0 deletions lib/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright © 2020 Atomist, Inc.
*
* 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 * as fs from "fs-extra";
import * as os from "os";
import * as path from "path";
import { Contextual } from "./handler";
import { warn } from "./log/console";
import { guid } from "./util";

export async function hydrate<T>(ctx: Contextual<any, any>): Promise<T> {
const key = stateKey(ctx);
try {
const stateFile = await ctx.storage.retrieve(key);
return fs.readJson(stateFile);
} catch (e) {
return {} as T;
}
}

export async function save(state: any, ctx: Contextual<any, any>): Promise<void> {
const key = stateKey(ctx);
try {
const targetFilePath = path.join(os.tmpdir() || "/tmp", guid());
await fs.ensureDir(path.dirname(targetFilePath));
await fs.writeJson(targetFilePath, state);
await ctx.storage.store(key, targetFilePath);
} catch (e) {
warn(`Failed to save state: ${e.message}`);
}
}

function stateKey(ctx: Contextual<any, any>): string {
return `state/${ctx.workspaceId}/${ctx.skill.namespace}/${ctx.skill.name}/${ctx.skill.id}.json`;
}

0 comments on commit 1d2cd72

Please sign in to comment.