-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fga] add relationship update job (#18671)
- Loading branch information
1 parent
b24ae94
commit 3a07121
Showing
6 changed files
with
84 additions
and
15 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
components/server/src/authorization/relationship-updater-job.ts
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Copyright (c) 2023 Gitpod GmbH. All rights reserved. | ||
* Licensed under the GNU Affero General Public License (AGPL). | ||
* See License.AGPL.txt in the project root for license information. | ||
*/ | ||
|
||
import { injectable, inject } from "inversify"; | ||
import { log } from "@gitpod/gitpod-protocol/lib/util/logging"; | ||
import { Job } from "../jobs/runner"; | ||
import { RelationshipUpdater } from "./relationship-updater"; | ||
import { TypeORM, UserDB } from "@gitpod/gitpod-db/lib"; | ||
|
||
@injectable() | ||
export class RelationshipUpdateJob implements Job { | ||
constructor( | ||
@inject(RelationshipUpdater) private relationshipUpdater: RelationshipUpdater, | ||
@inject(UserDB) private readonly userDB: UserDB, | ||
@inject(TypeORM) private readonly db: TypeORM, | ||
) {} | ||
|
||
public name = "relationship-update-job"; | ||
public frequencyMs = 1000 * 60 * 60 * 1; // 1h | ||
|
||
public async run(): Promise<void> { | ||
try { | ||
const connection = await this.db.getConnection(); | ||
const results = await connection.query(` | ||
SELECT id FROM d_b_user | ||
WHERE | ||
(additionalData->"$.fgaRelationshipsVersion" != ${RelationshipUpdater.version} OR | ||
additionalData->"$.fgaRelationshipsVersion" IS NULL) AND | ||
markedDeleted = 0 | ||
ORDER BY _lastModified DESC | ||
LIMIT 1000;`); | ||
const now = Date.now(); | ||
for (const result of results) { | ||
const user = await this.userDB.findUserById(result.id); | ||
if (!user) { | ||
continue; | ||
} | ||
try { | ||
await this.relationshipUpdater.migrate(user); | ||
} catch (error) { | ||
log.error(RelationshipUpdateJob.name + ": error running relationship update job", error); | ||
} | ||
} | ||
log.info( | ||
RelationshipUpdateJob.name + ": updated " + results.length + " users in " + (Date.now() - now) + "ms", | ||
); | ||
} catch (error) { | ||
log.error(RelationshipUpdateJob.name + ": error running relationship update job", error); | ||
} | ||
} | ||
} |
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 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