Skip to content

Commit

Permalink
chore: repairservice
Browse files Browse the repository at this point in the history
change to class
  • Loading branch information
seaerchin committed May 28, 2024
1 parent 3e9e3b0 commit 318437b
Showing 1 changed file with 47 additions and 37 deletions.
84 changes: 47 additions & 37 deletions src/services/admin/RepairService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,42 +13,52 @@ import GitFileSystemService from "../db/GitFileSystemService"

const LOCK_TIME_SECONDS = 15 * 60 // 15 minutes

// TODO: Add class constructor and update these to be deps
const gitFileSystemService: GitFileSystemService = (null as unknown) as GitFileSystemService
const reposService: ReposService = (null as unknown) as ReposService

export const lockRepo = (
repoName: string,
lockDurationSeconds: number = LOCK_TIME_SECONDS
) =>
ResultAsync.fromPromise(
lock(repoName, lockDurationSeconds),
(err) => new LockedError(`Unable to lock repo ${repoName}, ${err}`)
).map(() => repoName)

export const cloneRepo = (repoName: string) => {
const repoUrl = `[email protected]:isomerpages/${repoName}.git`

return (
gitFileSystemService
.cloneBranch(repoName, true)
// Repo does not exist in EFS, clone it
.andThen(() =>
// repo exists in efs, but we need to pull for staging and reset staging lite
gitFileSystemService
.pull(repoName, "staging")
.andThen(() =>
fromPromise(
reposService.setUpStagingLite(
path.join(EFS_VOL_PATH_STAGING_LITE, repoName),
repoUrl
),
(error) =>
new GitFileSystemError(
`Error setting up staging lite for repo ${repoName}: ${error}`
)
interface RepairServiceProps {
gitFileSystemService: GitFileSystemService
reposService: ReposService
}

export class RepairService {
gitFileSystemService: GitFileSystemService

reposService: ReposService

constructor({ gitFileSystemService, reposService }: RepairServiceProps) {
this.reposService = reposService
this.gitFileSystemService = gitFileSystemService
}

lockRepo(repoName: string, lockDurationSeconds: number = LOCK_TIME_SECONDS) {
return ResultAsync.fromPromise(
lock(repoName, lockDurationSeconds),
(err) => new LockedError(`Unable to lock repo ${repoName}, ${err}`)
).map(() => repoName)
}

cloneRepo(repoName: string) {
const repoUrl = `[email protected]:isomerpages/${repoName}.git`

return (
this.gitFileSystemService
.cloneBranch(repoName, true)
// Repo does not exist in EFS, clone it
.andThen(() =>
// repo exists in efs, but we need to pull for staging and reset staging lite
this.gitFileSystemService
.pull(repoName, "staging")
.andThen(() =>
fromPromise(
this.reposService.setUpStagingLite(
path.join(EFS_VOL_PATH_STAGING_LITE, repoName),
repoUrl
),
(error) =>
new GitFileSystemError(
`Error setting up staging lite for repo ${repoName}: ${error}`
)
)
)
)
)
)
)
)
}
}

0 comments on commit 318437b

Please sign in to comment.