-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update job execution to Micah's suggestion
- Loading branch information
Showing
6 changed files
with
138 additions
and
104 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
7 changes: 7 additions & 0 deletions
7
source/SIL.AppBuilder.Portal/node-server/job-executors/base.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,7 @@ | ||
import { BullMQ } from 'sil.appbuilder.portal.common'; | ||
import { Job } from 'bullmq'; | ||
|
||
export abstract class ScriptoriaJobExecutor<T extends BullMQ.ScriptoriaJobType> { | ||
constructor() {} | ||
abstract execute(job: Job<BullMQ.JobTypeMap[T], number, string>): Promise<number>; | ||
} |
2 changes: 2 additions & 0 deletions
2
source/SIL.AppBuilder.Portal/node-server/job-executors/index.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,2 @@ | ||
export { Test } from './test.js'; | ||
export { ReassignUserTasks } from './reassignUserTasks.js'; |
99 changes: 99 additions & 0 deletions
99
source/SIL.AppBuilder.Portal/node-server/job-executors/reassignUserTasks.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,99 @@ | ||
import { BullMQ, prisma, DatabaseWrites } from 'sil.appbuilder.portal.common'; | ||
import { RoleId } from 'sil.appbuilder.portal.common/prisma'; | ||
import { Job } from 'bullmq'; | ||
import { ScriptoriaJobExecutor } from './base.js'; | ||
|
||
export class ReassignUserTasks extends ScriptoriaJobExecutor<BullMQ.ScriptoriaJobType.ReassignUserTasks> { | ||
async execute(job: Job<BullMQ.SyncUserTasksJob, number, string>): Promise<number> { | ||
// TODO: Noop | ||
// Should | ||
// Clear preexecuteentries (product transition steps) | ||
// Remove relevant user tasks | ||
// Create new user tasks (send notifications) | ||
// Recreate preexecute entries | ||
const products = await prisma.products.findMany({ | ||
where: { | ||
ProjectId: job.data.projectId | ||
}, | ||
include: { | ||
ProductTransitions: true | ||
} | ||
}); | ||
for (const product of products) { | ||
// Clear PreExecuteEntries | ||
await DatabaseWrites.productTransitions.deleteMany({ | ||
where: { | ||
WorkflowUserId: null, | ||
ProductId: product.Id, | ||
DateTransition: null | ||
} | ||
}); | ||
// Clear existing UserTasks | ||
await DatabaseWrites.userTasks.deleteMany({ | ||
where: { | ||
ProductId: product.Id | ||
} | ||
}); | ||
// Create tasks for all users that could perform this activity | ||
// TODO: this comes from dwkit GetAllActorsFor(Direct|Reverse)CommandTransitions | ||
const organizationId = ( | ||
await prisma.projects.findUnique({ | ||
where: { | ||
Id: job.data.projectId | ||
}, | ||
include: { | ||
Organization: true | ||
} | ||
}) | ||
).OrganizationId; | ||
// All users that own the project or are org admins | ||
const allUsersWithAction = await prisma.users.findMany({ | ||
where: { | ||
OR: [ | ||
{ | ||
UserRoles: { | ||
some: { | ||
OrganizationId: organizationId, | ||
RoleId: RoleId.OrgAdmin | ||
} | ||
} | ||
}, | ||
{ | ||
Projects: { | ||
some: { | ||
Id: job.data.projectId | ||
} | ||
} | ||
} | ||
] | ||
} | ||
}); | ||
// TODO: DWKit: Need ActivityName and Status from dwkit implementation | ||
const createdTasks = allUsersWithAction.map((user) => ({ | ||
UserId: user.Id, | ||
ProductId: product.Id, | ||
ActivityName: null, | ||
Status: null | ||
})); | ||
await DatabaseWrites.userTasks.createMany({ | ||
data: createdTasks | ||
}); | ||
for (const task of createdTasks) { | ||
// Send notification for the new task | ||
// TODO | ||
// sendNotification(task); | ||
} | ||
// TODO: DWKit: CreatePreExecuteEntries | ||
} | ||
|
||
return ( | ||
await prisma.userTasks.findMany({ | ||
where: { | ||
Product: { | ||
ProjectId: job.data.projectId | ||
} | ||
} | ||
}) | ||
).length; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
source/SIL.AppBuilder.Portal/node-server/job-executors/test.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,13 @@ | ||
import { BullMQ } from 'sil.appbuilder.portal.common'; | ||
import { Job } from 'bullmq'; | ||
import { ScriptoriaJobExecutor } from './base.js'; | ||
|
||
export class Test extends ScriptoriaJobExecutor<BullMQ.ScriptoriaJobType.Test> { | ||
async execute(job: Job<BullMQ.TestJob, number, string>): Promise<number> { | ||
job.updateProgress(50); | ||
const time = job.data.time; | ||
await new Promise((r) => setTimeout(r, 1000 * time)); | ||
job.updateProgress(100); | ||
return 0; | ||
} | ||
} |