generated from freckle/typescript-action-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
76 additions
and
211 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,116 +1,78 @@ | ||
// https://stackoverflow.com/questions/45222819/can-pseudo-lock-objects-be-used-in-the-amazon-s3-api/75347123#75347123 | ||
|
||
import * as S3 from "@aws-sdk/client-s3"; | ||
import * as core from "@actions/core"; | ||
import { v4 as uuidv4 } from "uuid"; | ||
|
||
import type { S3Client as S3ClientType } from "@aws-sdk/client-s3"; | ||
import { | ||
S3Client, | ||
PutObjectCommand, | ||
ListObjectsV2Command, | ||
DeleteObjectCommand, | ||
} from "@aws-sdk/client-s3"; | ||
|
||
import { mapMaybe } from "./maybe"; | ||
import { Duration } from "./duration"; | ||
import { compareObjects } from "./sort-objects"; | ||
import { normalizePrefix } from "./normalize-prefix"; | ||
|
||
export type AcquireLockResult = "acquired" | "not-acquired"; | ||
import { createObjectKey, validateObjectKey } from "./S3LockExt"; | ||
|
||
export class S3Lock { | ||
bucket: string; | ||
prefix: string; | ||
name: string; | ||
uuid: string; | ||
expires: Duration; | ||
|
||
private key: string; | ||
private keyPrefix: string; | ||
private s3: S3.S3Client; | ||
|
||
constructor( | ||
bucket: string, | ||
prefix: string, | ||
name: string, | ||
expires: Duration, | ||
uuid?: string, | ||
) { | ||
private bucket: string; | ||
private prefix: string; | ||
private expires: Duration; | ||
private s3: S3ClientType; | ||
|
||
constructor(bucket: string, name: string, expires: Duration) { | ||
this.bucket = bucket; | ||
this.prefix = normalizePrefix(prefix); | ||
this.name = name; | ||
this.uuid = uuid ? uuid : uuidv4(); | ||
this.prefix = `${name}.`; | ||
this.expires = expires; | ||
|
||
this.keyPrefix = `${this.prefix}${this.name}.`; | ||
this.key = `${this.keyPrefix}${this.uuid}`; | ||
this.s3 = new S3.S3Client(); | ||
this.s3 = new S3Client(); | ||
} | ||
|
||
async acquireLock(): Promise<AcquireLockResult> { | ||
await this.createLock(); | ||
|
||
const output = await this.listLocks(); | ||
const outputKeys = (output.Contents || []).map((o) => o.Key); | ||
const oldestKey = this.getOldestKey(output); | ||
|
||
core.debug(`Keys\n ${outputKeys.join("\n ")}`); | ||
core.debug(`Oldest: ${oldestKey}`); | ||
async acquireLock(): Promise<string | null> { | ||
const key = createObjectKey(this.prefix, this.expires); | ||
|
||
if (oldestKey === this.key) { | ||
return "acquired"; | ||
} | ||
|
||
await this.releaseLock(); | ||
return "not-acquired"; | ||
} | ||
core.debug(`[s3] PutObject ${key}`); | ||
|
||
async releaseLock(): Promise<void> { | ||
core.debug(`DELETE ${this.key}`); | ||
await this.s3.send( | ||
new S3.DeleteObjectCommand({ | ||
Bucket: this.bucket, | ||
Key: this.key, | ||
}), | ||
new PutObjectCommand({ Bucket: this.bucket, Key: key, Body: "" }), | ||
); | ||
} | ||
|
||
private async createLock(): Promise<void> { | ||
const expires = this.expires.after(new Date()); | ||
|
||
core.debug(`PUT ${this.key} (Expires: ${expires})`); | ||
core.debug(`[s3] ListObjectsV2 ${this.prefix}`); | ||
|
||
await this.s3.send( | ||
new S3.PutObjectCommand({ | ||
const output = await this.s3.send( | ||
new ListObjectsV2Command({ | ||
Bucket: this.bucket, | ||
Key: this.key, | ||
Expires: expires, | ||
Prefix: this.prefix, | ||
}), | ||
); | ||
} | ||
|
||
private async listLocks(): Promise<S3.ListObjectsV2Output> { | ||
return await this.s3.send( | ||
new S3.ListObjectsV2Command({ | ||
Bucket: this.bucket, | ||
Prefix: this.keyPrefix, | ||
}), | ||
); | ||
} | ||
|
||
private getOldestKey(output: S3.ListObjectsV2Output): string { | ||
if (output.IsTruncated) { | ||
// If we've got > ~1,000 locks here, something is very wrong | ||
throw new Error("Too many lock objects present"); | ||
throw new Error("TODO"); | ||
} | ||
|
||
const contents = output.Contents ?? []; | ||
const keys = mapMaybe(output.Contents || [], (o) => | ||
validateObjectKey(this.prefix, o), | ||
).sort(); | ||
|
||
if (contents.length === 0) { | ||
// If our own lock didn't get written/returned, something is very wrong | ||
throw new Error("No lock objects found"); | ||
} | ||
core.debug(`Keys:\n- ${keys.join("\n- ")}`); | ||
|
||
const sorted = contents.sort(compareObjects); | ||
const sortedKey = sorted[0].Key; | ||
if (keys.length === 0) { | ||
throw new Error("TODO"); | ||
} | ||
|
||
if (!sortedKey) { | ||
// If the thing doesn't have a Key, something is very wrong | ||
throw new Error("Oldest object has no Key"); | ||
if (keys[0] === key) { | ||
return key; | ||
} | ||
|
||
return sortedKey; | ||
await S3Lock.releaseLock(this.bucket, key); | ||
return null; | ||
} | ||
|
||
static async releaseLock(bucket: string, key: string): Promise<void> { | ||
const s3 = new S3Client(); | ||
core.debug(`[s3] DeleteObject ${key}`); | ||
|
||
await s3.send( | ||
new DeleteObjectCommand({ | ||
Bucket: bucket, | ||
Key: key, | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.