@openforis/arena-core 2.18.22 → 2.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/auth/authorizer.d.ts +1 -0
- package/dist/auth/authorizer.js +3 -0
- package/dist/auth/authorizer.js.map +1 -1
- package/dist/esm/auth/authorizer.js +3 -0
- package/dist/esm/auth/authorizer.js.map +1 -1
- package/dist/esm/job/JobBase.js +46 -9
- package/dist/esm/job/JobBase.js.map +1 -1
- package/dist/job/JobBase.d.ts +5 -0
- package/dist/job/JobBase.js +46 -9
- package/dist/job/JobBase.js.map +1 -1
- package/package.json +1 -1
|
@@ -65,6 +65,7 @@ export declare const Authorizer: {
|
|
|
65
65
|
canViewNotOwnedRecords: typeof canViewNotOwnedRecords;
|
|
66
66
|
canExportRecordsList: (user: User, surveyInfo: Survey) => boolean;
|
|
67
67
|
canEditRecord: typeof canEditRecord;
|
|
68
|
+
canEditRecords: (user: User, surveyInfo: Survey) => boolean;
|
|
68
69
|
canDeleteRecord: typeof canEditRecord;
|
|
69
70
|
canDemoteRecord: typeof canChangeRecordProps;
|
|
70
71
|
canChangeRecordOwner: typeof canChangeRecordProps;
|
package/dist/auth/authorizer.js
CHANGED
|
@@ -113,6 +113,8 @@ const canEditRecord = (user, record, allowAnalysisStepEdit = false) => {
|
|
|
113
113
|
return level === permission_1.RecordStepPermission.all || (level === permission_1.RecordStepPermission.own && ownerUuid === user.uuid);
|
|
114
114
|
};
|
|
115
115
|
exports.canEditRecord = canEditRecord;
|
|
116
|
+
// survey level: the user's role allows editing records (e.g. not survey guests); see canEditRecord for a specific record
|
|
117
|
+
const canEditRecords = _hasSurveyPermission(permission_1.Permission.recordEdit);
|
|
116
118
|
const canChangeRecordProps = (user, record) => (0, exports.canEditRecord)(user, record, true);
|
|
117
119
|
const canDeleteRecord = exports.canEditRecord;
|
|
118
120
|
const canDemoteRecord = canChangeRecordProps;
|
|
@@ -226,6 +228,7 @@ exports.Authorizer = {
|
|
|
226
228
|
canViewNotOwnedRecords: exports.canViewNotOwnedRecords,
|
|
227
229
|
canExportRecordsList: exports.canExportRecordsList,
|
|
228
230
|
canEditRecord: exports.canEditRecord,
|
|
231
|
+
canEditRecords,
|
|
229
232
|
canDeleteRecord,
|
|
230
233
|
canDemoteRecord,
|
|
231
234
|
canChangeRecordOwner,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authorizer.js","sourceRoot":"","sources":["../../src/auth/authorizer.ts"],"names":[],"mappings":";;;AAAA,sCAAgD;AAChD,sCAA2C;AAC3C,2CAAsD;AACtD,6CAAyC;AACzC,6CAA+D;AAE/D,mCAA+B;AAE/B,SAAS;AACT,gBAAgB;AAChB,SAAS;AAET,MAAM,mBAAmB,GAAG,CAAC,IAAU,EAAE,UAAkB,EAAE,kBAAkB,GAAG,IAAI,EAAyB,EAAE,CAC/G,UAAU,CAAC,CAAC,CAAC,aAAK,CAAC,wBAAwB,CAAC,UAAU,CAAC,IAAI,EAAE,kBAAkB,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AAEpG,+FAA+F;AAC/F,MAAM,sBAAsB,GAAG,CAAC,EAAE,IAAI,EAAE,UAAU,EAAsC,EAAW,EAAE,CACnG,OAAO,CAAC,mBAAmB,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC,CAAA;AAEvD,MAAM,oBAAoB,GACxB,CAAC,UAAsB,EAAE,EAAE,CAC3B,CAAC,IAAU,EAAE,UAAkB,EAAW,EAAE;IAC1C,IAAI,CAAC,IAAI;QAAE,OAAO,KAAK,CAAA;IAEvB,IAAI,aAAK,CAAC,aAAa,CAAC,IAAI,CAAC;QAAE,OAAO,IAAI,CAAA;IAE1C,IAAI,CAAC,UAAU;QAAE,OAAO,KAAK,CAAA;IAE7B,MAAM,SAAS,GAAG,mBAAmB,CAAC,IAAI,EAAE,UAAU,CAAC,CAAA;IACvD,IAAI,CAAC,SAAS;QAAE,OAA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ACX,gBAAgB;IAChB,gBAAgB;IAChB,aAAa;IACb,mBAAmB;IACnB,8BAA8B;IAC9B,8BAA8B;IAC9B,wBAAwB;IACxB,qBAAqB;IACrB,uBAAuB;IACvB,0BAA0B;IAC1B,0BAA0B;IAC1B,4BAA4B;IAC5B,uBAAuB;IACvB,SAAS;IACT,sBAAsB;CACvB,CAAA","sourcesContent":["import { ArenaRecord, Records } from '../record'\nimport { Survey, Surveys } from '../survey'\nimport { AuthGroup, AuthGroupName } from './authGroup'\nimport { AuthGroups } from './authGroups'\nimport { Permission, RecordStepPermission } from './permission'\nimport { User } from './user'\nimport { Users } from './users'\n\n// ======\n// ====== Survey\n// ======\n\nconst _getSurveyUserGroup = (user: User, surveyInfo: Survey, includeSystemAdmin = true): AuthGroup | undefined =>\n surveyInfo ? Users.getAuthGroupBySurveyUuid(surveyInfo.uuid, includeSystemAdmin)(user) : undefined\n\n// system admins are not considered as members of the survey, so survey admins cannot edit them\nconst _hasAuthGroupForSurvey = ({ user, surveyInfo }: { user: User; surveyInfo: Survey }): boolean =>\n Boolean(_getSurveyUserGroup(user, surveyInfo, false))\n\nconst _hasSurveyPermission =\n (permission: Permission) =>\n (user: User, surveyInfo: Survey): boolean => {\n if (!user) return false\n\n if (Users.isSystemAdmin(user)) return true\n\n if (!surveyInfo) return false\n\n const authGroup = _getSurveyUserGroup(user, surveyInfo)\n if (!authGroup) return false\n\n return AuthGroups.getPermissions(authGroup).includes(permission)\n }\n\nconst _hasPermissionInSomeGroup =\n (permission: Permission) =>\n (user: User): boolean => {\n if (Users.isSystemAdmin(user)) return true\n const groups = Users.getAuthGroups(user)\n return groups.some((group) => AuthGroups.getPermissions(group).includes(permission))\n }\n\n// CREATE\nconst canCreateSurvey = _hasPermissionInSomeGroup(Permission.surveyCreate)\nconst canCreateTemplate = (user: User): boolean => Users.isSystemAdmin(user)\nconst getMaxSurveysUserCanCreate = (user: User): number => {\n if (Users.isSystemAdmin(user)) return Number.NaN\n if (canCreateSurvey(user)) return Users.getMaxSurveys(user)\n return 0\n}\n\n// READ\nconst canViewSurvey = (user: User, surveyInfo: Survey): boolean =>\n Users.isSystemAdmin(user) || _hasAuthGroupForSurvey({ user, surveyInfo })\n// any logged in user can view a published template (e.g. to clone it), without widening canViewSurvey\nconst canViewSurveyOrPublishedTemplate = (user: User, surveyInfo: Survey): boolean =>\n canViewSurvey(user, surveyInfo) || Boolean(user && surveyInfo?.template && surveyInfo.published)\nconst canExportSurvey = _hasSurveyPermission(Permission.recordAnalyse)\nconst canExportSurveysList = (user: User): boolean => Users.isSystemAdmin(user)\nconst canViewTemplates = (user: User): boolean => Users.isSystemAdmin(user)\n// UPDATE\nconst canEditSurvey = _hasSurveyPermission(Permission.surveyEdit)\nconst canEditSomeSurvey = _hasPermissionInSomeGroup(Permission.surveyEdit)\nconst canEditSurveyConfig = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditSurveyOwner = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditTemplates = (user: User): boolean => Users.isSystemAdmin(user)\nconst canRefreshAllSurveyRdbs = (user: User): boolean => Users.isSystemAdmin(user)\n\n// ======\n// ====== Record\n// ======\n\n// CREATE\nexport const canCreateRecord = _hasSurveyPermission(Permission.recordCreate)\n\n// READ\nexport const canViewRecord = _hasSurveyPermission(Permission.recordView)\nexport const canExportAllRecords = _hasSurveyPermission(Permission.recordCleanse)\nexport const canViewNotOwnedRecords = (user: User, surveyInfo: Survey): boolean => {\n if (!surveyInfo || !canViewSurvey(user, surveyInfo)) return false\n\n const { uuid: surveyUuid } = surveyInfo\n const groupInCurrentSurvey = Users.getAuthGroupBySurveyUuid(surveyUuid)(user)\n const groupName = groupInCurrentSurvey?.name\n\n if (groupName === AuthGroupName.dataEditor) {\n return Surveys.isDataEditorViewNotOwnedRecordsAllowed(surveyInfo)\n }\n if (groupName === AuthGroupName.dataAnalyst) {\n return Surveys.isDataAnalystViewNotOwnedRecordsAllowed(surveyInfo)\n }\n // survey guests can view (but not edit) all the records (restricted by user group qualifiers, if any)\n if (groupName === AuthGroupName.surveyGuest) return true\n if (canExportAllRecords(user, surveyInfo)) return true\n\n return false\n}\nexport const canExportRecordsList = _hasSurveyPermission(Permission.recordAnalyse)\n\n// UPDATE\nexport const canEditRecord = (user: User, record: ArenaRecord, allowAnalysisStepEdit = false): boolean => {\n if (\n !user ||\n !record ||\n // records in analysis cannot be edited\n (!allowAnalysisStepEdit && Records.isInAnalysisStep(record))\n ) {\n return false\n }\n // system admin does not have an auth group associated to the survey, but he can always edit records;\n if (Users.isSystemAdmin(user)) {\n return true\n }\n const { surveyUuid, ownerUuid } = record\n // if user doesn't have an auth group associated to the survey, he cannot edit records\n const userAuthGroup = Users.getAuthGroupBySurveyUuid(surveyUuid)(user)\n if (!userAuthGroup) return false\n\n // groups without the record edit permission (e.g. survey guests) cannot edit records,\n // even when their record steps permission allows it (record steps permissions are used also to view records)\n if (!AuthGroups.getPermissions(userAuthGroup).includes(Permission.recordEdit)) return false\n\n // Level = 'all' or 'own'. If 'own', user can only edit records assigned to him\n // If 'all', he can edit all survey's records\n // permission checks must not throw: a group without record steps cannot edit records\n const level = AuthGroups.getRecordSteps(userAuthGroup)?.[Records.getStep(record)]\n return level === RecordStepPermission.all || (level === RecordStepPermission.own && ownerUuid === user.uuid)\n}\n\nconst canChangeRecordProps = (user: User, record: ArenaRecord) => canEditRecord(user, record, true)\n\nconst canDeleteRecord = canEditRecord\n\nconst canDemoteRecord = canChangeRecordProps\n\nconst canChangeRecordOwner = canChangeRecordProps\n\nconst canChangeRecordStep = canChangeRecordProps\n\nconst canCleanseRecords = _hasSurveyPermission(Permission.recordCleanse)\n\nconst canExportRecords = _hasSurveyPermission(Permission.recordView)\n\nconst canImportRecords = (user: User, surveyInfo: Survey) =>\n surveyInfo?.published && _hasSurveyPermission(Permission.recordCreate)(user, surveyInfo)\n\nconst canAnalyzeRecords = _hasSurveyPermission(Permission.recordAnalyse)\n\nconst canUpdateRecordsStep = canAnalyzeRecords\n\n// ======\n// ====== Explorer\n// ======\n\nconst canUseExplorer = canCleanseRecords\n\n// ======\n// ====== Map\n// ======\n\nconst canUseMap = canAnalyzeRecords\n\n// ======\n// ====== Users\n// ======\n\n// CREATE\nconst canInviteUsers = _hasSurveyPermission(Permission.userInvite)\n\n// READ\nconst canViewSurveyUsers = _hasSurveyPermission(Permission.userInvite)\n\nconst _usersBelongToSameSurvey = ({ user, userToView }: { user: User; userToView: User }) =>\n Users.getAuthGroups(user).some(\n (authGroupUser) =>\n AuthGroups.isSurveyGroup(authGroupUser) &&\n Users.getAuthGroups(userToView).some(\n (authGroupUserToView) =>\n AuthGroups.isSurveyGroup(authGroupUserToView) &&\n AuthGroups.getSurveyUuid(authGroupUserToView) === AuthGroups.getSurveyUuid(authGroupUser)\n )\n )\n\nconst canViewUser = (user: User, _surveyInfo: Survey, userToView: User) =>\n Users.isSystemAdmin(user) || Users.isEqual(userToView)(user) || _usersBelongToSameSurvey({ user, userToView })\n\nconst canViewOtherUsersEmail = ({ user, surveyInfo }: { user: User; surveyInfo: Survey }) =>\n Users.isSystemAdmin(user) || canInviteUsers(user, surveyInfo)\n\nconst canViewOtherUsersNameInSameSurvey = (user: User, surveyInfo: Survey): boolean =>\n _hasSurveyPermission(Permission.recordView)(user, surveyInfo)\n\nconst canViewAllUsers = (user: User) => Users.isSystemAdmin(user)\n\n// EDIT\nconst _hasUserEditAccess = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.isSystemAdmin(user) ||\n (_hasSurveyPermission(Permission.userEdit)(user, surveyInfo) &&\n // user to update has an auth group in the same survey\n _hasAuthGroupForSurvey({ user: userToUpdate, surveyInfo }))\n\nconst canCreateUsers = (user: User): boolean => Users.isSystemAdmin(user)\n\nconst canEditUser = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.hasAccepted(userToUpdate) &&\n (Users.isEqual(user)(userToUpdate) || _hasUserEditAccess(user, surveyInfo, userToUpdate))\n\nconst canEditUserEmail = (user: User): boolean => Users.isSystemAdmin(user)\n\nconst canEditUserGroup = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n !Users.isEqual(user)(userToUpdate) && _hasUserEditAccess(user, surveyInfo, userToUpdate)\n\nconst canRemoveUser = (user: User, surveyInfo: Survey, userToRemove: User): boolean =>\n !Users.isEqual(user)(userToRemove) &&\n !Users.isSystemAdmin(userToRemove) &&\n _hasUserEditAccess(user, surveyInfo, userToRemove)\n\n// only survey admins (and system admins) can manage the user groups of a survey\nconst canManageUserGroups = (user: User, surveyInfo: Survey): boolean => {\n if (!user) return false\n if (Users.isSystemAdmin(user)) return true\n if (!surveyInfo) return false\n return _getSurveyUserGroup(user, surveyInfo)?.name === AuthGroupName.surveyAdmin\n}\n\n// qualifier attribute values are tied to user groups\nconst canEditQualifierAttributeValue = canManageUserGroups\n\nconst canEditUserAuthGroupExtraProps = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.isSystemAdmin(user) ||\n (canInviteUsers(user, surveyInfo) && _hasAuthGroupForSurvey({ user: userToUpdate, surveyInfo }))\n\nconst canEditUserSurveyManager = (user: User) => Users.isSystemAdmin(user)\nconst canEditUserMaxSurveys = (user: User) => Users.isSystemAdmin(user)\n\n// USER ACCESS REQUESTS\nconst canViewUsersAccessRequests = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditUsersAccessRequests = (user: User): boolean => Users.isSystemAdmin(user)\n\n// TWO-FACTOR AUTHENTICATION\nconst canManageUser2FADevices = (user: User): boolean => Users.isSystemAdmin(user)\n\n// INVITE\nconst getUserGroupsCanAssign = ({\n user,\n surveyInfo = null,\n showOnlySurveyGroups = false,\n}: {\n user: User\n surveyInfo?: Survey | null\n showOnlySurveyGroups?: boolean\n}) => {\n const groups = []\n\n let surveyGroups: AuthGroup[] = []\n if (surveyInfo) {\n if (surveyInfo.published) {\n surveyGroups = Surveys.getAuthGroups(surveyInfo)\n } else {\n const adminGroup = Surveys.getAuthGroupAdmin(surveyInfo)\n surveyGroups = adminGroup ? [adminGroup] : []\n }\n }\n\n // do not allow surveyEditor group selection (remove surveyEditor group completely?)\n surveyGroups = surveyGroups.filter((group) => group.name !== AuthGroupName.surveyEditor)\n\n groups.push(...surveyGroups)\n\n if (!showOnlySurveyGroups && (Users.isSystemAdmin(user) || Users.isSurveyManager(user))) {\n // Add SystemAdmin or SurveyManager group if current user is a SystemAdmin or SurveyManager himself\n const userNonSurveyGroups = Users.getAuthGroups(user).filter((group) => !AuthGroups.isSurveyGroup(group))\n groups.push(...userNonSurveyGroups)\n }\n return AuthGroups.sortGroups(groups)\n}\n\nexport const Authorizer = {\n // Survey\n canCreateSurvey,\n canCreateTemplate,\n getMaxSurveysUserCanCreate,\n canViewSurvey,\n canViewSurveyOrPublishedTemplate,\n canExportSurvey,\n canExportSurveysList,\n canViewTemplates,\n canEditSurvey,\n canEditSomeSurvey,\n canEditSurveyConfig,\n canEditSurveyOwner,\n canEditTemplates,\n canRefreshAllSurveyRdbs,\n // Record\n canCreateRecord,\n canViewRecord,\n canExportAllRecords,\n canViewNotOwnedRecords,\n canExportRecordsList,\n canEditRecord,\n canDeleteRecord,\n canDemoteRecord,\n canChangeRecordOwner,\n canChangeRecordStep,\n canCleanseRecords,\n canExportRecords,\n canImportRecords,\n canAnalyzeRecords,\n canUpdateRecordsStep,\n // Explorer\n canUseExplorer,\n // Map\n canUseMap,\n // Users\n canInviteUsers,\n canViewSurveyUsers,\n canViewUser,\n canViewOtherUsersEmail,\n canViewOtherUsersNameInSameSurvey,\n canViewAllUsers,\n canCreateUsers,\n canEditUser,\n canEditUserEmail,\n canEditUserGroup,\n canRemoveUser,\n canManageUserGroups,\n canEditQualifierAttributeValue,\n canEditUserAuthGroupExtraProps,\n canEditUserSurveyManager,\n canEditUserMaxSurveys,\n // User Access Requests\n canViewUsersAccessRequests,\n canEditUsersAccessRequests,\n // Two-factor authentication\n canManageUser2FADevices,\n // Invite\n getUserGroupsCanAssign,\n}\n"]}
|
|
1
|
+
{"version":3,"file":"authorizer.js","sourceRoot":"","s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vB,SAAS;IACT,sBAAsB;CACvB,CAAA","sourcesContent":["import { ArenaRecord, Records } from '../record'\nimport { Survey, Surveys } from '../survey'\nimport { AuthGroup, AuthGroupName } from './authGroup'\nimport { AuthGroups } from './authGroups'\nimport { Permission, RecordStepPermission } from './permission'\nimport { User } from './user'\nimport { Users } from './users'\n\n// ======\n// ====== Survey\n// ======\n\nconst _getSurveyUserGroup = (user: User, surveyInfo: Survey, includeSystemAdmin = true): AuthGroup | undefined =>\n surveyInfo ? Users.getAuthGroupBySurveyUuid(surveyInfo.uuid, includeSystemAdmin)(user) : undefined\n\n// system admins are not considered as members of the survey, so survey admins cannot edit them\nconst _hasAuthGroupForSurvey = ({ user, surveyInfo }: { user: User; surveyInfo: Survey }): boolean =>\n Boolean(_getSurveyUserGroup(user, surveyInfo, false))\n\nconst _hasSurveyPermission =\n (permission: Permission) =>\n (user: User, surveyInfo: Survey): boolean => {\n if (!user) return false\n\n if (Users.isSystemAdmin(user)) return true\n\n if (!surveyInfo) return false\n\n const authGroup = _getSurveyUserGroup(user, surveyInfo)\n if (!authGroup) return false\n\n return AuthGroups.getPermissions(authGroup).includes(permission)\n }\n\nconst _hasPermissionInSomeGroup =\n (permission: Permission) =>\n (user: User): boolean => {\n if (Users.isSystemAdmin(user)) return true\n const groups = Users.getAuthGroups(user)\n return groups.some((group) => AuthGroups.getPermissions(group).includes(permission))\n }\n\n// CREATE\nconst canCreateSurvey = _hasPermissionInSomeGroup(Permission.surveyCreate)\nconst canCreateTemplate = (user: User): boolean => Users.isSystemAdmin(user)\nconst getMaxSurveysUserCanCreate = (user: User): number => {\n if (Users.isSystemAdmin(user)) return Number.NaN\n if (canCreateSurvey(user)) return Users.getMaxSurveys(user)\n return 0\n}\n\n// READ\nconst canViewSurvey = (user: User, surveyInfo: Survey): boolean =>\n Users.isSystemAdmin(user) || _hasAuthGroupForSurvey({ user, surveyInfo })\n// any logged in user can view a published template (e.g. to clone it), without widening canViewSurvey\nconst canViewSurveyOrPublishedTemplate = (user: User, surveyInfo: Survey): boolean =>\n canViewSurvey(user, surveyInfo) || Boolean(user && surveyInfo?.template && surveyInfo.published)\nconst canExportSurvey = _hasSurveyPermission(Permission.recordAnalyse)\nconst canExportSurveysList = (user: User): boolean => Users.isSystemAdmin(user)\nconst canViewTemplates = (user: User): boolean => Users.isSystemAdmin(user)\n// UPDATE\nconst canEditSurvey = _hasSurveyPermission(Permission.surveyEdit)\nconst canEditSomeSurvey = _hasPermissionInSomeGroup(Permission.surveyEdit)\nconst canEditSurveyConfig = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditSurveyOwner = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditTemplates = (user: User): boolean => Users.isSystemAdmin(user)\nconst canRefreshAllSurveyRdbs = (user: User): boolean => Users.isSystemAdmin(user)\n\n// ======\n// ====== Record\n// ======\n\n// CREATE\nexport const canCreateRecord = _hasSurveyPermission(Permission.recordCreate)\n\n// READ\nexport const canViewRecord = _hasSurveyPermission(Permission.recordView)\nexport const canExportAllRecords = _hasSurveyPermission(Permission.recordCleanse)\nexport const canViewNotOwnedRecords = (user: User, surveyInfo: Survey): boolean => {\n if (!surveyInfo || !canViewSurvey(user, surveyInfo)) return false\n\n const { uuid: surveyUuid } = surveyInfo\n const groupInCurrentSurvey = Users.getAuthGroupBySurveyUuid(surveyUuid)(user)\n const groupName = groupInCurrentSurvey?.name\n\n if (groupName === AuthGroupName.dataEditor) {\n return Surveys.isDataEditorViewNotOwnedRecordsAllowed(surveyInfo)\n }\n if (groupName === AuthGroupName.dataAnalyst) {\n return Surveys.isDataAnalystViewNotOwnedRecordsAllowed(surveyInfo)\n }\n // survey guests can view (but not edit) all the records (restricted by user group qualifiers, if any)\n if (groupName === AuthGroupName.surveyGuest) return true\n if (canExportAllRecords(user, surveyInfo)) return true\n\n return false\n}\nexport const canExportRecordsList = _hasSurveyPermission(Permission.recordAnalyse)\n\n// UPDATE\nexport const canEditRecord = (user: User, record: ArenaRecord, allowAnalysisStepEdit = false): boolean => {\n if (\n !user ||\n !record ||\n // records in analysis cannot be edited\n (!allowAnalysisStepEdit && Records.isInAnalysisStep(record))\n ) {\n return false\n }\n // system admin does not have an auth group associated to the survey, but he can always edit records;\n if (Users.isSystemAdmin(user)) {\n return true\n }\n const { surveyUuid, ownerUuid } = record\n // if user doesn't have an auth group associated to the survey, he cannot edit records\n const userAuthGroup = Users.getAuthGroupBySurveyUuid(surveyUuid)(user)\n if (!userAuthGroup) return false\n\n // groups without the record edit permission (e.g. survey guests) cannot edit records,\n // even when their record steps permission allows it (record steps permissions are used also to view records)\n if (!AuthGroups.getPermissions(userAuthGroup).includes(Permission.recordEdit)) return false\n\n // Level = 'all' or 'own'. If 'own', user can only edit records assigned to him\n // If 'all', he can edit all survey's records\n // permission checks must not throw: a group without record steps cannot edit records\n const level = AuthGroups.getRecordSteps(userAuthGroup)?.[Records.getStep(record)]\n return level === RecordStepPermission.all || (level === RecordStepPermission.own && ownerUuid === user.uuid)\n}\n\n// survey level: the user's role allows editing records (e.g. not survey guests); see canEditRecord for a specific record\nconst canEditRecords = _hasSurveyPermission(Permission.recordEdit)\n\nconst canChangeRecordProps = (user: User, record: ArenaRecord) => canEditRecord(user, record, true)\n\nconst canDeleteRecord = canEditRecord\n\nconst canDemoteRecord = canChangeRecordProps\n\nconst canChangeRecordOwner = canChangeRecordProps\n\nconst canChangeRecordStep = canChangeRecordProps\n\nconst canCleanseRecords = _hasSurveyPermission(Permission.recordCleanse)\n\nconst canExportRecords = _hasSurveyPermission(Permission.recordView)\n\nconst canImportRecords = (user: User, surveyInfo: Survey) =>\n surveyInfo?.published && _hasSurveyPermission(Permission.recordCreate)(user, surveyInfo)\n\nconst canAnalyzeRecords = _hasSurveyPermission(Permission.recordAnalyse)\n\nconst canUpdateRecordsStep = canAnalyzeRecords\n\n// ======\n// ====== Explorer\n// ======\n\nconst canUseExplorer = canCleanseRecords\n\n// ======\n// ====== Map\n// ======\n\nconst canUseMap = canAnalyzeRecords\n\n// ======\n// ====== Users\n// ======\n\n// CREATE\nconst canInviteUsers = _hasSurveyPermission(Permission.userInvite)\n\n// READ\nconst canViewSurveyUsers = _hasSurveyPermission(Permission.userInvite)\n\nconst _usersBelongToSameSurvey = ({ user, userToView }: { user: User; userToView: User }) =>\n Users.getAuthGroups(user).some(\n (authGroupUser) =>\n AuthGroups.isSurveyGroup(authGroupUser) &&\n Users.getAuthGroups(userToView).some(\n (authGroupUserToView) =>\n AuthGroups.isSurveyGroup(authGroupUserToView) &&\n AuthGroups.getSurveyUuid(authGroupUserToView) === AuthGroups.getSurveyUuid(authGroupUser)\n )\n )\n\nconst canViewUser = (user: User, _surveyInfo: Survey, userToView: User) =>\n Users.isSystemAdmin(user) || Users.isEqual(userToView)(user) || _usersBelongToSameSurvey({ user, userToView })\n\nconst canViewOtherUsersEmail = ({ user, surveyInfo }: { user: User; surveyInfo: Survey }) =>\n Users.isSystemAdmin(user) || canInviteUsers(user, surveyInfo)\n\nconst canViewOtherUsersNameInSameSurvey = (user: User, surveyInfo: Survey): boolean =>\n _hasSurveyPermission(Permission.recordView)(user, surveyInfo)\n\nconst canViewAllUsers = (user: User) => Users.isSystemAdmin(user)\n\n// EDIT\nconst _hasUserEditAccess = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.isSystemAdmin(user) ||\n (_hasSurveyPermission(Permission.userEdit)(user, surveyInfo) &&\n // user to update has an auth group in the same survey\n _hasAuthGroupForSurvey({ user: userToUpdate, surveyInfo }))\n\nconst canCreateUsers = (user: User): boolean => Users.isSystemAdmin(user)\n\nconst canEditUser = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.hasAccepted(userToUpdate) &&\n (Users.isEqual(user)(userToUpdate) || _hasUserEditAccess(user, surveyInfo, userToUpdate))\n\nconst canEditUserEmail = (user: User): boolean => Users.isSystemAdmin(user)\n\nconst canEditUserGroup = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n !Users.isEqual(user)(userToUpdate) && _hasUserEditAccess(user, surveyInfo, userToUpdate)\n\nconst canRemoveUser = (user: User, surveyInfo: Survey, userToRemove: User): boolean =>\n !Users.isEqual(user)(userToRemove) &&\n !Users.isSystemAdmin(userToRemove) &&\n _hasUserEditAccess(user, surveyInfo, userToRemove)\n\n// only survey admins (and system admins) can manage the user groups of a survey\nconst canManageUserGroups = (user: User, surveyInfo: Survey): boolean => {\n if (!user) return false\n if (Users.isSystemAdmin(user)) return true\n if (!surveyInfo) return false\n return _getSurveyUserGroup(user, surveyInfo)?.name === AuthGroupName.surveyAdmin\n}\n\n// qualifier attribute values are tied to user groups\nconst canEditQualifierAttributeValue = canManageUserGroups\n\nconst canEditUserAuthGroupExtraProps = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.isSystemAdmin(user) ||\n (canInviteUsers(user, surveyInfo) && _hasAuthGroupForSurvey({ user: userToUpdate, surveyInfo }))\n\nconst canEditUserSurveyManager = (user: User) => Users.isSystemAdmin(user)\nconst canEditUserMaxSurveys = (user: User) => Users.isSystemAdmin(user)\n\n// USER ACCESS REQUESTS\nconst canViewUsersAccessRequests = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditUsersAccessRequests = (user: User): boolean => Users.isSystemAdmin(user)\n\n// TWO-FACTOR AUTHENTICATION\nconst canManageUser2FADevices = (user: User): boolean => Users.isSystemAdmin(user)\n\n// INVITE\nconst getUserGroupsCanAssign = ({\n user,\n surveyInfo = null,\n showOnlySurveyGroups = false,\n}: {\n user: User\n surveyInfo?: Survey | null\n showOnlySurveyGroups?: boolean\n}) => {\n const groups = []\n\n let surveyGroups: AuthGroup[] = []\n if (surveyInfo) {\n if (surveyInfo.published) {\n surveyGroups = Surveys.getAuthGroups(surveyInfo)\n } else {\n const adminGroup = Surveys.getAuthGroupAdmin(surveyInfo)\n surveyGroups = adminGroup ? [adminGroup] : []\n }\n }\n\n // do not allow surveyEditor group selection (remove surveyEditor group completely?)\n surveyGroups = surveyGroups.filter((group) => group.name !== AuthGroupName.surveyEditor)\n\n groups.push(...surveyGroups)\n\n if (!showOnlySurveyGroups && (Users.isSystemAdmin(user) || Users.isSurveyManager(user))) {\n // Add SystemAdmin or SurveyManager group if current user is a SystemAdmin or SurveyManager himself\n const userNonSurveyGroups = Users.getAuthGroups(user).filter((group) => !AuthGroups.isSurveyGroup(group))\n groups.push(...userNonSurveyGroups)\n }\n return AuthGroups.sortGroups(groups)\n}\n\nexport const Authorizer = {\n // Survey\n canCreateSurvey,\n canCreateTemplate,\n getMaxSurveysUserCanCreate,\n canViewSurvey,\n canViewSurveyOrPublishedTemplate,\n canExportSurvey,\n canExportSurveysList,\n canViewTemplates,\n canEditSurvey,\n canEditSomeSurvey,\n canEditSurveyConfig,\n canEditSurveyOwner,\n canEditTemplates,\n canRefreshAllSurveyRdbs,\n // Record\n canCreateRecord,\n canViewRecord,\n canExportAllRecords,\n canViewNotOwnedRecords,\n canExportRecordsList,\n canEditRecord,\n canEditRecords,\n canDeleteRecord,\n canDemoteRecord,\n canChangeRecordOwner,\n canChangeRecordStep,\n canCleanseRecords,\n canExportRecords,\n canImportRecords,\n canAnalyzeRecords,\n canUpdateRecordsStep,\n // Explorer\n canUseExplorer,\n // Map\n canUseMap,\n // Users\n canInviteUsers,\n canViewSurveyUsers,\n canViewUser,\n canViewOtherUsersEmail,\n canViewOtherUsersNameInSameSurvey,\n canViewAllUsers,\n canCreateUsers,\n canEditUser,\n canEditUserEmail,\n canEditUserGroup,\n canRemoveUser,\n canManageUserGroups,\n canEditQualifierAttributeValue,\n canEditUserAuthGroupExtraProps,\n canEditUserSurveyManager,\n canEditUserMaxSurveys,\n // User Access Requests\n canViewUsersAccessRequests,\n canEditUsersAccessRequests,\n // Two-factor authentication\n canManageUser2FADevices,\n // Invite\n getUserGroupsCanAssign,\n}\n"]}
|
|
@@ -108,6 +108,8 @@ export const canEditRecord = (user, record, allowAnalysisStepEdit = false) => {
|
|
|
108
108
|
const level = (_a = AuthGroups.getRecordSteps(userAuthGroup)) === null || _a === void 0 ? void 0 : _a[Records.getStep(record)];
|
|
109
109
|
return level === RecordStepPermission.all || (level === RecordStepPermission.own && ownerUuid === user.uuid);
|
|
110
110
|
};
|
|
111
|
+
// survey level: the user's role allows editing records (e.g. not survey guests); see canEditRecord for a specific record
|
|
112
|
+
const canEditRecords = _hasSurveyPermission(Permission.recordEdit);
|
|
111
113
|
const canChangeRecordProps = (user, record) => canEditRecord(user, record, true);
|
|
112
114
|
const canDeleteRecord = canEditRecord;
|
|
113
115
|
const canDemoteRecord = canChangeRecordProps;
|
|
@@ -221,6 +223,7 @@ export const Authorizer = {
|
|
|
221
223
|
canViewNotOwnedRecords,
|
|
222
224
|
canExportRecordsList,
|
|
223
225
|
canEditRecord,
|
|
226
|
+
canEditRecords,
|
|
224
227
|
canDeleteRecord,
|
|
225
228
|
canDemoteRecord,
|
|
226
229
|
canChangeRecordOwner,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"authorizer.js","sourceRoot":"","sources":["../../../src/auth/authorizer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,OAAO,EAAE,MAAM,WAAW,CAAA;AAChD,OAAO,EAAU,OAAO,EAAE,MAAM,WAAW,CAAA;AAC3C,OAAO,EAAa,aAAa,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AAE/D,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,SAAS;AACT,gBAAgB;AAChB,SAAS;AAET,MAAM,mBAAmB,GAAG,CAAC,IAAU,EAAE,UAAkB,EAAE,kBAAkB,GAAG,IAAI,EAAyB,EAAE,CAC/G,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,wBAAwB,CAAC,UAAU,CAAC,IAAI,EAAE,kBAAkB,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AAEpG,+FAA+F;AAC/F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mBAAmB;IACnB,sBAAsB;IACtB,oBAAoB;IACpB,aAAa;IACb,eAAe;IACf,eAAe;IACf,oBAAoB;IACpB,mBAAmB;IACnB,iBAAiB;IACjB,gBAAgB;IAChB,gBAAgB;IAChB,iBAAiB;IACjB,oBAAoB;IACpB,WAAW;IACX,cAAc;IACd,MAAM;IACN,SAAS;IACT,QAAQ;IACR,cAAc;IACd,kBAAkB;IAClB,WAAW;IACX,sBAAsB;IACtB,iCAAiC;IACjC,eAAe;IACf,cAAc;IACd,WAAW;IACX,gBAAgB;IAChB,gBAAgB;IAChB,aAAa;IACb,mBAAmB;IACnB,8BAA8B;IAC9B,8BAA8B;IAC9B,wBAAwB;IACxB,qBAAqB;IACrB,uBAAuB;IACvB,0BAA0B;IAC1B,0BAA0B;IAC1B,4BAA4B;IAC5B,uBAAuB;IACvB,SAAS;IACT,sBAAsB;CACvB,CAAA","sourcesContent":["import { ArenaRecord, Records } from '../record'\nimport { Survey, Surveys } from '../survey'\nimport { AuthGroup, AuthGroupName } from './authGroup'\nimport { AuthGroups } from './authGroups'\nimport { Permission, RecordStepPermission } from './permission'\nimport { User } from './user'\nimport { Users } from './users'\n\n// ======\n// ====== Survey\n// ======\n\nconst _getSurveyUserGroup = (user: User, surveyInfo: Survey, includeSystemAdmin = true): AuthGroup | undefined =>\n surveyInfo ? Users.getAuthGroupBySurveyUuid(surveyInfo.uuid, includeSystemAdmin)(user) : undefined\n\n// system admins are not considered as members of the survey, so survey admins cannot edit them\nconst _hasAuthGroupForSurvey = ({ user, surveyInfo }: { user: User; surveyInfo: Survey }): boolean =>\n Boolean(_getSurveyUserGroup(user, surveyInfo, false))\n\nconst _hasSurveyPermission =\n (permission: Permission) =>\n (user: User, surveyInfo: Survey): boolean => {\n if (!user) return false\n\n if (Users.isSystemAdmin(user)) return true\n\n if (!surveyInfo) return false\n\n const authGroup = _getSurveyUserGroup(user, surveyInfo)\n if (!authGroup) return false\n\n return AuthGroups.getPermissions(authGroup).includes(permission)\n }\n\nconst _hasPermissionInSomeGroup =\n (permission: Permission) =>\n (user: User): boolean => {\n if (Users.isSystemAdmin(user)) return true\n const groups = Users.getAuthGroups(user)\n return groups.some((group) => AuthGroups.getPermissions(group).includes(permission))\n }\n\n// CREATE\nconst canCreateSurvey = _hasPermissionInSomeGroup(Permission.surveyCreate)\nconst canCreateTemplate = (user: User): boolean => Users.isSystemAdmin(user)\nconst getMaxSurveysUserCanCreate = (user: User): number => {\n if (Users.isSystemAdmin(user)) return Number.NaN\n if (canCreateSurvey(user)) return Users.getMaxSurveys(user)\n return 0\n}\n\n// READ\nconst canViewSurvey = (user: User, surveyInfo: Survey): boolean =>\n Users.isSystemAdmin(user) || _hasAuthGroupForSurvey({ user, surveyInfo })\n// any logged in user can view a published template (e.g. to clone it), without widening canViewSurvey\nconst canViewSurveyOrPublishedTemplate = (user: User, surveyInfo: Survey): boolean =>\n canViewSurvey(user, surveyInfo) || Boolean(user && surveyInfo?.template && surveyInfo.published)\nconst canExportSurvey = _hasSurveyPermission(Permission.recordAnalyse)\nconst canExportSurveysList = (user: User): boolean => Users.isSystemAdmin(user)\nconst canViewTemplates = (user: User): boolean => Users.isSystemAdmin(user)\n// UPDATE\nconst canEditSurvey = _hasSurveyPermission(Permission.surveyEdit)\nconst canEditSomeSurvey = _hasPermissionInSomeGroup(Permission.surveyEdit)\nconst canEditSurveyConfig = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditSurveyOwner = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditTemplates = (user: User): boolean => Users.isSystemAdmin(user)\nconst canRefreshAllSurveyRdbs = (user: User): boolean => Users.isSystemAdmin(user)\n\n// ======\n// ====== Record\n// ======\n\n// CREATE\nexport const canCreateRecord = _hasSurveyPermission(Permission.recordCreate)\n\n// READ\nexport const canViewRecord = _hasSurveyPermission(Permission.recordView)\nexport const canExportAllRecords = _hasSurveyPermission(Permission.recordCleanse)\nexport const canViewNotOwnedRecords = (user: User, surveyInfo: Survey): boolean => {\n if (!surveyInfo || !canViewSurvey(user, surveyInfo)) return false\n\n const { uuid: surveyUuid } = surveyInfo\n const groupInCurrentSurvey = Users.getAuthGroupBySurveyUuid(surveyUuid)(user)\n const groupName = groupInCurrentSurvey?.name\n\n if (groupName === AuthGroupName.dataEditor) {\n return Surveys.isDataEditorViewNotOwnedRecordsAllowed(surveyInfo)\n }\n if (groupName === AuthGroupName.dataAnalyst) {\n return Surveys.isDataAnalystViewNotOwnedRecordsAllowed(surveyInfo)\n }\n // survey guests can view (but not edit) all the records (restricted by user group qualifiers, if any)\n if (groupName === AuthGroupName.surveyGuest) return true\n if (canExportAllRecords(user, surveyInfo)) return true\n\n return false\n}\nexport const canExportRecordsList = _hasSurveyPermission(Permission.recordAnalyse)\n\n// UPDATE\nexport const canEditRecord = (user: User, record: ArenaRecord, allowAnalysisStepEdit = false): boolean => {\n if (\n !user ||\n !record ||\n // records in analysis cannot be edited\n (!allowAnalysisStepEdit && Records.isInAnalysisStep(record))\n ) {\n return false\n }\n // system admin does not have an auth group associated to the survey, but he can always edit records;\n if (Users.isSystemAdmin(user)) {\n return true\n }\n const { surveyUuid, ownerUuid } = record\n // if user doesn't have an auth group associated to the survey, he cannot edit records\n const userAuthGroup = Users.getAuthGroupBySurveyUuid(surveyUuid)(user)\n if (!userAuthGroup) return false\n\n // groups without the record edit permission (e.g. survey guests) cannot edit records,\n // even when their record steps permission allows it (record steps permissions are used also to view records)\n if (!AuthGroups.getPermissions(userAuthGroup).includes(Permission.recordEdit)) return false\n\n // Level = 'all' or 'own'. If 'own', user can only edit records assigned to him\n // If 'all', he can edit all survey's records\n // permission checks must not throw: a group without record steps cannot edit records\n const level = AuthGroups.getRecordSteps(userAuthGroup)?.[Records.getStep(record)]\n return level === RecordStepPermission.all || (level === RecordStepPermission.own && ownerUuid === user.uuid)\n}\n\nconst canChangeRecordProps = (user: User, record: ArenaRecord) => canEditRecord(user, record, true)\n\nconst canDeleteRecord = canEditRecord\n\nconst canDemoteRecord = canChangeRecordProps\n\nconst canChangeRecordOwner = canChangeRecordProps\n\nconst canChangeRecordStep = canChangeRecordProps\n\nconst canCleanseRecords = _hasSurveyPermission(Permission.recordCleanse)\n\nconst canExportRecords = _hasSurveyPermission(Permission.recordView)\n\nconst canImportRecords = (user: User, surveyInfo: Survey) =>\n surveyInfo?.published && _hasSurveyPermission(Permission.recordCreate)(user, surveyInfo)\n\nconst canAnalyzeRecords = _hasSurveyPermission(Permission.recordAnalyse)\n\nconst canUpdateRecordsStep = canAnalyzeRecords\n\n// ======\n// ====== Explorer\n// ======\n\nconst canUseExplorer = canCleanseRecords\n\n// ======\n// ====== Map\n// ======\n\nconst canUseMap = canAnalyzeRecords\n\n// ======\n// ====== Users\n// ======\n\n// CREATE\nconst canInviteUsers = _hasSurveyPermission(Permission.userInvite)\n\n// READ\nconst canViewSurveyUsers = _hasSurveyPermission(Permission.userInvite)\n\nconst _usersBelongToSameSurvey = ({ user, userToView }: { user: User; userToView: User }) =>\n Users.getAuthGroups(user).some(\n (authGroupUser) =>\n AuthGroups.isSurveyGroup(authGroupUser) &&\n Users.getAuthGroups(userToView).some(\n (authGroupUserToView) =>\n AuthGroups.isSurveyGroup(authGroupUserToView) &&\n AuthGroups.getSurveyUuid(authGroupUserToView) === AuthGroups.getSurveyUuid(authGroupUser)\n )\n )\n\nconst canViewUser = (user: User, _surveyInfo: Survey, userToView: User) =>\n Users.isSystemAdmin(user) || Users.isEqual(userToView)(user) || _usersBelongToSameSurvey({ user, userToView })\n\nconst canViewOtherUsersEmail = ({ user, surveyInfo }: { user: User; surveyInfo: Survey }) =>\n Users.isSystemAdmin(user) || canInviteUsers(user, surveyInfo)\n\nconst canViewOtherUsersNameInSameSurvey = (user: User, surveyInfo: Survey): boolean =>\n _hasSurveyPermission(Permission.recordView)(user, surveyInfo)\n\nconst canViewAllUsers = (user: User) => Users.isSystemAdmin(user)\n\n// EDIT\nconst _hasUserEditAccess = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.isSystemAdmin(user) ||\n (_hasSurveyPermission(Permission.userEdit)(user, surveyInfo) &&\n // user to update has an auth group in the same survey\n _hasAuthGroupForSurvey({ user: userToUpdate, surveyInfo }))\n\nconst canCreateUsers = (user: User): boolean => Users.isSystemAdmin(user)\n\nconst canEditUser = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.hasAccepted(userToUpdate) &&\n (Users.isEqual(user)(userToUpdate) || _hasUserEditAccess(user, surveyInfo, userToUpdate))\n\nconst canEditUserEmail = (user: User): boolean => Users.isSystemAdmin(user)\n\nconst canEditUserGroup = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n !Users.isEqual(user)(userToUpdate) && _hasUserEditAccess(user, surveyInfo, userToUpdate)\n\nconst canRemoveUser = (user: User, surveyInfo: Survey, userToRemove: User): boolean =>\n !Users.isEqual(user)(userToRemove) &&\n !Users.isSystemAdmin(userToRemove) &&\n _hasUserEditAccess(user, surveyInfo, userToRemove)\n\n// only survey admins (and system admins) can manage the user groups of a survey\nconst canManageUserGroups = (user: User, surveyInfo: Survey): boolean => {\n if (!user) return false\n if (Users.isSystemAdmin(user)) return true\n if (!surveyInfo) return false\n return _getSurveyUserGroup(user, surveyInfo)?.name === AuthGroupName.surveyAdmin\n}\n\n// qualifier attribute values are tied to user groups\nconst canEditQualifierAttributeValue = canManageUserGroups\n\nconst canEditUserAuthGroupExtraProps = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.isSystemAdmin(user) ||\n (canInviteUsers(user, surveyInfo) && _hasAuthGroupForSurvey({ user: userToUpdate, surveyInfo }))\n\nconst canEditUserSurveyManager = (user: User) => Users.isSystemAdmin(user)\nconst canEditUserMaxSurveys = (user: User) => Users.isSystemAdmin(user)\n\n// USER ACCESS REQUESTS\nconst canViewUsersAccessRequests = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditUsersAccessRequests = (user: User): boolean => Users.isSystemAdmin(user)\n\n// TWO-FACTOR AUTHENTICATION\nconst canManageUser2FADevices = (user: User): boolean => Users.isSystemAdmin(user)\n\n// INVITE\nconst getUserGroupsCanAssign = ({\n user,\n surveyInfo = null,\n showOnlySurveyGroups = false,\n}: {\n user: User\n surveyInfo?: Survey | null\n showOnlySurveyGroups?: boolean\n}) => {\n const groups = []\n\n let surveyGroups: AuthGroup[] = []\n if (surveyInfo) {\n if (surveyInfo.published) {\n surveyGroups = Surveys.getAuthGroups(surveyInfo)\n } else {\n const adminGroup = Surveys.getAuthGroupAdmin(surveyInfo)\n surveyGroups = adminGroup ? [adminGroup] : []\n }\n }\n\n // do not allow surveyEditor group selection (remove surveyEditor group completely?)\n surveyGroups = surveyGroups.filter((group) => group.name !== AuthGroupName.surveyEditor)\n\n groups.push(...surveyGroups)\n\n if (!showOnlySurveyGroups && (Users.isSystemAdmin(user) || Users.isSurveyManager(user))) {\n // Add SystemAdmin or SurveyManager group if current user is a SystemAdmin or SurveyManager himself\n const userNonSurveyGroups = Users.getAuthGroups(user).filter((group) => !AuthGroups.isSurveyGroup(group))\n groups.push(...userNonSurveyGroups)\n }\n return AuthGroups.sortGroups(groups)\n}\n\nexport const Authorizer = {\n // Survey\n canCreateSurvey,\n canCreateTemplate,\n getMaxSurveysUserCanCreate,\n canViewSurvey,\n canViewSurveyOrPublishedTemplate,\n canExportSurvey,\n canExportSurveysList,\n canViewTemplates,\n canEditSurvey,\n canEditSomeSurvey,\n canEditSurveyConfig,\n canEditSurveyOwner,\n canEditTemplates,\n canRefreshAllSurveyRdbs,\n // Record\n canCreateRecord,\n canViewRecord,\n canExportAllRecords,\n canViewNotOwnedRecords,\n canExportRecordsList,\n canEditRecord,\n canDeleteRecord,\n canDemoteRecord,\n canChangeRecordOwner,\n canChangeRecordStep,\n canCleanseRecords,\n canExportRecords,\n canImportRecords,\n canAnalyzeRecords,\n canUpdateRecordsStep,\n // Explorer\n canUseExplorer,\n // Map\n canUseMap,\n // Users\n canInviteUsers,\n canViewSurveyUsers,\n canViewUser,\n canViewOtherUsersEmail,\n canViewOtherUsersNameInSameSurvey,\n canViewAllUsers,\n canCreateUsers,\n canEditUser,\n canEditUserEmail,\n canEditUserGroup,\n canRemoveUser,\n canManageUserGroups,\n canEditQualifierAttributeValue,\n canEditUserAuthGroupExtraProps,\n canEditUserSurveyManager,\n canEditUserMaxSurveys,\n // User Access Requests\n canViewUsersAccessRequests,\n canEditUsersAccessRequests,\n // Two-factor authentication\n canManageUser2FADevices,\n // Invite\n getUserGroupsCanAssign,\n}\n"]}
|
|
1
|
+
{"version":3,"file":"authorizer.js","sourceRoot":"","sources":["../../../src/auth/authorizer.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,OAAO,EAAE,MAAM,WAAW,CAAA;AAChD,OAAO,EAAU,OAAO,EAAE,MAAM,WAAW,CAAA;AAC3C,OAAO,EAAa,aAAa,EAAE,MAAM,aAAa,CAAA;AACtD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;AACzC,OAAO,EAAE,UAAU,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAA;AAE/D,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE/B,SAAS;AACT,gBAAgB;AAChB,SAAS;AAET,MAAM,mBAAmB,GAAG,CAAC,IAAU,EAAE,UAAkB,EAAE,kBAAkB,GAAG,IAAI,EAAyB,EAAE,CAC/G,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,wBAAwB,CAAC,UAAU,CAAC,IAAI,EAAE,kBAAkB,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;AAEpG,+FAA+F;AAC/F,MAAM,sBAAsB,GAAG,CAAC,EAAE,IAAI,EAAE,UAAU,EAAsC,EAAW,EAAE,CACnG,OAAO,CAAC,mBAAmB,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,CAAC,CAAC,CAAA;AAEvD,MAAM,oBAAoB,GACxB,CAAC,UAAsB,EAAE,EAAE,CAC3B,CAAC,IAAU,EAAE,UAAkB,EAAW,EAAE;IAC1C,IAAI,CAAC,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B,WAAW;IACX,cAAc;IACd,MAAM;IACN,SAAS;IACT,QAAQ;IACR,cAAc;IACd,kBAAkB;IAClB,WAAW;IACX,sBAAsB;IACtB,iCAAiC;IACjC,eAAe;IACf,cAAc;IACd,WAAW;IACX,gBAAgB;IAChB,gBAAgB;IAChB,aAAa;IACb,mBAAmB;IACnB,8BAA8B;IAC9B,8BAA8B;IAC9B,wBAAwB;IACxB,qBAAqB;IACrB,uBAAuB;IACvB,0BAA0B;IAC1B,0BAA0B;IAC1B,4BAA4B;IAC5B,uBAAuB;IACvB,SAAS;IACT,sBAAsB;CACvB,CAAA","sourcesContent":["import { ArenaRecord, Records } from '../record'\nimport { Survey, Surveys } from '../survey'\nimport { AuthGroup, AuthGroupName } from './authGroup'\nimport { AuthGroups } from './authGroups'\nimport { Permission, RecordStepPermission } from './permission'\nimport { User } from './user'\nimport { Users } from './users'\n\n// ======\n// ====== Survey\n// ======\n\nconst _getSurveyUserGroup = (user: User, surveyInfo: Survey, includeSystemAdmin = true): AuthGroup | undefined =>\n surveyInfo ? Users.getAuthGroupBySurveyUuid(surveyInfo.uuid, includeSystemAdmin)(user) : undefined\n\n// system admins are not considered as members of the survey, so survey admins cannot edit them\nconst _hasAuthGroupForSurvey = ({ user, surveyInfo }: { user: User; surveyInfo: Survey }): boolean =>\n Boolean(_getSurveyUserGroup(user, surveyInfo, false))\n\nconst _hasSurveyPermission =\n (permission: Permission) =>\n (user: User, surveyInfo: Survey): boolean => {\n if (!user) return false\n\n if (Users.isSystemAdmin(user)) return true\n\n if (!surveyInfo) return false\n\n const authGroup = _getSurveyUserGroup(user, surveyInfo)\n if (!authGroup) return false\n\n return AuthGroups.getPermissions(authGroup).includes(permission)\n }\n\nconst _hasPermissionInSomeGroup =\n (permission: Permission) =>\n (user: User): boolean => {\n if (Users.isSystemAdmin(user)) return true\n const groups = Users.getAuthGroups(user)\n return groups.some((group) => AuthGroups.getPermissions(group).includes(permission))\n }\n\n// CREATE\nconst canCreateSurvey = _hasPermissionInSomeGroup(Permission.surveyCreate)\nconst canCreateTemplate = (user: User): boolean => Users.isSystemAdmin(user)\nconst getMaxSurveysUserCanCreate = (user: User): number => {\n if (Users.isSystemAdmin(user)) return Number.NaN\n if (canCreateSurvey(user)) return Users.getMaxSurveys(user)\n return 0\n}\n\n// READ\nconst canViewSurvey = (user: User, surveyInfo: Survey): boolean =>\n Users.isSystemAdmin(user) || _hasAuthGroupForSurvey({ user, surveyInfo })\n// any logged in user can view a published template (e.g. to clone it), without widening canViewSurvey\nconst canViewSurveyOrPublishedTemplate = (user: User, surveyInfo: Survey): boolean =>\n canViewSurvey(user, surveyInfo) || Boolean(user && surveyInfo?.template && surveyInfo.published)\nconst canExportSurvey = _hasSurveyPermission(Permission.recordAnalyse)\nconst canExportSurveysList = (user: User): boolean => Users.isSystemAdmin(user)\nconst canViewTemplates = (user: User): boolean => Users.isSystemAdmin(user)\n// UPDATE\nconst canEditSurvey = _hasSurveyPermission(Permission.surveyEdit)\nconst canEditSomeSurvey = _hasPermissionInSomeGroup(Permission.surveyEdit)\nconst canEditSurveyConfig = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditSurveyOwner = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditTemplates = (user: User): boolean => Users.isSystemAdmin(user)\nconst canRefreshAllSurveyRdbs = (user: User): boolean => Users.isSystemAdmin(user)\n\n// ======\n// ====== Record\n// ======\n\n// CREATE\nexport const canCreateRecord = _hasSurveyPermission(Permission.recordCreate)\n\n// READ\nexport const canViewRecord = _hasSurveyPermission(Permission.recordView)\nexport const canExportAllRecords = _hasSurveyPermission(Permission.recordCleanse)\nexport const canViewNotOwnedRecords = (user: User, surveyInfo: Survey): boolean => {\n if (!surveyInfo || !canViewSurvey(user, surveyInfo)) return false\n\n const { uuid: surveyUuid } = surveyInfo\n const groupInCurrentSurvey = Users.getAuthGroupBySurveyUuid(surveyUuid)(user)\n const groupName = groupInCurrentSurvey?.name\n\n if (groupName === AuthGroupName.dataEditor) {\n return Surveys.isDataEditorViewNotOwnedRecordsAllowed(surveyInfo)\n }\n if (groupName === AuthGroupName.dataAnalyst) {\n return Surveys.isDataAnalystViewNotOwnedRecordsAllowed(surveyInfo)\n }\n // survey guests can view (but not edit) all the records (restricted by user group qualifiers, if any)\n if (groupName === AuthGroupName.surveyGuest) return true\n if (canExportAllRecords(user, surveyInfo)) return true\n\n return false\n}\nexport const canExportRecordsList = _hasSurveyPermission(Permission.recordAnalyse)\n\n// UPDATE\nexport const canEditRecord = (user: User, record: ArenaRecord, allowAnalysisStepEdit = false): boolean => {\n if (\n !user ||\n !record ||\n // records in analysis cannot be edited\n (!allowAnalysisStepEdit && Records.isInAnalysisStep(record))\n ) {\n return false\n }\n // system admin does not have an auth group associated to the survey, but he can always edit records;\n if (Users.isSystemAdmin(user)) {\n return true\n }\n const { surveyUuid, ownerUuid } = record\n // if user doesn't have an auth group associated to the survey, he cannot edit records\n const userAuthGroup = Users.getAuthGroupBySurveyUuid(surveyUuid)(user)\n if (!userAuthGroup) return false\n\n // groups without the record edit permission (e.g. survey guests) cannot edit records,\n // even when their record steps permission allows it (record steps permissions are used also to view records)\n if (!AuthGroups.getPermissions(userAuthGroup).includes(Permission.recordEdit)) return false\n\n // Level = 'all' or 'own'. If 'own', user can only edit records assigned to him\n // If 'all', he can edit all survey's records\n // permission checks must not throw: a group without record steps cannot edit records\n const level = AuthGroups.getRecordSteps(userAuthGroup)?.[Records.getStep(record)]\n return level === RecordStepPermission.all || (level === RecordStepPermission.own && ownerUuid === user.uuid)\n}\n\n// survey level: the user's role allows editing records (e.g. not survey guests); see canEditRecord for a specific record\nconst canEditRecords = _hasSurveyPermission(Permission.recordEdit)\n\nconst canChangeRecordProps = (user: User, record: ArenaRecord) => canEditRecord(user, record, true)\n\nconst canDeleteRecord = canEditRecord\n\nconst canDemoteRecord = canChangeRecordProps\n\nconst canChangeRecordOwner = canChangeRecordProps\n\nconst canChangeRecordStep = canChangeRecordProps\n\nconst canCleanseRecords = _hasSurveyPermission(Permission.recordCleanse)\n\nconst canExportRecords = _hasSurveyPermission(Permission.recordView)\n\nconst canImportRecords = (user: User, surveyInfo: Survey) =>\n surveyInfo?.published && _hasSurveyPermission(Permission.recordCreate)(user, surveyInfo)\n\nconst canAnalyzeRecords = _hasSurveyPermission(Permission.recordAnalyse)\n\nconst canUpdateRecordsStep = canAnalyzeRecords\n\n// ======\n// ====== Explorer\n// ======\n\nconst canUseExplorer = canCleanseRecords\n\n// ======\n// ====== Map\n// ======\n\nconst canUseMap = canAnalyzeRecords\n\n// ======\n// ====== Users\n// ======\n\n// CREATE\nconst canInviteUsers = _hasSurveyPermission(Permission.userInvite)\n\n// READ\nconst canViewSurveyUsers = _hasSurveyPermission(Permission.userInvite)\n\nconst _usersBelongToSameSurvey = ({ user, userToView }: { user: User; userToView: User }) =>\n Users.getAuthGroups(user).some(\n (authGroupUser) =>\n AuthGroups.isSurveyGroup(authGroupUser) &&\n Users.getAuthGroups(userToView).some(\n (authGroupUserToView) =>\n AuthGroups.isSurveyGroup(authGroupUserToView) &&\n AuthGroups.getSurveyUuid(authGroupUserToView) === AuthGroups.getSurveyUuid(authGroupUser)\n )\n )\n\nconst canViewUser = (user: User, _surveyInfo: Survey, userToView: User) =>\n Users.isSystemAdmin(user) || Users.isEqual(userToView)(user) || _usersBelongToSameSurvey({ user, userToView })\n\nconst canViewOtherUsersEmail = ({ user, surveyInfo }: { user: User; surveyInfo: Survey }) =>\n Users.isSystemAdmin(user) || canInviteUsers(user, surveyInfo)\n\nconst canViewOtherUsersNameInSameSurvey = (user: User, surveyInfo: Survey): boolean =>\n _hasSurveyPermission(Permission.recordView)(user, surveyInfo)\n\nconst canViewAllUsers = (user: User) => Users.isSystemAdmin(user)\n\n// EDIT\nconst _hasUserEditAccess = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.isSystemAdmin(user) ||\n (_hasSurveyPermission(Permission.userEdit)(user, surveyInfo) &&\n // user to update has an auth group in the same survey\n _hasAuthGroupForSurvey({ user: userToUpdate, surveyInfo }))\n\nconst canCreateUsers = (user: User): boolean => Users.isSystemAdmin(user)\n\nconst canEditUser = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.hasAccepted(userToUpdate) &&\n (Users.isEqual(user)(userToUpdate) || _hasUserEditAccess(user, surveyInfo, userToUpdate))\n\nconst canEditUserEmail = (user: User): boolean => Users.isSystemAdmin(user)\n\nconst canEditUserGroup = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n !Users.isEqual(user)(userToUpdate) && _hasUserEditAccess(user, surveyInfo, userToUpdate)\n\nconst canRemoveUser = (user: User, surveyInfo: Survey, userToRemove: User): boolean =>\n !Users.isEqual(user)(userToRemove) &&\n !Users.isSystemAdmin(userToRemove) &&\n _hasUserEditAccess(user, surveyInfo, userToRemove)\n\n// only survey admins (and system admins) can manage the user groups of a survey\nconst canManageUserGroups = (user: User, surveyInfo: Survey): boolean => {\n if (!user) return false\n if (Users.isSystemAdmin(user)) return true\n if (!surveyInfo) return false\n return _getSurveyUserGroup(user, surveyInfo)?.name === AuthGroupName.surveyAdmin\n}\n\n// qualifier attribute values are tied to user groups\nconst canEditQualifierAttributeValue = canManageUserGroups\n\nconst canEditUserAuthGroupExtraProps = (user: User, surveyInfo: Survey, userToUpdate: User): boolean =>\n Users.isSystemAdmin(user) ||\n (canInviteUsers(user, surveyInfo) && _hasAuthGroupForSurvey({ user: userToUpdate, surveyInfo }))\n\nconst canEditUserSurveyManager = (user: User) => Users.isSystemAdmin(user)\nconst canEditUserMaxSurveys = (user: User) => Users.isSystemAdmin(user)\n\n// USER ACCESS REQUESTS\nconst canViewUsersAccessRequests = (user: User): boolean => Users.isSystemAdmin(user)\nconst canEditUsersAccessRequests = (user: User): boolean => Users.isSystemAdmin(user)\n\n// TWO-FACTOR AUTHENTICATION\nconst canManageUser2FADevices = (user: User): boolean => Users.isSystemAdmin(user)\n\n// INVITE\nconst getUserGroupsCanAssign = ({\n user,\n surveyInfo = null,\n showOnlySurveyGroups = false,\n}: {\n user: User\n surveyInfo?: Survey | null\n showOnlySurveyGroups?: boolean\n}) => {\n const groups = []\n\n let surveyGroups: AuthGroup[] = []\n if (surveyInfo) {\n if (surveyInfo.published) {\n surveyGroups = Surveys.getAuthGroups(surveyInfo)\n } else {\n const adminGroup = Surveys.getAuthGroupAdmin(surveyInfo)\n surveyGroups = adminGroup ? [adminGroup] : []\n }\n }\n\n // do not allow surveyEditor group selection (remove surveyEditor group completely?)\n surveyGroups = surveyGroups.filter((group) => group.name !== AuthGroupName.surveyEditor)\n\n groups.push(...surveyGroups)\n\n if (!showOnlySurveyGroups && (Users.isSystemAdmin(user) || Users.isSurveyManager(user))) {\n // Add SystemAdmin or SurveyManager group if current user is a SystemAdmin or SurveyManager himself\n const userNonSurveyGroups = Users.getAuthGroups(user).filter((group) => !AuthGroups.isSurveyGroup(group))\n groups.push(...userNonSurveyGroups)\n }\n return AuthGroups.sortGroups(groups)\n}\n\nexport const Authorizer = {\n // Survey\n canCreateSurvey,\n canCreateTemplate,\n getMaxSurveysUserCanCreate,\n canViewSurvey,\n canViewSurveyOrPublishedTemplate,\n canExportSurvey,\n canExportSurveysList,\n canViewTemplates,\n canEditSurvey,\n canEditSomeSurvey,\n canEditSurveyConfig,\n canEditSurveyOwner,\n canEditTemplates,\n canRefreshAllSurveyRdbs,\n // Record\n canCreateRecord,\n canViewRecord,\n canExportAllRecords,\n canViewNotOwnedRecords,\n canExportRecordsList,\n canEditRecord,\n canEditRecords,\n canDeleteRecord,\n canDemoteRecord,\n canChangeRecordOwner,\n canChangeRecordStep,\n canCleanseRecords,\n canExportRecords,\n canImportRecords,\n canAnalyzeRecords,\n canUpdateRecordsStep,\n // Explorer\n canUseExplorer,\n // Map\n canUseMap,\n // Users\n canInviteUsers,\n canViewSurveyUsers,\n canViewUser,\n canViewOtherUsersEmail,\n canViewOtherUsersNameInSameSurvey,\n canViewAllUsers,\n canCreateUsers,\n canEditUser,\n canEditUserEmail,\n canEditUserGroup,\n canRemoveUser,\n canManageUserGroups,\n canEditQualifierAttributeValue,\n canEditUserAuthGroupExtraProps,\n canEditUserSurveyManager,\n canEditUserMaxSurveys,\n // User Access Requests\n canViewUsersAccessRequests,\n canEditUsersAccessRequests,\n // Two-factor authentication\n canManageUser2FADevices,\n // Invite\n getUserGroupsCanAssign,\n}\n"]}
|
package/dist/esm/job/JobBase.js
CHANGED
|
@@ -50,6 +50,10 @@ export class JobBase {
|
|
|
50
50
|
this.beforeEndCalled = false;
|
|
51
51
|
// inner job events are handled in sequence; executeJobs waits for them before going on
|
|
52
52
|
this.innerJobEventsQueue = Promise.resolve();
|
|
53
|
+
// true while the job is executing in its own transaction: onEnd must run out of it
|
|
54
|
+
this.executingInTransaction = false;
|
|
55
|
+
// status change event of a job ended while executing in its transaction (e.g. when an inner job fails)
|
|
56
|
+
this.deferredEndEvent = null;
|
|
53
57
|
this.context = Object.assign({}, context);
|
|
54
58
|
this.innerJobs = innerJobs;
|
|
55
59
|
this.uuid = UUIDs.v4();
|
|
@@ -208,15 +212,7 @@ export class JobBase {
|
|
|
208
212
|
return;
|
|
209
213
|
this.logDebug('start');
|
|
210
214
|
try {
|
|
211
|
-
|
|
212
|
-
yield client.tx((tx) => __awaiter(this, void 0, void 0, function* () {
|
|
213
|
-
this.context.tx = tx;
|
|
214
|
-
yield this.executeInTransaction();
|
|
215
|
-
}));
|
|
216
|
-
}
|
|
217
|
-
else {
|
|
218
|
-
yield this.executeInTransaction();
|
|
219
|
-
}
|
|
215
|
+
yield this.executeInOwnTransaction(client);
|
|
220
216
|
if (!this.isCancelRequestedOrNotRunning()) {
|
|
221
217
|
yield this.setStatus(JobStatus.succeeded);
|
|
222
218
|
}
|
|
@@ -235,6 +231,28 @@ export class JobBase {
|
|
|
235
231
|
}
|
|
236
232
|
});
|
|
237
233
|
}
|
|
234
|
+
executeInOwnTransaction(client) {
|
|
235
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
236
|
+
this.executingInTransaction = true;
|
|
237
|
+
try {
|
|
238
|
+
if (client) {
|
|
239
|
+
yield client.tx((tx) => __awaiter(this, void 0, void 0, function* () {
|
|
240
|
+
this.context.tx = tx;
|
|
241
|
+
yield this.executeInTransaction();
|
|
242
|
+
}));
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
yield this.executeInTransaction();
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
finally {
|
|
249
|
+
this.executingInTransaction = false;
|
|
250
|
+
// the job ended while executing (e.g. an inner job failed, or it was canceled): onEnd runs now, out of the
|
|
251
|
+
// transaction (onEnd can use other connections, e.g. to delete data locked by the transaction)
|
|
252
|
+
yield this.endDeferred();
|
|
253
|
+
}
|
|
254
|
+
});
|
|
255
|
+
}
|
|
238
256
|
executeInTransaction() {
|
|
239
257
|
return __awaiter(this, void 0, void 0, function* () {
|
|
240
258
|
try {
|
|
@@ -374,6 +392,16 @@ export class JobBase {
|
|
|
374
392
|
if (status === JobStatus.failed) {
|
|
375
393
|
event.errors = this.errors;
|
|
376
394
|
}
|
|
395
|
+
if (this.isEnded() && this.executingInTransaction) {
|
|
396
|
+
// onEnd (and the event notifying the end) run when the transaction has ended
|
|
397
|
+
this.deferredEndEvent = event;
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
yield this.end(event);
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
end(event) {
|
|
404
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
377
405
|
if (this.isEnded()) {
|
|
378
406
|
this.logDebug('onEnd...');
|
|
379
407
|
yield this.onEnd();
|
|
@@ -382,6 +410,15 @@ export class JobBase {
|
|
|
382
410
|
this.notifyEvent(event);
|
|
383
411
|
});
|
|
384
412
|
}
|
|
413
|
+
endDeferred() {
|
|
414
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
415
|
+
const event = this.deferredEndEvent;
|
|
416
|
+
if (!event)
|
|
417
|
+
return;
|
|
418
|
+
this.deferredEndEvent = null;
|
|
419
|
+
yield this.end(event);
|
|
420
|
+
});
|
|
421
|
+
}
|
|
385
422
|
/**
|
|
386
423
|
* Inner job event handler.
|
|
387
424
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"JobBase.js","sourceRoot":"","sources":["../../../src/job/JobBase.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAGvD,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AAIhC,OAAO,EAAY,YAAY,EAAE,MAAM,YAAY,CAAA;AAEnD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAcpC,MAAM,qCAAqC,GAAG,IAAI,CAAA;AAElD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAgB,OAAO;IAiC3B,YAAmB,OAAU,EAAE,SAAS,GAAsB,EAAE;;QAxBhE,WAAM,GAAc,SAAS,CAAC,OAAO,CAAA;QAIrC,WAAM,GAAkB,SAAS,CAAA;QACjC,WAAM,GAAwB,EAAE,CAAA;QAMtB,yBAAoB,GAAG,CAAC,CAAC,CAAA;QAE3B,eAAU,GAAG,CAAC,CAAA;QACd,kBAAa,GAA+C,SAAS,CAAA;QACrE,8BAAyB,GAA8C,SAAS,CAAA;QAChF,gCAA2B,GAAG,CAAC,CAAA;QAC/B,2BAAsB,GAAG,IAAI,CAAA;QACrC,8FAA8F;QACtF,oBAAe,GAAG,KAAK,CAAA;QACvB,oBAAe,GAAG,KAAK,CAAA;QAC/B,uFAAuF;QAC/E,wBAAmB,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAA;QAG5D,IAAI,CAAC,OAAO,qBAAQ,OAAO,CAAE,CAAA;QAC7B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;QAE1B,IAAI,CAAC,IAAI,GAAG,KAAK,CAAC,EAAE,EAAE,CAAA;QACtB,IAAI,CAAC,IAAI,SAAG,IAAI,CAAC,OAAO,CAAC,IAAI,mCAAI,IAA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1F,GAAG,EAAE,CAAA;QACP,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,yBAAyB,GAAG,UAAU,CAAC,GAAG,EAAE,qCAAqC,GAAG,OAAO,CAAC,CAAA;QACnG,CAAC;IACH,CAAC;IAED;;OAEG;IACK,oBAAoB;QAC1B,IAAI,IAAI,CAAC,yBAAyB,EAAE,CAAC;YACnC,YAAY,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAA;YAC5C,IAAI,CAAC,yBAAyB,GAAG,SAAS,CAAA;QAC5C,CAAC;QACD,IAAI,CAAC,2BAA2B,GAAG,CAAC,CAAA;IACtC,CAAC;IAED;;;;OAIG;IACK,WAAW,CAAC,KAAe;QACjC,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAA;QACnC,IAAI,CAAC,QAAQ;YAAE,OAAM;QACrB,IAAI,CAAC;YACH,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAA;QACjF,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;QAC9B,CAAC;IACH,CAAC;IAEO,gBAAgB,CAAC,KAAU;;QACjC,IAAI,CAAC,QAAQ,CAAC,gCAAgC,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,KAAK,mCAAI,KAAK,EAAE,CAAC,CAAA;IACxE,CAAC;CACF;AAhnBiB,mBAAW,GAAG;IAC5B,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;CACb,AAJ0B,CAI1B","sourcesContent":["import { User } from '../auth'\nimport { Dictionary } from '../common'\nimport { SystemError, SystemErrorKeys } from '../error'\nimport { Logger } from '../logger'\nimport { Survey } from '../survey'\nimport { UUIDs } from '../utils'\n\nimport { Job } from './job'\nimport { JobContext } from './jobContext'\nimport { JobEvent, JobEventType } from './jobEvent'\nimport { JobSerialized } from './jobSerialized'\nimport { JobStatus } from './status'\n\nexport interface JobConstructor {\n new <C extends JobContext, R>(context: C, innerJobs?: JobBase<any>[]): JobBase<C, R>\n readonly prototype: JobBase<any, any>\n}\n\n/**\n * Client able to open a transaction (e.g. a pg-promise database): the job runs inside `client.tx(callback)`.\n */\nexport interface JobTxClient {\n tx(callback: (tx: any) => Promise<void>): Promise<unknown>\n}\n\nconst PROGRESS_NOTIFICATION_THROTTLE_MILLIS = 1000\n\n/**\n * Asynchronous task handler.\n *\n * Status workflow:\n * - pending\n * - running\n * - (end)\n * -- succeeded\n * -- failed\n * -- canceled\n *\n * Methods that can be overwritten by subclasses:\n * - shouldExecute (in tx)\n * - onStart (in tx)\n * - execute (in tx)\n * - beforeSuccess (in tx)\n * - generateResult (in tx)\n * - beforeEnd (in tx)\n * - onEnd (out of tx)\n * - getErrorInfo\n */\nexport abstract class JobBase<C extends JobContext, R = undefined> implements Job<R> {\n static readonly keysContext = {\n surveyId: 'surveyId',\n survey: 'survey',\n user: 'user',\n }\n\n readonly uuid: string\n readonly type: string\n status: JobStatus = JobStatus.pending\n startTime?: Date\n endTime?: Date\n total: number\n result: R | undefined = undefined\n errors: Record<string, any> = {}\n canceledByAdmin?: boolean\n\n protected logger: Logger\n protected context: C\n protected innerJobs: JobBase<C, any>[]\n protected currentInnerJobIndex = -1\n\n private _processed = 0\n private eventListener: ((event: JobEvent) => unknown) | undefined = undefined\n private progressThrottleTimeoutId: ReturnType<typeof setTimeout> | undefined = undefined\n private progressThrottleLastRunTime = 0\n private _stopOnInnerJobFailure = true\n // set as soon as cancel() is called (the canceled status is set only after beforeEnd has run)\n private cancelRequested = false\n private beforeEndCalled = false\n // inner job events are handled in sequence; executeJobs waits for them before going on\n private innerJobEventsQueue: Promise<void> = Promise.resolve()\n\n public constructor(context: C, innerJobs: JobBase<C, any>[] = []) {\n this.context = { ...context }\n this.innerJobs = innerJobs\n\n this.uuid = UUIDs.v4()\n this.type = this.context.type ?? this.constructor.name\n this.total = innerJobs.length > 0 ? innerJobs.length : 1\n\n // Created last, so that createLogger() overrides can safely build the logger name out of\n // the job identity (e.g. arena's `Job <ClassName> (<uuid>)` log correlation id).\n this.logger = this.createLogger()\n }\n\n get processed(): number {\n return this._processed\n }\n\n set processed(value: number) {\n this._processed = value\n this.notifyProgress()\n }\n\n get stopOnInnerJobFailure(): boolean {\n return this._stopOnInnerJobFailure\n }\n\n set stopOnInnerJobFailure(value: boolean) {\n this._stopOnInnerJobFailure = value\n }\n\n protected get surveyId(): number | null {\n return this.context.surveyId ?? null\n }\n\n protected get user(): User {\n return this.context.user\n }\n\n protected get userUuid(): string | undefined {\n return this.user?.uuid\n }\n\n protected get contextSurvey(): Survey | undefined {\n return this.context.survey\n }\n\n /**\n * Returns the value of the specified context prop, or the default value (null if not specified) when not set.\n */\n protected getContextProp<K extends keyof C & string>(prop: K, defaultValue?: C[K] | null): C[K] | null\n /** Context prop not declared in the context type: the value is typed by the caller. */\n protected getContextProp<T = any, P extends string = string>(\n prop: P extends keyof C ? never : P,\n defaultValue?: T | null\n ): T | null\n protected getContextProp(prop: string, defaultValue: unknown = null): unknown {\n const value = (this.context as Record<string, unknown>)[prop]\n return value ?? defaultValue\n }\n\n protected setContext(context: Partial<C>): void {\n Object.assign(this.context, context)\n }\n\n protected deleteContextProps(...propNames: string[]): void {\n propNames.forEach((propName) => {\n delete (this.context as any)[propName]\n })\n }\n\n /**\n * Registers the listener notified of job events (status changes and progress).\n * Only one listener can be registered at a time; registering a new one replaces the previous one.\n * Errors thrown (or rejections returned) by the listener are logged and do not affect the job execution.\n */\n onEvent(listener: (event: JobEvent) => unknown): this {\n this.eventListener = listener\n return this\n }\n\n isPending(): boolean {\n return this.status === JobStatus.pending\n }\n\n isRunning(): boolean {\n return this.status === JobStatus.running\n }\n\n isSucceeded(): boolean {\n return this.status === JobStatus.succeeded\n }\n\n isFailed(): boolean {\n return this.status === JobStatus.failed\n }\n\n isCanceled(): boolean {\n return this.status === JobStatus.canceled\n }\n\n isEnded(): boolean {\n return [JobStatus.succeeded, JobStatus.failed, JobStatus.canceled].includes(this.status)\n }\n\n hasErrors(): boolean {\n return Object.keys(this.errors).length > 0\n }\n\n protected getCurrentInnerJob(): JobBase<C, any> | undefined {\n return this.innerJobs[this.currentInnerJobIndex]\n }\n\n protected combineInnerJobsResults(): Record<string, any> {\n const results: Record<string, any> = {}\n this.innerJobs.forEach((innerJob) => Object.assign(results, innerJob.result ?? {}))\n return results\n }\n\n protected combineInnerJobsErrors(): Record<string, any> {\n const errors: Record<string, any> = {}\n this.innerJobs.forEach((innerJob) => JobBase.mergeInnerJobErrors(errors, innerJob))\n return errors\n }\n\n /**\n * Adds the errors of the inner job to the specified errors.\n * Error keys already in use (e.g. the same item number used by two inner jobs) are prefixed with the inner job type\n * (and suffixed with a counter if still in use), so that no error gets overwritten.\n */\n private static mergeInnerJobErrors(errors: Record<string, any>, innerJob: JobBase<any, any>): void {\n Object.entries(innerJob.errors ?? {}).forEach(([key, error]) => {\n let uniqueKey = key\n if (uniqueKey in errors) {\n const prefixedKey = `${innerJob.type} ${key}`\n uniqueKey = prefixedKey\n let count = 2\n while (uniqueKey in errors) {\n uniqueKey = `${prefixedKey} (${count})`\n count += 1\n }\n }\n errors[uniqueKey] = error\n })\n }\n\n async cancel(options: { canceledByAdmin?: boolean } = {}): Promise<void> {\n const { canceledByAdmin = false } = options\n if (this.isEnded()) return\n\n this.cancelRequested = true\n\n const currentInnerJob = this.getCurrentInnerJob()\n if (currentInnerJob && !currentInnerJob.isEnded()) {\n // inner job running or about to start (pending):\n // the parent job status will be set to canceled when handling the canceled event of the inner job\n await currentInnerJob.cancel({ canceledByAdmin })\n return\n }\n // no inner job running (job without inner jobs, or between two inner jobs)\n this.canceledByAdmin = canceledByAdmin\n // release the resources (temp files/directories, streams) allocated before the cancellation\n await this.runBeforeEnd()\n await this.setStatus(JobStatus.canceled)\n }\n\n /**\n * Returns true when the job has been canceled (or is no longer running).\n * Long-running subclasses can check it to stop processing items (e.g. in a loop) as soon as the job is canceled.\n */\n protected isCancelRequestedOrNotRunning(): boolean {\n return this.cancelRequested || !this.isRunning()\n }\n\n /**\n * Runs beforeEnd only once (it can be called by cancel() and when the job execution ends).\n */\n private async runBeforeEnd(): Promise<void> {\n if (this.beforeEndCalled) return\n this.beforeEndCalled = true\n await this.beforeEnd()\n }\n\n async start(client: JobTxClient | null = null): Promise<void> {\n // canceled before being started: nothing to execute\n if (this.cancelRequested || this.isEnded()) return\n\n this.logDebug('start')\n\n try {\n if (client) {\n await client.tx(async (tx: any) => {\n this.context.tx = tx\n await this.executeInTransaction()\n })\n } else {\n await this.executeInTransaction()\n }\n if (!this.isCancelRequestedOrNotRunning()) {\n await this.setStatus(JobStatus.succeeded)\n }\n } catch (error: any) {\n // a job can fail while still pending (e.g. the transaction cannot be opened, or onStart fails)\n if (!this.cancelRequested && !this.isFailed() && !this.isCanceled()) {\n this.logError(error.stack ?? error)\n const { key, params } = this.getErrorInfo(error)\n this.addError({ error: { valid: false, errors: [{ key, params }] } })\n await this.setStatus(JobStatus.failed)\n }\n } finally {\n this.context.tx = undefined\n }\n }\n\n private async executeInTransaction(): Promise<void> {\n try {\n // canceled while the transaction was being opened: it must not be set as running again\n if (this.cancelRequested) return\n\n await this.onStart()\n\n const shouldExecute = await this.shouldExecute()\n if (shouldExecute) {\n if (this.innerJobs.length > 0) {\n await this.executeJobs()\n } else {\n await this.execute()\n }\n if (!this.isCancelRequestedOrNotRunning()) {\n await this.beforeSuccess()\n }\n }\n } finally {\n // runs only if not already run by cancel()\n await this.runBeforeEnd()\n this.context.tx = undefined\n }\n\n if (this.isCancelRequestedOrNotRunning()) {\n this.throwError(SystemErrorKeys.job.canceledOrErrorsFound)\n }\n }\n\n /**\n * Generates a plain JSON-serializable representation of the job, including its inner jobs.\n */\n toJSON(): JobSerialized<R> {\n const { errors, processed, result, status, total, type, userUuid, uuid } = this\n\n return {\n uuid,\n type,\n userUuid,\n surveyId: this.surveyId,\n innerJobs: this.innerJobs.map((job) => job.toJSON()),\n currentInnerJobIndex: this.currentInnerJobIndex,\n\n status,\n pending: this.isPending(),\n running: this.isRunning(),\n succeeded: this.isSucceeded(),\n canceled: this.isCanceled(),\n failed: this.isFailed(),\n ended: this.isEnded(),\n\n total,\n processed,\n progressPercent: this.calculateProgressPercent(),\n elapsedMillis: this.calculateElapsedMillis(),\n\n errors: this.isFailed() ? errors : undefined,\n result: this.isSucceeded() ? result : undefined,\n }\n }\n\n private async executeJobs(): Promise<void> {\n this.logDebug(`- ${this.total} inner jobs found`)\n\n for (let i = 0; i < this.innerJobs.length; i++) {\n // canceled between two inner jobs: the next ones must not be started\n if (this.cancelRequested) break\n\n this.logDebug(`- executing inner job ${i + 1}`)\n this.currentInnerJobIndex = i\n const currentInnerJob = this.innerJobs[i]\n if (currentInnerJob.context) {\n Object.assign(this.context, currentInnerJob.context)\n }\n currentInnerJob.context = this.context\n currentInnerJob.onEvent((event) => {\n // handle the inner job events in sequence; errors are logged (they would be unhandled rejections otherwise)\n this.innerJobEventsQueue = this.innerJobEventsQueue\n .then(() => this.onInnerJobEvent(event))\n .catch((error) => this.logError(error?.stack ?? error))\n })\n\n // The inner job shares this very context object, and its own start()/executeInTransaction()\n // clear `context.tx` when they terminate: without saving and restoring it here, the first\n // inner job would wipe the parent's transaction handle, leaving every following inner job\n // (and the parent's own beforeSuccess()/beforeEnd()) running outside of the transaction.\n // start() never throws (it swallows/records errors internally), so a plain restore is enough.\n const parentTx = this.context.tx\n await currentInnerJob.start(parentTx)\n this.context.tx = parentTx\n // the inner job events (e.g. canceled, failed) update the status of this job\n await this.innerJobEventsQueue\n\n if (this.cancelRequested || this.isCanceled() || currentInnerJob.isCanceled()) {\n break\n }\n if (currentInnerJob.isSucceeded()) {\n this.incrementProcessedItems()\n } else if (this.stopOnInnerJobFailure) {\n break\n }\n }\n if (\n !this.stopOnInnerJobFailure &&\n !this.cancelRequested &&\n this.innerJobs.some((innerJob) => innerJob.isFailed())\n ) {\n // set only after all inner jobs have run: setting it at the first failure would end this job (onEnd) too early\n await this.runBeforeEnd()\n await this.setStatus(JobStatus.failed)\n }\n\n this.logDebug(`- ${this.processed} inner jobs processed successfully`)\n }\n\n /**\n * Runs the job's own work when it has no inner jobs. Default implementation is a no-op, so\n * subclasses that only supply inner jobs (and never override this) don't need to. It also means\n * a subclass can safely call `super.execute()` defensively without knowing whether any ancestor\n * overrides it - if this were `abstract` instead, TypeScript would compile away any runtime\n * property for it entirely, so `super.execute()` would throw the moment no ancestor overrides it\n * (confirmed in production: a subclass whose immediate parent didn't override `execute()` crashed\n * with \"(intermediate value).execute is not a function\" the instant it called `super.execute()`).\n */\n protected async execute(): Promise<void> {\n // to be extended by subclasses\n }\n\n /**\n * Determines whether the job should actually run.\n * When it returns false, \"execute\" (or the inner jobs) will be skipped, but the job will still succeed.\n */\n protected shouldExecute(): Promise<boolean> {\n return Promise.resolve(true)\n }\n\n protected incrementProcessedItems(incrementBy = 1): void {\n this.processed += incrementBy\n }\n\n protected async setStatus(status: JobStatus): Promise<void> {\n this.logDebug(`set status: ${status}`)\n this.status = status\n\n const event: JobEvent = { type: JobEventType.statusChange, status, total: this.total, processed: this.processed }\n if (status === JobStatus.failed) {\n event.errors = this.errors\n }\n\n if (this.isEnded()) {\n this.logDebug('onEnd...')\n await this.onEnd()\n this.logDebug(`onEnd run. Job completed in ${this.elapsedTimePrettyFormat}`)\n }\n\n this.notifyEvent(event)\n }\n\n /**\n * Inner job event handler.\n */\n protected async onInnerJobEvent(event: JobEvent): Promise<void> {\n const { status } = event\n if (status === JobStatus.canceled) {\n this.canceledByAdmin = this.getCurrentInnerJob()?.canceledByAdmin ?? false\n // beforeEnd must run before onEnd (it runs only once: it won't run again when the execution ends)\n await this.runBeforeEnd()\n return this.setStatus(status)\n }\n if (status === JobStatus.failed) {\n // report the errors of the failed inner job in the failed event (and in the errors) of this job\n const currentInnerJob = this.getCurrentInnerJob()\n if (currentInnerJob) {\n JobBase.mergeInnerJobErrors(this.errors, currentInnerJob)\n }\n // when not stopping on inner job failures, this job is set as failed after all the inner jobs have run\n if (this.stopOnInnerJobFailure) {\n await this.runBeforeEnd()\n await this.setStatus(status)\n }\n return\n }\n if (status === JobStatus.running) {\n this.notifyEvent({\n type: JobEventType.progress,\n status: this.status,\n total: this.total,\n processed: this.processed,\n })\n return\n }\n this.logDebug(`Unknown inner job status: ${status}`)\n }\n\n /**\n * Called when the job just has been started.\n */\n protected async onStart(): Promise<void> {\n this.startTime = new Date()\n await this.setStatus(JobStatus.running)\n }\n\n /**\n * Called before beforeEnd only if the status will change to 'success'.\n * Default implementation stores the value returned by generateResult() via setResult().\n * It runs INSIDE the current db transaction.\n */\n protected async beforeSuccess(): Promise<void> {\n this.setResult(await this.generateResult())\n }\n\n /**\n * Computes the value beforeSuccess() will store as this job's result.\n * Default implementation returns whatever is already in `this.result`\n * (e.g. accumulated via earlier setResult() calls during execute()).\n */\n protected generateResult(): Promise<R | undefined> {\n return Promise.resolve(this.result)\n }\n\n /**\n * Updates this job's result. When both the current and incoming values are plain objects,\n * they are merged (Object.assign); otherwise the incoming value replaces the current one.\n * This keeps it safe for subclasses whose result is a primitive (e.g. a plain number) as\n * well as ones that build an object result incrementally across multiple setResult() calls.\n */\n protected setResult(result: R | undefined): void {\n if (result && typeof result === 'object' && this.result && typeof this.result === 'object') {\n Object.assign(this.result, result)\n } else {\n this.result = result\n }\n }\n\n /**\n * Called before onEnd. Useful for flushing resources used by the job before it terminates completely.\n * It runs INSIDE the current db transaction.\n */\n protected beforeEnd(): Promise<void> {\n this.logDebug('Cleanup')\n return Promise.resolve()\n }\n\n /**\n * Called when the job status changes to success, failed or canceled\n * (it runs OUTSIDE of the current db transaction)\n */\n protected onEnd(): Promise<void> {\n this.endTime = new Date()\n this.cancelNotifyProgress()\n return Promise.resolve()\n }\n\n protected addError(error: any, errorKey?: string): void {\n const key = errorKey ?? String(this.processed + 1)\n this.errors[key] = error\n }\n\n protected throwError(errorKey: string, params?: Dictionary<any>): void {\n // SystemError: getErrorInfo will report the translatable key instead of a generic error\n throw new SystemError(errorKey, params)\n }\n\n protected getErrorInfo(error: any): { key: string; params: Record<string, any> } {\n if (error instanceof SystemError) {\n return { key: `appErrors:${error.key}`, params: error.params }\n }\n return { key: 'appErrors:generic', params: { text: error.toString() } }\n }\n\n protected abstract createLogger(): Logger\n\n protected logDebug(...msgs: any[]): void {\n this.logger.debug(...msgs)\n }\n\n protected logInfo(...msgs: any[]): void {\n this.logger.info(...msgs)\n }\n\n protected logWarn(...msgs: any[]): void {\n this.logger.warn(...msgs)\n }\n\n protected logError(...msgs: any[]): void {\n this.logger.error(...msgs)\n }\n\n private get elapsedTimePrettyFormat(): string {\n const elapsedTime = this.calculateElapsedMillis()\n const elapsedMins = Math.floor(elapsedTime / 1000 / 60)\n const elapsedSeconds = String(Math.floor(elapsedTime / 1000) % 60).padStart(2, '0')\n const elapsedMillis = String(elapsedTime % 1000).padStart(3, '0')\n return `${elapsedMins}:${elapsedSeconds}.${elapsedMillis}`\n }\n\n private calculatePartialProgress(): number {\n if (this.isSucceeded()) return 100\n if (this.total > 0) return Math.floor((100 * this.processed) / this.total)\n return 0\n }\n\n private calculateProgressPercent(): number {\n const partialProgress = this.calculatePartialProgress()\n const currentInnerJob = this.getCurrentInnerJob()\n if (\n !currentInnerJob ||\n this.currentInnerJobIndex < 0 ||\n partialProgress === 100 ||\n this.processed > this.currentInnerJobIndex\n ) {\n return partialProgress\n }\n return partialProgress + Math.floor(currentInnerJob.calculateProgressPercent() / this.total)\n }\n\n private calculateElapsedMillis(): number {\n if (!this.startTime) return 0\n return (this.endTime ?? new Date()).getTime() - this.startTime.getTime()\n }\n\n /**\n * Notifies a progress event to the registered listener, throttled so that fast-changing\n * progress does not flood consumers (e.g. web socket clients).\n */\n private notifyProgress(): void {\n // onEnd has already cleared the throttle: no progress events after the job has ended\n if (this.isEnded()) return\n\n const run = (): void => {\n this.progressThrottleTimeoutId = undefined\n this.progressThrottleLastRunTime = Date.now()\n this.notifyEvent({\n type: JobEventType.progress,\n status: this.status,\n total: this.total,\n processed: this.processed,\n })\n }\n\n if (this.progressThrottleTimeoutId) return\n\n const elapsed = Date.now() - this.progressThrottleLastRunTime\n if (!this.progressThrottleLastRunTime || elapsed >= PROGRESS_NOTIFICATION_THROTTLE_MILLIS) {\n run()\n } else {\n this.progressThrottleTimeoutId = setTimeout(run, PROGRESS_NOTIFICATION_THROTTLE_MILLIS - elapsed)\n }\n }\n\n /**\n * Cancels any pending throttled progress notification and resets the throttle state.\n */\n private cancelNotifyProgress(): void {\n if (this.progressThrottleTimeoutId) {\n clearTimeout(this.progressThrottleTimeoutId)\n this.progressThrottleTimeoutId = undefined\n }\n this.progressThrottleLastRunTime = 0\n }\n\n /**\n * Notifies the event to the registered listener.\n * Listener errors are only logged: they would otherwise fail the job or, when thrown inside the progress\n * throttle timeout, crash the process.\n */\n private notifyEvent(event: JobEvent): void {\n const listener = this.eventListener\n if (!listener) return\n try {\n Promise.resolve(listener(event)).catch((error) => this.logListenerError(error))\n } catch (error) {\n this.logListenerError(error)\n }\n }\n\n private logListenerError(error: any): void {\n this.logError(`error in job event listener: ${error?.stack ?? error}`)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"JobBase.js","sourceRoot":"","sources":["../../../src/job/JobBase.ts"],"names":[],"mappings":";;;;;;;;;AAEA,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,UAAU,CAAA;AAGvD,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAA;AAIhC,OAAO,EAAY,YAAY,EAAE,MAAM,YAAY,CAAA;AAEnD,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAA;AAcpC,MAAM,qCAAqC,GAAG,IAAI,CAAA;AAElD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,OAAgB,OAAO;IAqC3B,YAAmB,OAAU,EAAE,SAAS,GAAsB,EA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gC,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,KAAK,mCAAI,KAAK,EAAE,CAAC,CAAA;IACxE,CAAC;CACF;AAhpBiB,mBAAW,GAAG;IAC5B,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;CACb,AAJ0B,CAI1B","sourcesContent":["import { User } from '../auth'\nimport { Dictionary } from '../common'\nimport { SystemError, SystemErrorKeys } from '../error'\nimport { Logger } from '../logger'\nimport { Survey } from '../survey'\nimport { UUIDs } from '../utils'\n\nimport { Job } from './job'\nimport { JobContext } from './jobContext'\nimport { JobEvent, JobEventType } from './jobEvent'\nimport { JobSerialized } from './jobSerialized'\nimport { JobStatus } from './status'\n\nexport interface JobConstructor {\n new <C extends JobContext, R>(context: C, innerJobs?: JobBase<any>[]): JobBase<C, R>\n readonly prototype: JobBase<any, any>\n}\n\n/**\n * Client able to open a transaction (e.g. a pg-promise database): the job runs inside `client.tx(callback)`.\n */\nexport interface JobTxClient {\n tx(callback: (tx: any) => Promise<void>): Promise<unknown>\n}\n\nconst PROGRESS_NOTIFICATION_THROTTLE_MILLIS = 1000\n\n/**\n * Asynchronous task handler.\n *\n * Status workflow:\n * - pending\n * - running\n * - (end)\n * -- succeeded\n * -- failed\n * -- canceled\n *\n * Methods that can be overwritten by subclasses:\n * - shouldExecute (in tx)\n * - onStart (in tx)\n * - execute (in tx)\n * - beforeSuccess (in tx)\n * - generateResult (in tx)\n * - beforeEnd (in tx)\n * - onEnd (out of tx)\n * - getErrorInfo\n */\nexport abstract class JobBase<C extends JobContext, R = undefined> implements Job<R> {\n static readonly keysContext = {\n surveyId: 'surveyId',\n survey: 'survey',\n user: 'user',\n }\n\n readonly uuid: string\n readonly type: string\n status: JobStatus = JobStatus.pending\n startTime?: Date\n endTime?: Date\n total: number\n result: R | undefined = undefined\n errors: Record<string, any> = {}\n canceledByAdmin?: boolean\n\n protected logger: Logger\n protected context: C\n protected innerJobs: JobBase<C, any>[]\n protected currentInnerJobIndex = -1\n\n private _processed = 0\n private eventListener: ((event: JobEvent) => unknown) | undefined = undefined\n private progressThrottleTimeoutId: ReturnType<typeof setTimeout> | undefined = undefined\n private progressThrottleLastRunTime = 0\n private _stopOnInnerJobFailure = true\n // set as soon as cancel() is called (the canceled status is set only after beforeEnd has run)\n private cancelRequested = false\n private beforeEndCalled = false\n // inner job events are handled in sequence; executeJobs waits for them before going on\n private innerJobEventsQueue: Promise<void> = Promise.resolve()\n // true while the job is executing in its own transaction: onEnd must run out of it\n private executingInTransaction = false\n // status change event of a job ended while executing in its transaction (e.g. when an inner job fails)\n private deferredEndEvent: JobEvent | null = null\n\n public constructor(context: C, innerJobs: JobBase<C, any>[] = []) {\n this.context = { ...context }\n this.innerJobs = innerJobs\n\n this.uuid = UUIDs.v4()\n this.type = this.context.type ?? this.constructor.name\n this.total = innerJobs.length > 0 ? innerJobs.length : 1\n\n // Created last, so that createLogger() overrides can safely build the logger name out of\n // the job identity (e.g. arena's `Job <ClassName> (<uuid>)` log correlation id).\n this.logger = this.createLogger()\n }\n\n get processed(): number {\n return this._processed\n }\n\n set processed(value: number) {\n this._processed = value\n this.notifyProgress()\n }\n\n get stopOnInnerJobFailure(): boolean {\n return this._stopOnInnerJobFailure\n }\n\n set stopOnInnerJobFailure(value: boolean) {\n this._stopOnInnerJobFailure = value\n }\n\n protected get surveyId(): number | null {\n return this.context.surveyId ?? null\n }\n\n protected get user(): User {\n return this.context.user\n }\n\n protected get userUuid(): string | undefined {\n return this.user?.uuid\n }\n\n protected get contextSurvey(): Survey | undefined {\n return this.context.survey\n }\n\n /**\n * Returns the value of the specified context prop, or the default value (null if not specified) when not set.\n */\n protected getContextProp<K extends keyof C & string>(prop: K, defaultValue?: C[K] | null): C[K] | null\n /** Context prop not declared in the context type: the value is typed by the caller. */\n protected getContextProp<T = any, P extends string = string>(\n prop: P extends keyof C ? never : P,\n defaultValue?: T | null\n ): T | null\n protected getContextProp(prop: string, defaultValue: unknown = null): unknown {\n const value = (this.context as Record<string, unknown>)[prop]\n return value ?? defaultValue\n }\n\n protected setContext(context: Partial<C>): void {\n Object.assign(this.context, context)\n }\n\n protected deleteContextProps(...propNames: string[]): void {\n propNames.forEach((propName) => {\n delete (this.context as any)[propName]\n })\n }\n\n /**\n * Registers the listener notified of job events (status changes and progress).\n * Only one listener can be registered at a time; registering a new one replaces the previous one.\n * Errors thrown (or rejections returned) by the listener are logged and do not affect the job execution.\n */\n onEvent(listener: (event: JobEvent) => unknown): this {\n this.eventListener = listener\n return this\n }\n\n isPending(): boolean {\n return this.status === JobStatus.pending\n }\n\n isRunning(): boolean {\n return this.status === JobStatus.running\n }\n\n isSucceeded(): boolean {\n return this.status === JobStatus.succeeded\n }\n\n isFailed(): boolean {\n return this.status === JobStatus.failed\n }\n\n isCanceled(): boolean {\n return this.status === JobStatus.canceled\n }\n\n isEnded(): boolean {\n return [JobStatus.succeeded, JobStatus.failed, JobStatus.canceled].includes(this.status)\n }\n\n hasErrors(): boolean {\n return Object.keys(this.errors).length > 0\n }\n\n protected getCurrentInnerJob(): JobBase<C, any> | undefined {\n return this.innerJobs[this.currentInnerJobIndex]\n }\n\n protected combineInnerJobsResults(): Record<string, any> {\n const results: Record<string, any> = {}\n this.innerJobs.forEach((innerJob) => Object.assign(results, innerJob.result ?? {}))\n return results\n }\n\n protected combineInnerJobsErrors(): Record<string, any> {\n const errors: Record<string, any> = {}\n this.innerJobs.forEach((innerJob) => JobBase.mergeInnerJobErrors(errors, innerJob))\n return errors\n }\n\n /**\n * Adds the errors of the inner job to the specified errors.\n * Error keys already in use (e.g. the same item number used by two inner jobs) are prefixed with the inner job type\n * (and suffixed with a counter if still in use), so that no error gets overwritten.\n */\n private static mergeInnerJobErrors(errors: Record<string, any>, innerJob: JobBase<any, any>): void {\n Object.entries(innerJob.errors ?? {}).forEach(([key, error]) => {\n let uniqueKey = key\n if (uniqueKey in errors) {\n const prefixedKey = `${innerJob.type} ${key}`\n uniqueKey = prefixedKey\n let count = 2\n while (uniqueKey in errors) {\n uniqueKey = `${prefixedKey} (${count})`\n count += 1\n }\n }\n errors[uniqueKey] = error\n })\n }\n\n async cancel(options: { canceledByAdmin?: boolean } = {}): Promise<void> {\n const { canceledByAdmin = false } = options\n if (this.isEnded()) return\n\n this.cancelRequested = true\n\n const currentInnerJob = this.getCurrentInnerJob()\n if (currentInnerJob && !currentInnerJob.isEnded()) {\n // inner job running or about to start (pending):\n // the parent job status will be set to canceled when handling the canceled event of the inner job\n await currentInnerJob.cancel({ canceledByAdmin })\n return\n }\n // no inner job running (job without inner jobs, or between two inner jobs)\n this.canceledByAdmin = canceledByAdmin\n // release the resources (temp files/directories, streams) allocated before the cancellation\n await this.runBeforeEnd()\n await this.setStatus(JobStatus.canceled)\n }\n\n /**\n * Returns true when the job has been canceled (or is no longer running).\n * Long-running subclasses can check it to stop processing items (e.g. in a loop) as soon as the job is canceled.\n */\n protected isCancelRequestedOrNotRunning(): boolean {\n return this.cancelRequested || !this.isRunning()\n }\n\n /**\n * Runs beforeEnd only once (it can be called by cancel() and when the job execution ends).\n */\n private async runBeforeEnd(): Promise<void> {\n if (this.beforeEndCalled) return\n this.beforeEndCalled = true\n await this.beforeEnd()\n }\n\n async start(client: JobTxClient | null = null): Promise<void> {\n // canceled before being started: nothing to execute\n if (this.cancelRequested || this.isEnded()) return\n\n this.logDebug('start')\n\n try {\n await this.executeInOwnTransaction(client)\n if (!this.isCancelRequestedOrNotRunning()) {\n await this.setStatus(JobStatus.succeeded)\n }\n } catch (error: any) {\n // a job can fail while still pending (e.g. the transaction cannot be opened, or onStart fails)\n if (!this.cancelRequested && !this.isFailed() && !this.isCanceled()) {\n this.logError(error.stack ?? error)\n const { key, params } = this.getErrorInfo(error)\n this.addError({ error: { valid: false, errors: [{ key, params }] } })\n await this.setStatus(JobStatus.failed)\n }\n } finally {\n this.context.tx = undefined\n }\n }\n\n private async executeInOwnTransaction(client: JobTxClient | null): Promise<void> {\n this.executingInTransaction = true\n try {\n if (client) {\n await client.tx(async (tx: any) => {\n this.context.tx = tx\n await this.executeInTransaction()\n })\n } else {\n await this.executeInTransaction()\n }\n } finally {\n this.executingInTransaction = false\n // the job ended while executing (e.g. an inner job failed, or it was canceled): onEnd runs now, out of the\n // transaction (onEnd can use other connections, e.g. to delete data locked by the transaction)\n await this.endDeferred()\n }\n }\n\n private async executeInTransaction(): Promise<void> {\n try {\n // canceled while the transaction was being opened: it must not be set as running again\n if (this.cancelRequested) return\n\n await this.onStart()\n\n const shouldExecute = await this.shouldExecute()\n if (shouldExecute) {\n if (this.innerJobs.length > 0) {\n await this.executeJobs()\n } else {\n await this.execute()\n }\n if (!this.isCancelRequestedOrNotRunning()) {\n await this.beforeSuccess()\n }\n }\n } finally {\n // runs only if not already run by cancel()\n await this.runBeforeEnd()\n this.context.tx = undefined\n }\n\n if (this.isCancelRequestedOrNotRunning()) {\n this.throwError(SystemErrorKeys.job.canceledOrErrorsFound)\n }\n }\n\n /**\n * Generates a plain JSON-serializable representation of the job, including its inner jobs.\n */\n toJSON(): JobSerialized<R> {\n const { errors, processed, result, status, total, type, userUuid, uuid } = this\n\n return {\n uuid,\n type,\n userUuid,\n surveyId: this.surveyId,\n innerJobs: this.innerJobs.map((job) => job.toJSON()),\n currentInnerJobIndex: this.currentInnerJobIndex,\n\n status,\n pending: this.isPending(),\n running: this.isRunning(),\n succeeded: this.isSucceeded(),\n canceled: this.isCanceled(),\n failed: this.isFailed(),\n ended: this.isEnded(),\n\n total,\n processed,\n progressPercent: this.calculateProgressPercent(),\n elapsedMillis: this.calculateElapsedMillis(),\n\n errors: this.isFailed() ? errors : undefined,\n result: this.isSucceeded() ? result : undefined,\n }\n }\n\n private async executeJobs(): Promise<void> {\n this.logDebug(`- ${this.total} inner jobs found`)\n\n for (let i = 0; i < this.innerJobs.length; i++) {\n // canceled between two inner jobs: the next ones must not be started\n if (this.cancelRequested) break\n\n this.logDebug(`- executing inner job ${i + 1}`)\n this.currentInnerJobIndex = i\n const currentInnerJob = this.innerJobs[i]\n if (currentInnerJob.context) {\n Object.assign(this.context, currentInnerJob.context)\n }\n currentInnerJob.context = this.context\n currentInnerJob.onEvent((event) => {\n // handle the inner job events in sequence; errors are logged (they would be unhandled rejections otherwise)\n this.innerJobEventsQueue = this.innerJobEventsQueue\n .then(() => this.onInnerJobEvent(event))\n .catch((error) => this.logError(error?.stack ?? error))\n })\n\n // The inner job shares this very context object, and its own start()/executeInTransaction()\n // clear `context.tx` when they terminate: without saving and restoring it here, the first\n // inner job would wipe the parent's transaction handle, leaving every following inner job\n // (and the parent's own beforeSuccess()/beforeEnd()) running outside of the transaction.\n // start() never throws (it swallows/records errors internally), so a plain restore is enough.\n const parentTx = this.context.tx\n await currentInnerJob.start(parentTx)\n this.context.tx = parentTx\n // the inner job events (e.g. canceled, failed) update the status of this job\n await this.innerJobEventsQueue\n\n if (this.cancelRequested || this.isCanceled() || currentInnerJob.isCanceled()) {\n break\n }\n if (currentInnerJob.isSucceeded()) {\n this.incrementProcessedItems()\n } else if (this.stopOnInnerJobFailure) {\n break\n }\n }\n if (\n !this.stopOnInnerJobFailure &&\n !this.cancelRequested &&\n this.innerJobs.some((innerJob) => innerJob.isFailed())\n ) {\n // set only after all inner jobs have run: setting it at the first failure would end this job (onEnd) too early\n await this.runBeforeEnd()\n await this.setStatus(JobStatus.failed)\n }\n\n this.logDebug(`- ${this.processed} inner jobs processed successfully`)\n }\n\n /**\n * Runs the job's own work when it has no inner jobs. Default implementation is a no-op, so\n * subclasses that only supply inner jobs (and never override this) don't need to. It also means\n * a subclass can safely call `super.execute()` defensively without knowing whether any ancestor\n * overrides it - if this were `abstract` instead, TypeScript would compile away any runtime\n * property for it entirely, so `super.execute()` would throw the moment no ancestor overrides it\n * (confirmed in production: a subclass whose immediate parent didn't override `execute()` crashed\n * with \"(intermediate value).execute is not a function\" the instant it called `super.execute()`).\n */\n protected async execute(): Promise<void> {\n // to be extended by subclasses\n }\n\n /**\n * Determines whether the job should actually run.\n * When it returns false, \"execute\" (or the inner jobs) will be skipped, but the job will still succeed.\n */\n protected shouldExecute(): Promise<boolean> {\n return Promise.resolve(true)\n }\n\n protected incrementProcessedItems(incrementBy = 1): void {\n this.processed += incrementBy\n }\n\n protected async setStatus(status: JobStatus): Promise<void> {\n this.logDebug(`set status: ${status}`)\n this.status = status\n\n const event: JobEvent = { type: JobEventType.statusChange, status, total: this.total, processed: this.processed }\n if (status === JobStatus.failed) {\n event.errors = this.errors\n }\n\n if (this.isEnded() && this.executingInTransaction) {\n // onEnd (and the event notifying the end) run when the transaction has ended\n this.deferredEndEvent = event\n return\n }\n await this.end(event)\n }\n\n private async end(event: JobEvent): Promise<void> {\n if (this.isEnded()) {\n this.logDebug('onEnd...')\n await this.onEnd()\n this.logDebug(`onEnd run. Job completed in ${this.elapsedTimePrettyFormat}`)\n }\n\n this.notifyEvent(event)\n }\n\n private async endDeferred(): Promise<void> {\n const event = this.deferredEndEvent\n if (!event) return\n this.deferredEndEvent = null\n await this.end(event)\n }\n\n /**\n * Inner job event handler.\n */\n protected async onInnerJobEvent(event: JobEvent): Promise<void> {\n const { status } = event\n if (status === JobStatus.canceled) {\n this.canceledByAdmin = this.getCurrentInnerJob()?.canceledByAdmin ?? false\n // beforeEnd must run before onEnd (it runs only once: it won't run again when the execution ends)\n await this.runBeforeEnd()\n return this.setStatus(status)\n }\n if (status === JobStatus.failed) {\n // report the errors of the failed inner job in the failed event (and in the errors) of this job\n const currentInnerJob = this.getCurrentInnerJob()\n if (currentInnerJob) {\n JobBase.mergeInnerJobErrors(this.errors, currentInnerJob)\n }\n // when not stopping on inner job failures, this job is set as failed after all the inner jobs have run\n if (this.stopOnInnerJobFailure) {\n await this.runBeforeEnd()\n await this.setStatus(status)\n }\n return\n }\n if (status === JobStatus.running) {\n this.notifyEvent({\n type: JobEventType.progress,\n status: this.status,\n total: this.total,\n processed: this.processed,\n })\n return\n }\n this.logDebug(`Unknown inner job status: ${status}`)\n }\n\n /**\n * Called when the job just has been started.\n */\n protected async onStart(): Promise<void> {\n this.startTime = new Date()\n await this.setStatus(JobStatus.running)\n }\n\n /**\n * Called before beforeEnd only if the status will change to 'success'.\n * Default implementation stores the value returned by generateResult() via setResult().\n * It runs INSIDE the current db transaction.\n */\n protected async beforeSuccess(): Promise<void> {\n this.setResult(await this.generateResult())\n }\n\n /**\n * Computes the value beforeSuccess() will store as this job's result.\n * Default implementation returns whatever is already in `this.result`\n * (e.g. accumulated via earlier setResult() calls during execute()).\n */\n protected generateResult(): Promise<R | undefined> {\n return Promise.resolve(this.result)\n }\n\n /**\n * Updates this job's result. When both the current and incoming values are plain objects,\n * they are merged (Object.assign); otherwise the incoming value replaces the current one.\n * This keeps it safe for subclasses whose result is a primitive (e.g. a plain number) as\n * well as ones that build an object result incrementally across multiple setResult() calls.\n */\n protected setResult(result: R | undefined): void {\n if (result && typeof result === 'object' && this.result && typeof this.result === 'object') {\n Object.assign(this.result, result)\n } else {\n this.result = result\n }\n }\n\n /**\n * Called before onEnd. Useful for flushing resources used by the job before it terminates completely.\n * It runs INSIDE the current db transaction.\n */\n protected beforeEnd(): Promise<void> {\n this.logDebug('Cleanup')\n return Promise.resolve()\n }\n\n /**\n * Called when the job status changes to success, failed or canceled\n * (it runs OUTSIDE of the current db transaction)\n */\n protected onEnd(): Promise<void> {\n this.endTime = new Date()\n this.cancelNotifyProgress()\n return Promise.resolve()\n }\n\n protected addError(error: any, errorKey?: string): void {\n const key = errorKey ?? String(this.processed + 1)\n this.errors[key] = error\n }\n\n protected throwError(errorKey: string, params?: Dictionary<any>): void {\n // SystemError: getErrorInfo will report the translatable key instead of a generic error\n throw new SystemError(errorKey, params)\n }\n\n protected getErrorInfo(error: any): { key: string; params: Record<string, any> } {\n if (error instanceof SystemError) {\n return { key: `appErrors:${error.key}`, params: error.params }\n }\n return { key: 'appErrors:generic', params: { text: error.toString() } }\n }\n\n protected abstract createLogger(): Logger\n\n protected logDebug(...msgs: any[]): void {\n this.logger.debug(...msgs)\n }\n\n protected logInfo(...msgs: any[]): void {\n this.logger.info(...msgs)\n }\n\n protected logWarn(...msgs: any[]): void {\n this.logger.warn(...msgs)\n }\n\n protected logError(...msgs: any[]): void {\n this.logger.error(...msgs)\n }\n\n private get elapsedTimePrettyFormat(): string {\n const elapsedTime = this.calculateElapsedMillis()\n const elapsedMins = Math.floor(elapsedTime / 1000 / 60)\n const elapsedSeconds = String(Math.floor(elapsedTime / 1000) % 60).padStart(2, '0')\n const elapsedMillis = String(elapsedTime % 1000).padStart(3, '0')\n return `${elapsedMins}:${elapsedSeconds}.${elapsedMillis}`\n }\n\n private calculatePartialProgress(): number {\n if (this.isSucceeded()) return 100\n if (this.total > 0) return Math.floor((100 * this.processed) / this.total)\n return 0\n }\n\n private calculateProgressPercent(): number {\n const partialProgress = this.calculatePartialProgress()\n const currentInnerJob = this.getCurrentInnerJob()\n if (\n !currentInnerJob ||\n this.currentInnerJobIndex < 0 ||\n partialProgress === 100 ||\n this.processed > this.currentInnerJobIndex\n ) {\n return partialProgress\n }\n return partialProgress + Math.floor(currentInnerJob.calculateProgressPercent() / this.total)\n }\n\n private calculateElapsedMillis(): number {\n if (!this.startTime) return 0\n return (this.endTime ?? new Date()).getTime() - this.startTime.getTime()\n }\n\n /**\n * Notifies a progress event to the registered listener, throttled so that fast-changing\n * progress does not flood consumers (e.g. web socket clients).\n */\n private notifyProgress(): void {\n // onEnd has already cleared the throttle: no progress events after the job has ended\n if (this.isEnded()) return\n\n const run = (): void => {\n this.progressThrottleTimeoutId = undefined\n this.progressThrottleLastRunTime = Date.now()\n this.notifyEvent({\n type: JobEventType.progress,\n status: this.status,\n total: this.total,\n processed: this.processed,\n })\n }\n\n if (this.progressThrottleTimeoutId) return\n\n const elapsed = Date.now() - this.progressThrottleLastRunTime\n if (!this.progressThrottleLastRunTime || elapsed >= PROGRESS_NOTIFICATION_THROTTLE_MILLIS) {\n run()\n } else {\n this.progressThrottleTimeoutId = setTimeout(run, PROGRESS_NOTIFICATION_THROTTLE_MILLIS - elapsed)\n }\n }\n\n /**\n * Cancels any pending throttled progress notification and resets the throttle state.\n */\n private cancelNotifyProgress(): void {\n if (this.progressThrottleTimeoutId) {\n clearTimeout(this.progressThrottleTimeoutId)\n this.progressThrottleTimeoutId = undefined\n }\n this.progressThrottleLastRunTime = 0\n }\n\n /**\n * Notifies the event to the registered listener.\n * Listener errors are only logged: they would otherwise fail the job or, when thrown inside the progress\n * throttle timeout, crash the process.\n */\n private notifyEvent(event: JobEvent): void {\n const listener = this.eventListener\n if (!listener) return\n try {\n Promise.resolve(listener(event)).catch((error) => this.logListenerError(error))\n } catch (error) {\n this.logListenerError(error)\n }\n }\n\n private logListenerError(error: any): void {\n this.logError(`error in job event listener: ${error?.stack ?? error}`)\n }\n}\n"]}
|
package/dist/job/JobBase.d.ts
CHANGED
|
@@ -65,6 +65,8 @@ export declare abstract class JobBase<C extends JobContext, R = undefined> imple
|
|
|
65
65
|
private cancelRequested;
|
|
66
66
|
private beforeEndCalled;
|
|
67
67
|
private innerJobEventsQueue;
|
|
68
|
+
private executingInTransaction;
|
|
69
|
+
private deferredEndEvent;
|
|
68
70
|
constructor(context: C, innerJobs?: JobBase<C, any>[]);
|
|
69
71
|
get processed(): number;
|
|
70
72
|
set processed(value: number);
|
|
@@ -117,6 +119,7 @@ export declare abstract class JobBase<C extends JobContext, R = undefined> imple
|
|
|
117
119
|
*/
|
|
118
120
|
private runBeforeEnd;
|
|
119
121
|
start(client?: JobTxClient | null): Promise<void>;
|
|
122
|
+
private executeInOwnTransaction;
|
|
120
123
|
private executeInTransaction;
|
|
121
124
|
/**
|
|
122
125
|
* Generates a plain JSON-serializable representation of the job, including its inner jobs.
|
|
@@ -140,6 +143,8 @@ export declare abstract class JobBase<C extends JobContext, R = undefined> imple
|
|
|
140
143
|
protected shouldExecute(): Promise<boolean>;
|
|
141
144
|
protected incrementProcessedItems(incrementBy?: number): void;
|
|
142
145
|
protected setStatus(status: JobStatus): Promise<void>;
|
|
146
|
+
private end;
|
|
147
|
+
private endDeferred;
|
|
143
148
|
/**
|
|
144
149
|
* Inner job event handler.
|
|
145
150
|
*/
|
package/dist/job/JobBase.js
CHANGED
|
@@ -53,6 +53,10 @@ class JobBase {
|
|
|
53
53
|
this.beforeEndCalled = false;
|
|
54
54
|
// inner job events are handled in sequence; executeJobs waits for them before going on
|
|
55
55
|
this.innerJobEventsQueue = Promise.resolve();
|
|
56
|
+
// true while the job is executing in its own transaction: onEnd must run out of it
|
|
57
|
+
this.executingInTransaction = false;
|
|
58
|
+
// status change event of a job ended while executing in its transaction (e.g. when an inner job fails)
|
|
59
|
+
this.deferredEndEvent = null;
|
|
56
60
|
this.context = Object.assign({}, context);
|
|
57
61
|
this.innerJobs = innerJobs;
|
|
58
62
|
this.uuid = utils_1.UUIDs.v4();
|
|
@@ -211,15 +215,7 @@ class JobBase {
|
|
|
211
215
|
return;
|
|
212
216
|
this.logDebug('start');
|
|
213
217
|
try {
|
|
214
|
-
|
|
215
|
-
yield client.tx((tx) => __awaiter(this, void 0, void 0, function* () {
|
|
216
|
-
this.context.tx = tx;
|
|
217
|
-
yield this.executeInTransaction();
|
|
218
|
-
}));
|
|
219
|
-
}
|
|
220
|
-
else {
|
|
221
|
-
yield this.executeInTransaction();
|
|
222
|
-
}
|
|
218
|
+
yield this.executeInOwnTransaction(client);
|
|
223
219
|
if (!this.isCancelRequestedOrNotRunning()) {
|
|
224
220
|
yield this.setStatus(status_1.JobStatus.succeeded);
|
|
225
221
|
}
|
|
@@ -238,6 +234,28 @@ class JobBase {
|
|
|
238
234
|
}
|
|
239
235
|
});
|
|
240
236
|
}
|
|
237
|
+
executeInOwnTransaction(client) {
|
|
238
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
239
|
+
this.executingInTransaction = true;
|
|
240
|
+
try {
|
|
241
|
+
if (client) {
|
|
242
|
+
yield client.tx((tx) => __awaiter(this, void 0, void 0, function* () {
|
|
243
|
+
this.context.tx = tx;
|
|
244
|
+
yield this.executeInTransaction();
|
|
245
|
+
}));
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
yield this.executeInTransaction();
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
finally {
|
|
252
|
+
this.executingInTransaction = false;
|
|
253
|
+
// the job ended while executing (e.g. an inner job failed, or it was canceled): onEnd runs now, out of the
|
|
254
|
+
// transaction (onEnd can use other connections, e.g. to delete data locked by the transaction)
|
|
255
|
+
yield this.endDeferred();
|
|
256
|
+
}
|
|
257
|
+
});
|
|
258
|
+
}
|
|
241
259
|
executeInTransaction() {
|
|
242
260
|
return __awaiter(this, void 0, void 0, function* () {
|
|
243
261
|
try {
|
|
@@ -377,6 +395,16 @@ class JobBase {
|
|
|
377
395
|
if (status === status_1.JobStatus.failed) {
|
|
378
396
|
event.errors = this.errors;
|
|
379
397
|
}
|
|
398
|
+
if (this.isEnded() && this.executingInTransaction) {
|
|
399
|
+
// onEnd (and the event notifying the end) run when the transaction has ended
|
|
400
|
+
this.deferredEndEvent = event;
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
yield this.end(event);
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
end(event) {
|
|
407
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
380
408
|
if (this.isEnded()) {
|
|
381
409
|
this.logDebug('onEnd...');
|
|
382
410
|
yield this.onEnd();
|
|
@@ -385,6 +413,15 @@ class JobBase {
|
|
|
385
413
|
this.notifyEvent(event);
|
|
386
414
|
});
|
|
387
415
|
}
|
|
416
|
+
endDeferred() {
|
|
417
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
418
|
+
const event = this.deferredEndEvent;
|
|
419
|
+
if (!event)
|
|
420
|
+
return;
|
|
421
|
+
this.deferredEndEvent = null;
|
|
422
|
+
yield this.end(event);
|
|
423
|
+
});
|
|
424
|
+
}
|
|
388
425
|
/**
|
|
389
426
|
* Inner job event handler.
|
|
390
427
|
*/
|
package/dist/job/JobBase.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,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BAAyB,CAAC,CAAA;YAC5C,IAAI,CAAC,yBAAyB,GAAG,SAAS,CAAA;QAC5C,CAAC;QACD,IAAI,CAAC,2BAA2B,GAAG,CAAC,CAAA;IACtC,CAAC;IAED;;;;OAIG;IACK,WAAW,CAAC,KAAe;QACjC,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAA;QACnC,IAAI,CAAC,QAAQ;YAAE,OAAM;QACrB,IAAI,CAAC;YACH,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAA;QACjF,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;QAC9B,CAAC;IACH,CAAC;IAEO,gBAAgB,CAAC,KAAU;;QACjC,IAAI,CAAC,QAAQ,CAAC,gCAAgC,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,KAAK,mCAAI,KAAK,EAAE,CAAC,CAAA;IACxE,CAAC;CACF;;AAhnBiB,mBAAW,GAAG;IAC5B,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;CACb,AAJ0B,CAI1B","sourcesContent":["import { User } from '../auth'\nimport { Dictionary } from '../common'\nimport { SystemError, SystemErrorKeys } from '../error'\nimport { Logger } from '../logger'\nimport { Survey } from '../survey'\nimport { UUIDs } from '../utils'\n\nimport { Job } from './job'\nimport { JobContext } from './jobContext'\nimport { JobEvent, JobEventType } from './jobEvent'\nimport { JobSerialized } from './jobSerialized'\nimport { JobStatus } from './status'\n\nexport interface JobConstructor {\n new <C extends JobContext, R>(context: C, innerJobs?: JobBase<any>[]): JobBase<C, R>\n readonly prototype: JobBase<any, any>\n}\n\n/**\n * Client able to open a transaction (e.g. a pg-promise database): the job runs inside `client.tx(callback)`.\n */\nexport interface JobTxClient {\n tx(callback: (tx: any) => Promise<void>): Promise<unknown>\n}\n\nconst PROGRESS_NOTIFICATION_THROTTLE_MILLIS = 1000\n\n/**\n * Asynchronous task handler.\n *\n * Status workflow:\n * - pending\n * - running\n * - (end)\n * -- succeeded\n * -- failed\n * -- canceled\n *\n * Methods that can be overwritten by subclasses:\n * - shouldExecute (in tx)\n * - onStart (in tx)\n * - execute (in tx)\n * - beforeSuccess (in tx)\n * - generateResult (in tx)\n * - beforeEnd (in tx)\n * - onEnd (out of tx)\n * - getErrorInfo\n */\nexport abstract class JobBase<C extends JobContext, R = undefined> implements Job<R> {\n static readonly keysContext = {\n surveyId: 'surveyId',\n survey: 'survey',\n user: 'user',\n }\n\n readonly uuid: string\n readonly type: string\n status: JobStatus = JobStatus.pending\n startTime?: Date\n endTime?: Date\n total: number\n result: R | undefined = undefined\n errors: Record<string, any> = {}\n canceledByAdmin?: boolean\n\n protected logger: Logger\n protected context: C\n protected innerJobs: JobBase<C, any>[]\n protected currentInnerJobIndex = -1\n\n private _processed = 0\n private eventListener: ((event: JobEvent) => unknown) | undefined = undefined\n private progressThrottleTimeoutId: ReturnType<typeof setTimeout> | undefined = undefined\n private progressThrottleLastRunTime = 0\n private _stopOnInnerJobFailure = true\n // set as soon as cancel() is called (the canceled status is set only after beforeEnd has run)\n private cancelRequested = false\n private beforeEndCalled = false\n // inner job events are handled in sequence; executeJobs waits for them before going on\n private innerJobEventsQueue: Promise<void> = Promise.resolve()\n\n public constructor(context: C, innerJobs: JobBase<C, any>[] = []) {\n this.context = { ...context }\n this.innerJobs = innerJobs\n\n this.uuid = UUIDs.v4()\n this.type = this.context.type ?? this.constructor.name\n this.total = innerJobs.length > 0 ? innerJobs.length : 1\n\n // Created last, so that createLogger() overrides can safely build the logger name out of\n // the job identity (e.g. arena's `Job <ClassName> (<uuid>)` log correlation id).\n this.logger = this.createLogger()\n }\n\n get processed(): number {\n return this._processed\n }\n\n set processed(value: number) {\n this._processed = value\n this.notifyProgress()\n }\n\n get stopOnInnerJobFailure(): boolean {\n return this._stopOnInnerJobFailure\n }\n\n set stopOnInnerJobFailure(value: boolean) {\n this._stopOnInnerJobFailure = value\n }\n\n protected get surveyId(): number | null {\n return this.context.surveyId ?? null\n }\n\n protected get user(): User {\n return this.context.user\n }\n\n protected get userUuid(): string | undefined {\n return this.user?.uuid\n }\n\n protected get contextSurvey(): Survey | undefined {\n return this.context.survey\n }\n\n /**\n * Returns the value of the specified context prop, or the default value (null if not specified) when not set.\n */\n protected getContextProp<K extends keyof C & string>(prop: K, defaultValue?: C[K] | null): C[K] | null\n /** Context prop not declared in the context type: the value is typed by the caller. */\n protected getContextProp<T = any, P extends string = string>(\n prop: P extends keyof C ? never : P,\n defaultValue?: T | null\n ): T | null\n protected getContextProp(prop: string, defaultValue: unknown = null): unknown {\n const value = (this.context as Record<string, unknown>)[prop]\n return value ?? defaultValue\n }\n\n protected setContext(context: Partial<C>): void {\n Object.assign(this.context, context)\n }\n\n protected deleteContextProps(...propNames: string[]): void {\n propNames.forEach((propName) => {\n delete (this.context as any)[propName]\n })\n }\n\n /**\n * Registers the listener notified of job events (status changes and progress).\n * Only one listener can be registered at a time; registering a new one replaces the previous one.\n * Errors thrown (or rejections returned) by the listener are logged and do not affect the job execution.\n */\n onEvent(listener: (event: JobEvent) => unknown): this {\n this.eventListener = listener\n return this\n }\n\n isPending(): boolean {\n return this.status === JobStatus.pending\n }\n\n isRunning(): boolean {\n return this.status === JobStatus.running\n }\n\n isSucceeded(): boolean {\n return this.status === JobStatus.succeeded\n }\n\n isFailed(): boolean {\n return this.status === JobStatus.failed\n }\n\n isCanceled(): boolean {\n return this.status === JobStatus.canceled\n }\n\n isEnded(): boolean {\n return [JobStatus.succeeded, JobStatus.failed, JobStatus.canceled].includes(this.status)\n }\n\n hasErrors(): boolean {\n return Object.keys(this.errors).length > 0\n }\n\n protected getCurrentInnerJob(): JobBase<C, any> | undefined {\n return this.innerJobs[this.currentInnerJobIndex]\n }\n\n protected combineInnerJobsResults(): Record<string, any> {\n const results: Record<string, any> = {}\n this.innerJobs.forEach((innerJob) => Object.assign(results, innerJob.result ?? {}))\n return results\n }\n\n protected combineInnerJobsErrors(): Record<string, any> {\n const errors: Record<string, any> = {}\n this.innerJobs.forEach((innerJob) => JobBase.mergeInnerJobErrors(errors, innerJob))\n return errors\n }\n\n /**\n * Adds the errors of the inner job to the specified errors.\n * Error keys already in use (e.g. the same item number used by two inner jobs) are prefixed with the inner job type\n * (and suffixed with a counter if still in use), so that no error gets overwritten.\n */\n private static mergeInnerJobErrors(errors: Record<string, any>, innerJob: JobBase<any, any>): void {\n Object.entries(innerJob.errors ?? {}).forEach(([key, error]) => {\n let uniqueKey = key\n if (uniqueKey in errors) {\n const prefixedKey = `${innerJob.type} ${key}`\n uniqueKey = prefixedKey\n let count = 2\n while (uniqueKey in errors) {\n uniqueKey = `${prefixedKey} (${count})`\n count += 1\n }\n }\n errors[uniqueKey] = error\n })\n }\n\n async cancel(options: { canceledByAdmin?: boolean } = {}): Promise<void> {\n const { canceledByAdmin = false } = options\n if (this.isEnded()) return\n\n this.cancelRequested = true\n\n const currentInnerJob = this.getCurrentInnerJob()\n if (currentInnerJob && !currentInnerJob.isEnded()) {\n // inner job running or about to start (pending):\n // the parent job status will be set to canceled when handling the canceled event of the inner job\n await currentInnerJob.cancel({ canceledByAdmin })\n return\n }\n // no inner job running (job without inner jobs, or between two inner jobs)\n this.canceledByAdmin = canceledByAdmin\n // release the resources (temp files/directories, streams) allocated before the cancellation\n await this.runBeforeEnd()\n await this.setStatus(JobStatus.canceled)\n }\n\n /**\n * Returns true when the job has been canceled (or is no longer running).\n * Long-running subclasses can check it to stop processing items (e.g. in a loop) as soon as the job is canceled.\n */\n protected isCancelRequestedOrNotRunning(): boolean {\n return this.cancelRequested || !this.isRunning()\n }\n\n /**\n * Runs beforeEnd only once (it can be called by cancel() and when the job execution ends).\n */\n private async runBeforeEnd(): Promise<void> {\n if (this.beforeEndCalled) return\n this.beforeEndCalled = true\n await this.beforeEnd()\n }\n\n async start(client: JobTxClient | null = null): Promise<void> {\n // canceled before being started: nothing to execute\n if (this.cancelRequested || this.isEnded()) return\n\n this.logDebug('start')\n\n try {\n if (client) {\n await client.tx(async (tx: any) => {\n this.context.tx = tx\n await this.executeInTransaction()\n })\n } else {\n await this.executeInTransaction()\n }\n if (!this.isCancelRequestedOrNotRunning()) {\n await this.setStatus(JobStatus.succeeded)\n }\n } catch (error: any) {\n // a job can fail while still pending (e.g. the transaction cannot be opened, or onStart fails)\n if (!this.cancelRequested && !this.isFailed() && !this.isCanceled()) {\n this.logError(error.stack ?? error)\n const { key, params } = this.getErrorInfo(error)\n this.addError({ error: { valid: false, errors: [{ key, params }] } })\n await this.setStatus(JobStatus.failed)\n }\n } finally {\n this.context.tx = undefined\n }\n }\n\n private async executeInTransaction(): Promise<void> {\n try {\n // canceled while the transaction was being opened: it must not be set as running again\n if (this.cancelRequested) return\n\n await this.onStart()\n\n const shouldExecute = await this.shouldExecute()\n if (shouldExecute) {\n if (this.innerJobs.length > 0) {\n await this.executeJobs()\n } else {\n await this.execute()\n }\n if (!this.isCancelRequestedOrNotRunning()) {\n await this.beforeSuccess()\n }\n }\n } finally {\n // runs only if not already run by cancel()\n await this.runBeforeEnd()\n this.context.tx = undefined\n }\n\n if (this.isCancelRequestedOrNotRunning()) {\n this.throwError(SystemErrorKeys.job.canceledOrErrorsFound)\n }\n }\n\n /**\n * Generates a plain JSON-serializable representation of the job, including its inner jobs.\n */\n toJSON(): JobSerialized<R> {\n const { errors, processed, result, status, total, type, userUuid, uuid } = this\n\n return {\n uuid,\n type,\n userUuid,\n surveyId: this.surveyId,\n innerJobs: this.innerJobs.map((job) => job.toJSON()),\n currentInnerJobIndex: this.currentInnerJobIndex,\n\n status,\n pending: this.isPending(),\n running: this.isRunning(),\n succeeded: this.isSucceeded(),\n canceled: this.isCanceled(),\n failed: this.isFailed(),\n ended: this.isEnded(),\n\n total,\n processed,\n progressPercent: this.calculateProgressPercent(),\n elapsedMillis: this.calculateElapsedMillis(),\n\n errors: this.isFailed() ? errors : undefined,\n result: this.isSucceeded() ? result : undefined,\n }\n }\n\n private async executeJobs(): Promise<void> {\n this.logDebug(`- ${this.total} inner jobs found`)\n\n for (let i = 0; i < this.innerJobs.length; i++) {\n // canceled between two inner jobs: the next ones must not be started\n if (this.cancelRequested) break\n\n this.logDebug(`- executing inner job ${i + 1}`)\n this.currentInnerJobIndex = i\n const currentInnerJob = this.innerJobs[i]\n if (currentInnerJob.context) {\n Object.assign(this.context, currentInnerJob.context)\n }\n currentInnerJob.context = this.context\n currentInnerJob.onEvent((event) => {\n // handle the inner job events in sequence; errors are logged (they would be unhandled rejections otherwise)\n this.innerJobEventsQueue = this.innerJobEventsQueue\n .then(() => this.onInnerJobEvent(event))\n .catch((error) => this.logError(error?.stack ?? error))\n })\n\n // The inner job shares this very context object, and its own start()/executeInTransaction()\n // clear `context.tx` when they terminate: without saving and restoring it here, the first\n // inner job would wipe the parent's transaction handle, leaving every following inner job\n // (and the parent's own beforeSuccess()/beforeEnd()) running outside of the transaction.\n // start() never throws (it swallows/records errors internally), so a plain restore is enough.\n const parentTx = this.context.tx\n await currentInnerJob.start(parentTx)\n this.context.tx = parentTx\n // the inner job events (e.g. canceled, failed) update the status of this job\n await this.innerJobEventsQueue\n\n if (this.cancelRequested || this.isCanceled() || currentInnerJob.isCanceled()) {\n break\n }\n if (currentInnerJob.isSucceeded()) {\n this.incrementProcessedItems()\n } else if (this.stopOnInnerJobFailure) {\n break\n }\n }\n if (\n !this.stopOnInnerJobFailure &&\n !this.cancelRequested &&\n this.innerJobs.some((innerJob) => innerJob.isFailed())\n ) {\n // set only after all inner jobs have run: setting it at the first failure would end this job (onEnd) too early\n await this.runBeforeEnd()\n await this.setStatus(JobStatus.failed)\n }\n\n this.logDebug(`- ${this.processed} inner jobs processed successfully`)\n }\n\n /**\n * Runs the job's own work when it has no inner jobs. Default implementation is a no-op, so\n * subclasses that only supply inner jobs (and never override this) don't need to. It also means\n * a subclass can safely call `super.execute()` defensively without knowing whether any ancestor\n * overrides it - if this were `abstract` instead, TypeScript would compile away any runtime\n * property for it entirely, so `super.execute()` would throw the moment no ancestor overrides it\n * (confirmed in production: a subclass whose immediate parent didn't override `execute()` crashed\n * with \"(intermediate value).execute is not a function\" the instant it called `super.execute()`).\n */\n protected async execute(): Promise<void> {\n // to be extended by subclasses\n }\n\n /**\n * Determines whether the job should actually run.\n * When it returns false, \"execute\" (or the inner jobs) will be skipped, but the job will still succeed.\n */\n protected shouldExecute(): Promise<boolean> {\n return Promise.resolve(true)\n }\n\n protected incrementProcessedItems(incrementBy = 1): void {\n this.processed += incrementBy\n }\n\n protected async setStatus(status: JobStatus): Promise<void> {\n this.logDebug(`set status: ${status}`)\n this.status = status\n\n const event: JobEvent = { type: JobEventType.statusChange, status, total: this.total, processed: this.processed }\n if (status === JobStatus.failed) {\n event.errors = this.errors\n }\n\n if (this.isEnded()) {\n this.logDebug('onEnd...')\n await this.onEnd()\n this.logDebug(`onEnd run. Job completed in ${this.elapsedTimePrettyFormat}`)\n }\n\n this.notifyEvent(event)\n }\n\n /**\n * Inner job event handler.\n */\n protected async onInnerJobEvent(event: JobEvent): Promise<void> {\n const { status } = event\n if (status === JobStatus.canceled) {\n this.canceledByAdmin = this.getCurrentInnerJob()?.canceledByAdmin ?? false\n // beforeEnd must run before onEnd (it runs only once: it won't run again when the execution ends)\n await this.runBeforeEnd()\n return this.setStatus(status)\n }\n if (status === JobStatus.failed) {\n // report the errors of the failed inner job in the failed event (and in the errors) of this job\n const currentInnerJob = this.getCurrentInnerJob()\n if (currentInnerJob) {\n JobBase.mergeInnerJobErrors(this.errors, currentInnerJob)\n }\n // when not stopping on inner job failures, this job is set as failed after all the inner jobs have run\n if (this.stopOnInnerJobFailure) {\n await this.runBeforeEnd()\n await this.setStatus(status)\n }\n return\n }\n if (status === JobStatus.running) {\n this.notifyEvent({\n type: JobEventType.progress,\n status: this.status,\n total: this.total,\n processed: this.processed,\n })\n return\n }\n this.logDebug(`Unknown inner job status: ${status}`)\n }\n\n /**\n * Called when the job just has been started.\n */\n protected async onStart(): Promise<void> {\n this.startTime = new Date()\n await this.setStatus(JobStatus.running)\n }\n\n /**\n * Called before beforeEnd only if the status will change to 'success'.\n * Default implementation stores the value returned by generateResult() via setResult().\n * It runs INSIDE the current db transaction.\n */\n protected async beforeSuccess(): Promise<void> {\n this.setResult(await this.generateResult())\n }\n\n /**\n * Computes the value beforeSuccess() will store as this job's result.\n * Default implementation returns whatever is already in `this.result`\n * (e.g. accumulated via earlier setResult() calls during execute()).\n */\n protected generateResult(): Promise<R | undefined> {\n return Promise.resolve(this.result)\n }\n\n /**\n * Updates this job's result. When both the current and incoming values are plain objects,\n * they are merged (Object.assign); otherwise the incoming value replaces the current one.\n * This keeps it safe for subclasses whose result is a primitive (e.g. a plain number) as\n * well as ones that build an object result incrementally across multiple setResult() calls.\n */\n protected setResult(result: R | undefined): void {\n if (result && typeof result === 'object' && this.result && typeof this.result === 'object') {\n Object.assign(this.result, result)\n } else {\n this.result = result\n }\n }\n\n /**\n * Called before onEnd. Useful for flushing resources used by the job before it terminates completely.\n * It runs INSIDE the current db transaction.\n */\n protected beforeEnd(): Promise<void> {\n this.logDebug('Cleanup')\n return Promise.resolve()\n }\n\n /**\n * Called when the job status changes to success, failed or canceled\n * (it runs OUTSIDE of the current db transaction)\n */\n protected onEnd(): Promise<void> {\n this.endTime = new Date()\n this.cancelNotifyProgress()\n return Promise.resolve()\n }\n\n protected addError(error: any, errorKey?: string): void {\n const key = errorKey ?? String(this.processed + 1)\n this.errors[key] = error\n }\n\n protected throwError(errorKey: string, params?: Dictionary<any>): void {\n // SystemError: getErrorInfo will report the translatable key instead of a generic error\n throw new SystemError(errorKey, params)\n }\n\n protected getErrorInfo(error: any): { key: string; params: Record<string, any> } {\n if (error instanceof SystemError) {\n return { key: `appErrors:${error.key}`, params: error.params }\n }\n return { key: 'appErrors:generic', params: { text: error.toString() } }\n }\n\n protected abstract createLogger(): Logger\n\n protected logDebug(...msgs: any[]): void {\n this.logger.debug(...msgs)\n }\n\n protected logInfo(...msgs: any[]): void {\n this.logger.info(...msgs)\n }\n\n protected logWarn(...msgs: any[]): void {\n this.logger.warn(...msgs)\n }\n\n protected logError(...msgs: any[]): void {\n this.logger.error(...msgs)\n }\n\n private get elapsedTimePrettyFormat(): string {\n const elapsedTime = this.calculateElapsedMillis()\n const elapsedMins = Math.floor(elapsedTime / 1000 / 60)\n const elapsedSeconds = String(Math.floor(elapsedTime / 1000) % 60).padStart(2, '0')\n const elapsedMillis = String(elapsedTime % 1000).padStart(3, '0')\n return `${elapsedMins}:${elapsedSeconds}.${elapsedMillis}`\n }\n\n private calculatePartialProgress(): number {\n if (this.isSucceeded()) return 100\n if (this.total > 0) return Math.floor((100 * this.processed) / this.total)\n return 0\n }\n\n private calculateProgressPercent(): number {\n const partialProgress = this.calculatePartialProgress()\n const currentInnerJob = this.getCurrentInnerJob()\n if (\n !currentInnerJob ||\n this.currentInnerJobIndex < 0 ||\n partialProgress === 100 ||\n this.processed > this.currentInnerJobIndex\n ) {\n return partialProgress\n }\n return partialProgress + Math.floor(currentInnerJob.calculateProgressPercent() / this.total)\n }\n\n private calculateElapsedMillis(): number {\n if (!this.startTime) return 0\n return (this.endTime ?? new Date()).getTime() - this.startTime.getTime()\n }\n\n /**\n * Notifies a progress event to the registered listener, throttled so that fast-changing\n * progress does not flood consumers (e.g. web socket clients).\n */\n private notifyProgress(): void {\n // onEnd has already cleared the throttle: no progress events after the job has ended\n if (this.isEnded()) return\n\n const run = (): void => {\n this.progressThrottleTimeoutId = undefined\n this.progressThrottleLastRunTime = Date.now()\n this.notifyEvent({\n type: JobEventType.progress,\n status: this.status,\n total: this.total,\n processed: this.processed,\n })\n }\n\n if (this.progressThrottleTimeoutId) return\n\n const elapsed = Date.now() - this.progressThrottleLastRunTime\n if (!this.progressThrottleLastRunTime || elapsed >= PROGRESS_NOTIFICATION_THROTTLE_MILLIS) {\n run()\n } else {\n this.progressThrottleTimeoutId = setTimeout(run, PROGRESS_NOTIFICATION_THROTTLE_MILLIS - elapsed)\n }\n }\n\n /**\n * Cancels any pending throttled progress notification and resets the throttle state.\n */\n private cancelNotifyProgress(): void {\n if (this.progressThrottleTimeoutId) {\n clearTimeout(this.progressThrottleTimeoutId)\n this.progressThrottleTimeoutId = undefined\n }\n this.progressThrottleLastRunTime = 0\n }\n\n /**\n * Notifies the event to the registered listener.\n * Listener errors are only logged: they would otherwise fail the job or, when thrown inside the progress\n * throttle timeout, crash the process.\n */\n private notifyEvent(event: JobEvent): void {\n const listener = this.eventListener\n if (!listener) return\n try {\n Promise.resolve(listener(event)).catch((error) => this.logListenerError(error))\n } catch (error) {\n this.logListenerError(error)\n }\n }\n\n private logListenerError(error: any): void {\n this.logError(`error in job event listener: ${error?.stack ?? error}`)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"JobBase.js","sourceRoot":"","sources":["../../src/job/JobBase.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,oCAAuD;AAGvD,oCAAgC;AAIhC,yCAAmD;AAEnD,qCAAoC;AAcpC,MAAM,qCAAqC,GAAG,IAAI,CAAA;AAElD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH;IAqCE,YAAmB,OAAU,EAAE,SAAS,GAAsB,EAAE;;QA5BhE,WAAM,GAAc,kBAAS,CAAC,OAAO,CAAA;QAIrC,WAAM,GAAkB,SAAS,CAAA;QACjC,WAAM,GAAwB,EAAE,CAAA;QAMtB,yBAAoB,GAAG,CAAC,CAAC,CAAA;QAE3B,eAAU,GAAG,CAAC,CAAA;QACd,kBAAa,GAA+C,SAAS,CAAA;QACrE,8BAAyB,GAA8C,SAAS,CAAA;QAChF,gCAA2B,GAAG,CAAC,CAAA;QAC/B,2BAAsB,GAAG,IAAI,CAAA;QACrC,8FAA8F;QACtF,oBAAe,GAAG,KAAK,CAAA;QACvB,oBAAe,GAAG,KAAK,CAAA;QAC/B,uFAAuF;QAC/E,wBAA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;aAAM,CAAC;YACN,IAAI,CAAC,yBAAyB,GAAG,UAAU,CAAC,GAAG,EAAE,qCAAqC,GAAG,OAAO,CAAC,CAAA;QACnG,CAAC;IACH,CAAC;IAED;;OAEG;IACK,oBAAoB;QAC1B,IAAI,IAAI,CAAC,yBAAyB,EAAE,CAAC;YACnC,YAAY,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAA;YAC5C,IAAI,CAAC,yBAAyB,GAAG,SAAS,CAAA;QAC5C,CAAC;QACD,IAAI,CAAC,2BAA2B,GAAG,CAAC,CAAA;IACtC,CAAC;IAED;;;;OAIG;IACK,WAAW,CAAC,KAAe;QACjC,MAAM,QAAQ,GAAG,IAAI,CAAC,aAAa,CAAA;QACnC,IAAI,CAAC,QAAQ;YAAE,OAAM;QACrB,IAAI,CAAC;YACH,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAC,CAAA;QACjF,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,CAAA;QAC9B,CAAC;IACH,CAAC;IAEO,gBAAgB,CAAC,KAAU;;QACjC,IAAI,CAAC,QAAQ,CAAC,gCAAgC,MAAA,KAAK,aAAL,KAAK,uBAAL,KAAK,CAAE,KAAK,mCAAI,KAAK,EAAE,CAAC,CAAA;IACxE,CAAC;CACF;;AAhpBiB,mBAAW,GAAG;IAC5B,QAAQ,EAAE,UAAU;IACpB,MAAM,EAAE,QAAQ;IAChB,IAAI,EAAE,MAAM;CACb,AAJ0B,CAI1B","sourcesContent":["import { User } from '../auth'\nimport { Dictionary } from '../common'\nimport { SystemError, SystemErrorKeys } from '../error'\nimport { Logger } from '../logger'\nimport { Survey } from '../survey'\nimport { UUIDs } from '../utils'\n\nimport { Job } from './job'\nimport { JobContext } from './jobContext'\nimport { JobEvent, JobEventType } from './jobEvent'\nimport { JobSerialized } from './jobSerialized'\nimport { JobStatus } from './status'\n\nexport interface JobConstructor {\n new <C extends JobContext, R>(context: C, innerJobs?: JobBase<any>[]): JobBase<C, R>\n readonly prototype: JobBase<any, any>\n}\n\n/**\n * Client able to open a transaction (e.g. a pg-promise database): the job runs inside `client.tx(callback)`.\n */\nexport interface JobTxClient {\n tx(callback: (tx: any) => Promise<void>): Promise<unknown>\n}\n\nconst PROGRESS_NOTIFICATION_THROTTLE_MILLIS = 1000\n\n/**\n * Asynchronous task handler.\n *\n * Status workflow:\n * - pending\n * - running\n * - (end)\n * -- succeeded\n * -- failed\n * -- canceled\n *\n * Methods that can be overwritten by subclasses:\n * - shouldExecute (in tx)\n * - onStart (in tx)\n * - execute (in tx)\n * - beforeSuccess (in tx)\n * - generateResult (in tx)\n * - beforeEnd (in tx)\n * - onEnd (out of tx)\n * - getErrorInfo\n */\nexport abstract class JobBase<C extends JobContext, R = undefined> implements Job<R> {\n static readonly keysContext = {\n surveyId: 'surveyId',\n survey: 'survey',\n user: 'user',\n }\n\n readonly uuid: string\n readonly type: string\n status: JobStatus = JobStatus.pending\n startTime?: Date\n endTime?: Date\n total: number\n result: R | undefined = undefined\n errors: Record<string, any> = {}\n canceledByAdmin?: boolean\n\n protected logger: Logger\n protected context: C\n protected innerJobs: JobBase<C, any>[]\n protected currentInnerJobIndex = -1\n\n private _processed = 0\n private eventListener: ((event: JobEvent) => unknown) | undefined = undefined\n private progressThrottleTimeoutId: ReturnType<typeof setTimeout> | undefined = undefined\n private progressThrottleLastRunTime = 0\n private _stopOnInnerJobFailure = true\n // set as soon as cancel() is called (the canceled status is set only after beforeEnd has run)\n private cancelRequested = false\n private beforeEndCalled = false\n // inner job events are handled in sequence; executeJobs waits for them before going on\n private innerJobEventsQueue: Promise<void> = Promise.resolve()\n // true while the job is executing in its own transaction: onEnd must run out of it\n private executingInTransaction = false\n // status change event of a job ended while executing in its transaction (e.g. when an inner job fails)\n private deferredEndEvent: JobEvent | null = null\n\n public constructor(context: C, innerJobs: JobBase<C, any>[] = []) {\n this.context = { ...context }\n this.innerJobs = innerJobs\n\n this.uuid = UUIDs.v4()\n this.type = this.context.type ?? this.constructor.name\n this.total = innerJobs.length > 0 ? innerJobs.length : 1\n\n // Created last, so that createLogger() overrides can safely build the logger name out of\n // the job identity (e.g. arena's `Job <ClassName> (<uuid>)` log correlation id).\n this.logger = this.createLogger()\n }\n\n get processed(): number {\n return this._processed\n }\n\n set processed(value: number) {\n this._processed = value\n this.notifyProgress()\n }\n\n get stopOnInnerJobFailure(): boolean {\n return this._stopOnInnerJobFailure\n }\n\n set stopOnInnerJobFailure(value: boolean) {\n this._stopOnInnerJobFailure = value\n }\n\n protected get surveyId(): number | null {\n return this.context.surveyId ?? null\n }\n\n protected get user(): User {\n return this.context.user\n }\n\n protected get userUuid(): string | undefined {\n return this.user?.uuid\n }\n\n protected get contextSurvey(): Survey | undefined {\n return this.context.survey\n }\n\n /**\n * Returns the value of the specified context prop, or the default value (null if not specified) when not set.\n */\n protected getContextProp<K extends keyof C & string>(prop: K, defaultValue?: C[K] | null): C[K] | null\n /** Context prop not declared in the context type: the value is typed by the caller. */\n protected getContextProp<T = any, P extends string = string>(\n prop: P extends keyof C ? never : P,\n defaultValue?: T | null\n ): T | null\n protected getContextProp(prop: string, defaultValue: unknown = null): unknown {\n const value = (this.context as Record<string, unknown>)[prop]\n return value ?? defaultValue\n }\n\n protected setContext(context: Partial<C>): void {\n Object.assign(this.context, context)\n }\n\n protected deleteContextProps(...propNames: string[]): void {\n propNames.forEach((propName) => {\n delete (this.context as any)[propName]\n })\n }\n\n /**\n * Registers the listener notified of job events (status changes and progress).\n * Only one listener can be registered at a time; registering a new one replaces the previous one.\n * Errors thrown (or rejections returned) by the listener are logged and do not affect the job execution.\n */\n onEvent(listener: (event: JobEvent) => unknown): this {\n this.eventListener = listener\n return this\n }\n\n isPending(): boolean {\n return this.status === JobStatus.pending\n }\n\n isRunning(): boolean {\n return this.status === JobStatus.running\n }\n\n isSucceeded(): boolean {\n return this.status === JobStatus.succeeded\n }\n\n isFailed(): boolean {\n return this.status === JobStatus.failed\n }\n\n isCanceled(): boolean {\n return this.status === JobStatus.canceled\n }\n\n isEnded(): boolean {\n return [JobStatus.succeeded, JobStatus.failed, JobStatus.canceled].includes(this.status)\n }\n\n hasErrors(): boolean {\n return Object.keys(this.errors).length > 0\n }\n\n protected getCurrentInnerJob(): JobBase<C, any> | undefined {\n return this.innerJobs[this.currentInnerJobIndex]\n }\n\n protected combineInnerJobsResults(): Record<string, any> {\n const results: Record<string, any> = {}\n this.innerJobs.forEach((innerJob) => Object.assign(results, innerJob.result ?? {}))\n return results\n }\n\n protected combineInnerJobsErrors(): Record<string, any> {\n const errors: Record<string, any> = {}\n this.innerJobs.forEach((innerJob) => JobBase.mergeInnerJobErrors(errors, innerJob))\n return errors\n }\n\n /**\n * Adds the errors of the inner job to the specified errors.\n * Error keys already in use (e.g. the same item number used by two inner jobs) are prefixed with the inner job type\n * (and suffixed with a counter if still in use), so that no error gets overwritten.\n */\n private static mergeInnerJobErrors(errors: Record<string, any>, innerJob: JobBase<any, any>): void {\n Object.entries(innerJob.errors ?? {}).forEach(([key, error]) => {\n let uniqueKey = key\n if (uniqueKey in errors) {\n const prefixedKey = `${innerJob.type} ${key}`\n uniqueKey = prefixedKey\n let count = 2\n while (uniqueKey in errors) {\n uniqueKey = `${prefixedKey} (${count})`\n count += 1\n }\n }\n errors[uniqueKey] = error\n })\n }\n\n async cancel(options: { canceledByAdmin?: boolean } = {}): Promise<void> {\n const { canceledByAdmin = false } = options\n if (this.isEnded()) return\n\n this.cancelRequested = true\n\n const currentInnerJob = this.getCurrentInnerJob()\n if (currentInnerJob && !currentInnerJob.isEnded()) {\n // inner job running or about to start (pending):\n // the parent job status will be set to canceled when handling the canceled event of the inner job\n await currentInnerJob.cancel({ canceledByAdmin })\n return\n }\n // no inner job running (job without inner jobs, or between two inner jobs)\n this.canceledByAdmin = canceledByAdmin\n // release the resources (temp files/directories, streams) allocated before the cancellation\n await this.runBeforeEnd()\n await this.setStatus(JobStatus.canceled)\n }\n\n /**\n * Returns true when the job has been canceled (or is no longer running).\n * Long-running subclasses can check it to stop processing items (e.g. in a loop) as soon as the job is canceled.\n */\n protected isCancelRequestedOrNotRunning(): boolean {\n return this.cancelRequested || !this.isRunning()\n }\n\n /**\n * Runs beforeEnd only once (it can be called by cancel() and when the job execution ends).\n */\n private async runBeforeEnd(): Promise<void> {\n if (this.beforeEndCalled) return\n this.beforeEndCalled = true\n await this.beforeEnd()\n }\n\n async start(client: JobTxClient | null = null): Promise<void> {\n // canceled before being started: nothing to execute\n if (this.cancelRequested || this.isEnded()) return\n\n this.logDebug('start')\n\n try {\n await this.executeInOwnTransaction(client)\n if (!this.isCancelRequestedOrNotRunning()) {\n await this.setStatus(JobStatus.succeeded)\n }\n } catch (error: any) {\n // a job can fail while still pending (e.g. the transaction cannot be opened, or onStart fails)\n if (!this.cancelRequested && !this.isFailed() && !this.isCanceled()) {\n this.logError(error.stack ?? error)\n const { key, params } = this.getErrorInfo(error)\n this.addError({ error: { valid: false, errors: [{ key, params }] } })\n await this.setStatus(JobStatus.failed)\n }\n } finally {\n this.context.tx = undefined\n }\n }\n\n private async executeInOwnTransaction(client: JobTxClient | null): Promise<void> {\n this.executingInTransaction = true\n try {\n if (client) {\n await client.tx(async (tx: any) => {\n this.context.tx = tx\n await this.executeInTransaction()\n })\n } else {\n await this.executeInTransaction()\n }\n } finally {\n this.executingInTransaction = false\n // the job ended while executing (e.g. an inner job failed, or it was canceled): onEnd runs now, out of the\n // transaction (onEnd can use other connections, e.g. to delete data locked by the transaction)\n await this.endDeferred()\n }\n }\n\n private async executeInTransaction(): Promise<void> {\n try {\n // canceled while the transaction was being opened: it must not be set as running again\n if (this.cancelRequested) return\n\n await this.onStart()\n\n const shouldExecute = await this.shouldExecute()\n if (shouldExecute) {\n if (this.innerJobs.length > 0) {\n await this.executeJobs()\n } else {\n await this.execute()\n }\n if (!this.isCancelRequestedOrNotRunning()) {\n await this.beforeSuccess()\n }\n }\n } finally {\n // runs only if not already run by cancel()\n await this.runBeforeEnd()\n this.context.tx = undefined\n }\n\n if (this.isCancelRequestedOrNotRunning()) {\n this.throwError(SystemErrorKeys.job.canceledOrErrorsFound)\n }\n }\n\n /**\n * Generates a plain JSON-serializable representation of the job, including its inner jobs.\n */\n toJSON(): JobSerialized<R> {\n const { errors, processed, result, status, total, type, userUuid, uuid } = this\n\n return {\n uuid,\n type,\n userUuid,\n surveyId: this.surveyId,\n innerJobs: this.innerJobs.map((job) => job.toJSON()),\n currentInnerJobIndex: this.currentInnerJobIndex,\n\n status,\n pending: this.isPending(),\n running: this.isRunning(),\n succeeded: this.isSucceeded(),\n canceled: this.isCanceled(),\n failed: this.isFailed(),\n ended: this.isEnded(),\n\n total,\n processed,\n progressPercent: this.calculateProgressPercent(),\n elapsedMillis: this.calculateElapsedMillis(),\n\n errors: this.isFailed() ? errors : undefined,\n result: this.isSucceeded() ? result : undefined,\n }\n }\n\n private async executeJobs(): Promise<void> {\n this.logDebug(`- ${this.total} inner jobs found`)\n\n for (let i = 0; i < this.innerJobs.length; i++) {\n // canceled between two inner jobs: the next ones must not be started\n if (this.cancelRequested) break\n\n this.logDebug(`- executing inner job ${i + 1}`)\n this.currentInnerJobIndex = i\n const currentInnerJob = this.innerJobs[i]\n if (currentInnerJob.context) {\n Object.assign(this.context, currentInnerJob.context)\n }\n currentInnerJob.context = this.context\n currentInnerJob.onEvent((event) => {\n // handle the inner job events in sequence; errors are logged (they would be unhandled rejections otherwise)\n this.innerJobEventsQueue = this.innerJobEventsQueue\n .then(() => this.onInnerJobEvent(event))\n .catch((error) => this.logError(error?.stack ?? error))\n })\n\n // The inner job shares this very context object, and its own start()/executeInTransaction()\n // clear `context.tx` when they terminate: without saving and restoring it here, the first\n // inner job would wipe the parent's transaction handle, leaving every following inner job\n // (and the parent's own beforeSuccess()/beforeEnd()) running outside of the transaction.\n // start() never throws (it swallows/records errors internally), so a plain restore is enough.\n const parentTx = this.context.tx\n await currentInnerJob.start(parentTx)\n this.context.tx = parentTx\n // the inner job events (e.g. canceled, failed) update the status of this job\n await this.innerJobEventsQueue\n\n if (this.cancelRequested || this.isCanceled() || currentInnerJob.isCanceled()) {\n break\n }\n if (currentInnerJob.isSucceeded()) {\n this.incrementProcessedItems()\n } else if (this.stopOnInnerJobFailure) {\n break\n }\n }\n if (\n !this.stopOnInnerJobFailure &&\n !this.cancelRequested &&\n this.innerJobs.some((innerJob) => innerJob.isFailed())\n ) {\n // set only after all inner jobs have run: setting it at the first failure would end this job (onEnd) too early\n await this.runBeforeEnd()\n await this.setStatus(JobStatus.failed)\n }\n\n this.logDebug(`- ${this.processed} inner jobs processed successfully`)\n }\n\n /**\n * Runs the job's own work when it has no inner jobs. Default implementation is a no-op, so\n * subclasses that only supply inner jobs (and never override this) don't need to. It also means\n * a subclass can safely call `super.execute()` defensively without knowing whether any ancestor\n * overrides it - if this were `abstract` instead, TypeScript would compile away any runtime\n * property for it entirely, so `super.execute()` would throw the moment no ancestor overrides it\n * (confirmed in production: a subclass whose immediate parent didn't override `execute()` crashed\n * with \"(intermediate value).execute is not a function\" the instant it called `super.execute()`).\n */\n protected async execute(): Promise<void> {\n // to be extended by subclasses\n }\n\n /**\n * Determines whether the job should actually run.\n * When it returns false, \"execute\" (or the inner jobs) will be skipped, but the job will still succeed.\n */\n protected shouldExecute(): Promise<boolean> {\n return Promise.resolve(true)\n }\n\n protected incrementProcessedItems(incrementBy = 1): void {\n this.processed += incrementBy\n }\n\n protected async setStatus(status: JobStatus): Promise<void> {\n this.logDebug(`set status: ${status}`)\n this.status = status\n\n const event: JobEvent = { type: JobEventType.statusChange, status, total: this.total, processed: this.processed }\n if (status === JobStatus.failed) {\n event.errors = this.errors\n }\n\n if (this.isEnded() && this.executingInTransaction) {\n // onEnd (and the event notifying the end) run when the transaction has ended\n this.deferredEndEvent = event\n return\n }\n await this.end(event)\n }\n\n private async end(event: JobEvent): Promise<void> {\n if (this.isEnded()) {\n this.logDebug('onEnd...')\n await this.onEnd()\n this.logDebug(`onEnd run. Job completed in ${this.elapsedTimePrettyFormat}`)\n }\n\n this.notifyEvent(event)\n }\n\n private async endDeferred(): Promise<void> {\n const event = this.deferredEndEvent\n if (!event) return\n this.deferredEndEvent = null\n await this.end(event)\n }\n\n /**\n * Inner job event handler.\n */\n protected async onInnerJobEvent(event: JobEvent): Promise<void> {\n const { status } = event\n if (status === JobStatus.canceled) {\n this.canceledByAdmin = this.getCurrentInnerJob()?.canceledByAdmin ?? false\n // beforeEnd must run before onEnd (it runs only once: it won't run again when the execution ends)\n await this.runBeforeEnd()\n return this.setStatus(status)\n }\n if (status === JobStatus.failed) {\n // report the errors of the failed inner job in the failed event (and in the errors) of this job\n const currentInnerJob = this.getCurrentInnerJob()\n if (currentInnerJob) {\n JobBase.mergeInnerJobErrors(this.errors, currentInnerJob)\n }\n // when not stopping on inner job failures, this job is set as failed after all the inner jobs have run\n if (this.stopOnInnerJobFailure) {\n await this.runBeforeEnd()\n await this.setStatus(status)\n }\n return\n }\n if (status === JobStatus.running) {\n this.notifyEvent({\n type: JobEventType.progress,\n status: this.status,\n total: this.total,\n processed: this.processed,\n })\n return\n }\n this.logDebug(`Unknown inner job status: ${status}`)\n }\n\n /**\n * Called when the job just has been started.\n */\n protected async onStart(): Promise<void> {\n this.startTime = new Date()\n await this.setStatus(JobStatus.running)\n }\n\n /**\n * Called before beforeEnd only if the status will change to 'success'.\n * Default implementation stores the value returned by generateResult() via setResult().\n * It runs INSIDE the current db transaction.\n */\n protected async beforeSuccess(): Promise<void> {\n this.setResult(await this.generateResult())\n }\n\n /**\n * Computes the value beforeSuccess() will store as this job's result.\n * Default implementation returns whatever is already in `this.result`\n * (e.g. accumulated via earlier setResult() calls during execute()).\n */\n protected generateResult(): Promise<R | undefined> {\n return Promise.resolve(this.result)\n }\n\n /**\n * Updates this job's result. When both the current and incoming values are plain objects,\n * they are merged (Object.assign); otherwise the incoming value replaces the current one.\n * This keeps it safe for subclasses whose result is a primitive (e.g. a plain number) as\n * well as ones that build an object result incrementally across multiple setResult() calls.\n */\n protected setResult(result: R | undefined): void {\n if (result && typeof result === 'object' && this.result && typeof this.result === 'object') {\n Object.assign(this.result, result)\n } else {\n this.result = result\n }\n }\n\n /**\n * Called before onEnd. Useful for flushing resources used by the job before it terminates completely.\n * It runs INSIDE the current db transaction.\n */\n protected beforeEnd(): Promise<void> {\n this.logDebug('Cleanup')\n return Promise.resolve()\n }\n\n /**\n * Called when the job status changes to success, failed or canceled\n * (it runs OUTSIDE of the current db transaction)\n */\n protected onEnd(): Promise<void> {\n this.endTime = new Date()\n this.cancelNotifyProgress()\n return Promise.resolve()\n }\n\n protected addError(error: any, errorKey?: string): void {\n const key = errorKey ?? String(this.processed + 1)\n this.errors[key] = error\n }\n\n protected throwError(errorKey: string, params?: Dictionary<any>): void {\n // SystemError: getErrorInfo will report the translatable key instead of a generic error\n throw new SystemError(errorKey, params)\n }\n\n protected getErrorInfo(error: any): { key: string; params: Record<string, any> } {\n if (error instanceof SystemError) {\n return { key: `appErrors:${error.key}`, params: error.params }\n }\n return { key: 'appErrors:generic', params: { text: error.toString() } }\n }\n\n protected abstract createLogger(): Logger\n\n protected logDebug(...msgs: any[]): void {\n this.logger.debug(...msgs)\n }\n\n protected logInfo(...msgs: any[]): void {\n this.logger.info(...msgs)\n }\n\n protected logWarn(...msgs: any[]): void {\n this.logger.warn(...msgs)\n }\n\n protected logError(...msgs: any[]): void {\n this.logger.error(...msgs)\n }\n\n private get elapsedTimePrettyFormat(): string {\n const elapsedTime = this.calculateElapsedMillis()\n const elapsedMins = Math.floor(elapsedTime / 1000 / 60)\n const elapsedSeconds = String(Math.floor(elapsedTime / 1000) % 60).padStart(2, '0')\n const elapsedMillis = String(elapsedTime % 1000).padStart(3, '0')\n return `${elapsedMins}:${elapsedSeconds}.${elapsedMillis}`\n }\n\n private calculatePartialProgress(): number {\n if (this.isSucceeded()) return 100\n if (this.total > 0) return Math.floor((100 * this.processed) / this.total)\n return 0\n }\n\n private calculateProgressPercent(): number {\n const partialProgress = this.calculatePartialProgress()\n const currentInnerJob = this.getCurrentInnerJob()\n if (\n !currentInnerJob ||\n this.currentInnerJobIndex < 0 ||\n partialProgress === 100 ||\n this.processed > this.currentInnerJobIndex\n ) {\n return partialProgress\n }\n return partialProgress + Math.floor(currentInnerJob.calculateProgressPercent() / this.total)\n }\n\n private calculateElapsedMillis(): number {\n if (!this.startTime) return 0\n return (this.endTime ?? new Date()).getTime() - this.startTime.getTime()\n }\n\n /**\n * Notifies a progress event to the registered listener, throttled so that fast-changing\n * progress does not flood consumers (e.g. web socket clients).\n */\n private notifyProgress(): void {\n // onEnd has already cleared the throttle: no progress events after the job has ended\n if (this.isEnded()) return\n\n const run = (): void => {\n this.progressThrottleTimeoutId = undefined\n this.progressThrottleLastRunTime = Date.now()\n this.notifyEvent({\n type: JobEventType.progress,\n status: this.status,\n total: this.total,\n processed: this.processed,\n })\n }\n\n if (this.progressThrottleTimeoutId) return\n\n const elapsed = Date.now() - this.progressThrottleLastRunTime\n if (!this.progressThrottleLastRunTime || elapsed >= PROGRESS_NOTIFICATION_THROTTLE_MILLIS) {\n run()\n } else {\n this.progressThrottleTimeoutId = setTimeout(run, PROGRESS_NOTIFICATION_THROTTLE_MILLIS - elapsed)\n }\n }\n\n /**\n * Cancels any pending throttled progress notification and resets the throttle state.\n */\n private cancelNotifyProgress(): void {\n if (this.progressThrottleTimeoutId) {\n clearTimeout(this.progressThrottleTimeoutId)\n this.progressThrottleTimeoutId = undefined\n }\n this.progressThrottleLastRunTime = 0\n }\n\n /**\n * Notifies the event to the registered listener.\n * Listener errors are only logged: they would otherwise fail the job or, when thrown inside the progress\n * throttle timeout, crash the process.\n */\n private notifyEvent(event: JobEvent): void {\n const listener = this.eventListener\n if (!listener) return\n try {\n Promise.resolve(listener(event)).catch((error) => this.logListenerError(error))\n } catch (error) {\n this.logListenerError(error)\n }\n }\n\n private logListenerError(error: any): void {\n this.logError(`error in job event listener: ${error?.stack ?? error}`)\n }\n}\n"]}
|