feat: Job Scheduling (#3927)
* Adds back _JobSchedule as volatile class * wip * Restores jobs endpoints for creation, update and deletion * Adds tests * Fixes postgres tests * Enforce jobName exists before creating a schedule
This commit is contained in:
@@ -667,7 +667,7 @@ export class PostgresStorageAdapter {
|
||||
const joins = results.reduce((list, schema) => {
|
||||
return list.concat(joinTablesForSchema(schema.schema));
|
||||
}, []);
|
||||
const classes = ['_SCHEMA','_PushStatus','_JobStatus','_Hooks','_GlobalConfig', ...results.map(result => result.className), ...joins];
|
||||
const classes = ['_SCHEMA','_PushStatus','_JobStatus','_JobSchedule','_Hooks','_GlobalConfig', ...results.map(result => result.className), ...joins];
|
||||
return this._client.tx(t=>t.batch(classes.map(className=>t.none('DROP TABLE IF EXISTS $<className:name>', { className }))));
|
||||
}, error => {
|
||||
if (error.code === PostgresRelationDoesNotExistError) {
|
||||
|
||||
@@ -95,6 +95,16 @@ const defaultColumns = Object.freeze({
|
||||
"params": {type: 'Object'}, // params received when calling the job
|
||||
"finishedAt": {type: 'Date'}
|
||||
},
|
||||
_JobSchedule: {
|
||||
"jobName": {type:'String'},
|
||||
"description": {type:'String'},
|
||||
"params": {type:'String'},
|
||||
"startAfter": {type:'String'},
|
||||
"daysOfWeek": {type:'Array'},
|
||||
"timeOfDay": {type:'String'},
|
||||
"lastRun": {type:'Number'},
|
||||
"repeatMinutes":{type:'Number'}
|
||||
},
|
||||
_Hooks: {
|
||||
"functionName": {type:'String'},
|
||||
"className": {type:'String'},
|
||||
@@ -112,9 +122,9 @@ const requiredColumns = Object.freeze({
|
||||
_Role: ["name", "ACL"]
|
||||
});
|
||||
|
||||
const systemClasses = Object.freeze(['_User', '_Installation', '_Role', '_Session', '_Product', '_PushStatus', '_JobStatus']);
|
||||
const systemClasses = Object.freeze(['_User', '_Installation', '_Role', '_Session', '_Product', '_PushStatus', '_JobStatus', '_JobSchedule']);
|
||||
|
||||
const volatileClasses = Object.freeze(['_JobStatus', '_PushStatus', '_Hooks', '_GlobalConfig']);
|
||||
const volatileClasses = Object.freeze(['_JobStatus', '_PushStatus', '_Hooks', '_GlobalConfig', '_JobSchedule']);
|
||||
|
||||
// 10 alpha numberic chars + uppercase
|
||||
const userIdRegex = /^[a-zA-Z0-9]{10}$/;
|
||||
@@ -291,7 +301,12 @@ const _JobStatusSchema = convertSchemaToAdapterSchema(injectDefaultSchema({
|
||||
fields: {},
|
||||
classLevelPermissions: {}
|
||||
}));
|
||||
const VolatileClassesSchemas = [_HooksSchema, _JobStatusSchema, _PushStatusSchema, _GlobalConfigSchema];
|
||||
const _JobScheduleSchema = convertSchemaToAdapterSchema(injectDefaultSchema({
|
||||
className: "_JobSchedule",
|
||||
fields: {},
|
||||
classLevelPermissions: {}
|
||||
}));
|
||||
const VolatileClassesSchemas = [_HooksSchema, _JobStatusSchema, _JobScheduleSchema, _PushStatusSchema, _GlobalConfigSchema];
|
||||
|
||||
const dbTypeMatchesObjectType = (dbType, objectType) => {
|
||||
if (dbType.type !== objectType.type) return false;
|
||||
|
||||
@@ -1,20 +1,76 @@
|
||||
import PromiseRouter from '../PromiseRouter';
|
||||
const triggers = require('../triggers');
|
||||
import PromiseRouter from '../PromiseRouter';
|
||||
import Parse from 'parse/node';
|
||||
import rest from '../rest';
|
||||
const triggers = require('../triggers');
|
||||
const middleware = require('../middlewares');
|
||||
|
||||
function formatJobSchedule(job_schedule) {
|
||||
if (typeof job_schedule.startAfter === 'undefined') {
|
||||
job_schedule.startAfter = new Date().toISOString();
|
||||
}
|
||||
return job_schedule;
|
||||
}
|
||||
|
||||
function validateJobSchedule(config, job_schedule) {
|
||||
const jobs = triggers.getJobs(config.applicationId) || {};
|
||||
if (job_schedule.jobName && !jobs[job_schedule.jobName]) {
|
||||
throw new Parse.Error(Parse.Error.INTERNAL_SERVER_ERROR, 'Cannot Schedule a job that is not deployed');
|
||||
}
|
||||
}
|
||||
|
||||
export class CloudCodeRouter extends PromiseRouter {
|
||||
mountRoutes() {
|
||||
this.route('GET',`/cloud_code/jobs`, CloudCodeRouter.getJobs);
|
||||
this.route('GET', '/cloud_code/jobs', middleware.promiseEnforceMasterKeyAccess, CloudCodeRouter.getJobs);
|
||||
this.route('GET', '/cloud_code/jobs/data', middleware.promiseEnforceMasterKeyAccess, CloudCodeRouter.getJobsData);
|
||||
this.route('POST', '/cloud_code/jobs', middleware.promiseEnforceMasterKeyAccess, CloudCodeRouter.createJob);
|
||||
this.route('PUT', '/cloud_code/jobs/:objectId', middleware.promiseEnforceMasterKeyAccess, CloudCodeRouter.editJob);
|
||||
this.route('DELETE', '/cloud_code/jobs/:objectId', middleware.promiseEnforceMasterKeyAccess, CloudCodeRouter.deleteJob);
|
||||
}
|
||||
|
||||
static getJobs(req) {
|
||||
return rest.find(req.config, req.auth, '_JobSchedule', {}, {}).then((scheduledJobs) => {
|
||||
return {
|
||||
response: scheduledJobs.results
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static getJobsData(req) {
|
||||
const config = req.config;
|
||||
const jobs = triggers.getJobs(config.applicationId) || {};
|
||||
return Promise.resolve({
|
||||
response: Object.keys(jobs).map((jobName) => {
|
||||
return {
|
||||
jobName,
|
||||
return rest.find(req.config, req.auth, '_JobSchedule', {}, {}).then((scheduledJobs) => {
|
||||
return {
|
||||
response: {
|
||||
in_use: scheduledJobs.results.map((job) => job.jobName),
|
||||
jobs: Object.keys(jobs),
|
||||
}
|
||||
})
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
static createJob(req) {
|
||||
const { job_schedule } = req.body;
|
||||
validateJobSchedule(req.config, job_schedule);
|
||||
return rest.create(req.config, req.auth, '_JobSchedule', formatJobSchedule(job_schedule), req.client);
|
||||
}
|
||||
|
||||
static editJob(req) {
|
||||
const { objectId } = req.params;
|
||||
const { job_schedule } = req.body;
|
||||
validateJobSchedule(req.config, job_schedule);
|
||||
return rest.update(req.config, req.auth, '_JobSchedule', { objectId }, formatJobSchedule(job_schedule)).then((response) => {
|
||||
return {
|
||||
response
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static deleteJob(req) {
|
||||
const { objectId } = req.params;
|
||||
return rest.del(req.config, req.auth, '_JobSchedule', objectId).then((response) => {
|
||||
return {
|
||||
response
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +134,8 @@ function update(config, auth, className, restWhere, restObject, clientSDK) {
|
||||
});
|
||||
}
|
||||
|
||||
// Disallowing access to the _Role collection except by master key
|
||||
const classesWithMasterOnlyAccess = ['_JobStatus', '_PushStatus', '_Hooks', '_GlobalConfig', '_JobSchedule'];
|
||||
// Disallowing access to the _Role collection except by master key
|
||||
function enforceRoleSecurity(method, className, auth) {
|
||||
if (className === '_Installation' && !auth.isMaster) {
|
||||
if (method === 'delete' || method === 'find') {
|
||||
@@ -144,8 +145,7 @@ function enforceRoleSecurity(method, className, auth) {
|
||||
}
|
||||
|
||||
//all volatileClasses are masterKey only
|
||||
const volatileClasses = ['_JobStatus', '_PushStatus', '_Hooks', '_GlobalConfig'];
|
||||
if(volatileClasses.includes(className) && !auth.isMaster){
|
||||
if(classesWithMasterOnlyAccess.includes(className) && !auth.isMaster){
|
||||
const error = `Clients aren't allowed to perform the ${method} operation on the ${className} collection.`
|
||||
throw new Parse.Error(Parse.Error.OPERATION_FORBIDDEN, error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user