Skip to content

Commit

Permalink
Update job execution to Micah's suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
FyreByrd committed Oct 1, 2024
1 parent 285ee84 commit eae401b
Show file tree
Hide file tree
Showing 6 changed files with 138 additions and 104 deletions.
12 changes: 9 additions & 3 deletions source/SIL.AppBuilder.Portal/common/BullJobTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@ export enum ScriptoriaJobType {
ReassignUserTasks = 'ReassignUserTasks'
}

export interface TestJob {
export type TestJob = {
type: ScriptoriaJobType.Test;
time: number;
}

export interface SyncUserTasksJob {
export type SyncUserTasksJob = {
type: ScriptoriaJobType.ReassignUserTasks;
projectId: number;
}

export type ScriptoriaJob = TestJob | SyncUserTasksJob;
export type ScriptoriaJob = JobTypeMap[keyof JobTypeMap];

export type JobTypeMap = {
[ScriptoriaJobType.Test]: TestJob;
[ScriptoriaJobType.ReassignUserTasks]: SyncUserTasksJob;
// Add more mappings here as needed
};
109 changes: 8 additions & 101 deletions source/SIL.AppBuilder.Portal/node-server/BullWorker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Job, Worker } from 'bullmq';
import { BullMQ, DatabaseWrites, prisma } from 'sil.appbuilder.portal.common';
import { RoleId } from 'sil.appbuilder.portal.common/prisma';
import { BullMQ } from 'sil.appbuilder.portal.common';
import * as Executor from './job-executors/index.js';

export abstract class BullWorker<T, R> {
public worker: Worker;
Expand All @@ -20,105 +20,12 @@ export class ScriptoriaWorker extends BullWorker<BullMQ.ScriptoriaJob, number> {
}
async run(job: Job<BullMQ.ScriptoriaJob, number, string>): Promise<number> {
switch (job.data.type) {
case BullMQ.ScriptoriaJobType.Test: {
job.updateProgress(50);
const time = job.data.time;
await new Promise((r) => setTimeout(r, 1000 * time));
job.updateProgress(100);
return 0;
}
case BullMQ.ScriptoriaJobType.ReassignUserTasks: {
// 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;
}
case BullMQ.ScriptoriaJobType.Test:
return new Executor.Test().execute(job as Job<BullMQ.TestJob, number, string>);
case BullMQ.ScriptoriaJobType.ReassignUserTasks:
return new Executor.ReassignUserTasks().execute(
job as Job<BullMQ.SyncUserTasksJob, number, string>
);
}
}
}
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>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { Test } from './test.js';
export { ReassignUserTasks } from './reassignUserTasks.js';
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 source/SIL.AppBuilder.Portal/node-server/job-executors/test.ts
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;
}
}

0 comments on commit eae401b

Please sign in to comment.