diff --git a/src/imports/api/mixins/asyncReadyMixin.js b/src/imports/api/mixins/asyncReadyMixin.js new file mode 100644 index 0000000..80bc949 --- /dev/null +++ b/src/imports/api/mixins/asyncReadyMixin.js @@ -0,0 +1,43 @@ +const listKeys = (x= {}) => { + for (const nameKey in x) { + console.debug(nameKey, typeof x) + } +} +export const asyncReadyMixin = function (options) { + const { name, run } = options + const body = run.toString().split(/\n+/g) + options.isAsyncReady = body[1].includes('return Promise.asyncApply(() => {') + addToList(name, options.isAsyncReady) + + return options +} + +const internal = { list: [] } + +global.getAsyncReadySummary = ({ printNames } = {}) => { + const { list } = internal + const max = list.length + let ready = 0 + list.forEach(([name, isReady]) => { + if (isReady) { + ready++ + } + else if (printNames) { + console.log('Not Async Ready:', name) + } + }) + const notReady = max - ready + const divisor = max || 1 + const notReadyPerc = 100 * notReady / divisor + const readyPerc = 100 * ready / divisor + console.log('-------------------') + console.log('Async Ready Summary') + console.log('-------------------') + console.log('count', max) + console.log('ready', ready, `(${readyPerc.toFixed(2)}%)`) + console.log('todo!', notReady, `(${notReadyPerc.toFixed(2)}%)`) +} + +const addToList = (name, isAsyncReady) => { + internal.list.push([name, isAsyncReady]) +} diff --git a/src/imports/contexts/classroom/schoolclass/SchoolClass.js b/src/imports/contexts/classroom/schoolclass/SchoolClass.js index f2aac31..2adc04c 100644 --- a/src/imports/contexts/classroom/schoolclass/SchoolClass.js +++ b/src/imports/contexts/classroom/schoolclass/SchoolClass.js @@ -281,7 +281,7 @@ SchoolClass.methods.get = { schema: { _id: String }, - run: onServer(function ({ _id }) { + run: onServer(async function ({ _id }) { const { userId } = this const query = { $or: [ @@ -309,7 +309,7 @@ SchoolClass.methods.my = { }, 'ids.$': String }, - run: onServer(function ({ ids }) { + run: function myClasses ({ ids }) { const { userId } = this const query = { $or: [ @@ -326,7 +326,7 @@ SchoolClass.methods.my = { } return getCollection(SchoolClass.name).find(query).fetch() - }) + } } /** @@ -343,7 +343,7 @@ SchoolClass.methods.create = { 'timeFrame.to': SchoolClass.schema['timeFrame.to'] }, role: UserUtils.roles.teacher, - run: onServer(function createClass ({ title, timeFrame }) { + run: onServer(async function createClass ({ title, timeFrame }) { const { userId } = this const SchoolClassCollection = getCollection(SchoolClass.name) const insert = { title, createdBy: userId } @@ -466,7 +466,7 @@ SchoolClass.publications.single = { schema: { _id: String }, - run: onServer(function ({ _id }) { + run: onServer(async function ({ _id }) { const { userId } = this const query = { $or: [ @@ -483,7 +483,7 @@ SchoolClass.publications.single = { SchoolClass.publications.my = { name: 'schoolClass.publications.my', schema: {}, - run: onServer(function () { + run: onServer(async function () { const { userId } = this const query = { $or: [ diff --git a/src/imports/infrastructure/factories/createMethod.js b/src/imports/infrastructure/factories/createMethod.js index 7a46c56..4d9a850 100644 --- a/src/imports/infrastructure/factories/createMethod.js +++ b/src/imports/infrastructure/factories/createMethod.js @@ -2,8 +2,9 @@ import { createMethodFactory } from 'meteor/leaonline:method-factory' import { Schema } from '../../api/schema/Schema' import { checkPermissions } from '../../api/mixins/checkPermissions' import { logRuntimeEndpoints } from '../../api/mixins/logRuntimeEndpoints' +import { asyncReadyMixin } from '../../api/mixins/asyncReadyMixin' export const createMethod = createMethodFactory({ schemaFactory: Schema.create, - mixins: [checkPermissions, logRuntimeEndpoints] + mixins: [asyncReadyMixin, checkPermissions, logRuntimeEndpoints] }) diff --git a/src/imports/infrastructure/factories/createPublication.js b/src/imports/infrastructure/factories/createPublication.js index 5bdb246..22bf6f7 100644 --- a/src/imports/infrastructure/factories/createPublication.js +++ b/src/imports/infrastructure/factories/createPublication.js @@ -5,10 +5,11 @@ import { logError } from '../../api/errors/server/logerror' import { addErrorDetails } from '../../api/errors/server/addErrorDetails' import { createLog } from '../../api/log/createLog' import { logRuntimeEndpoints } from '../../api/mixins/logRuntimeEndpoints' +import { asyncReadyMixin } from '../../api/mixins/asyncReadyMixin' export const createPublication = createPublicationFactory({ schemaFactory: Schema.create, - mixins: [checkPermissions, logRuntimeEndpoints], + mixins: [asyncReadyMixin, checkPermissions, logRuntimeEndpoints], onError (publicationRuntimeError) { const env = this error('runtime error catch in publication [', env._name, ']') diff --git a/src/server/main.js b/src/server/main.js index 45cb2a9..9ee7683 100644 --- a/src/server/main.js +++ b/src/server/main.js @@ -2,3 +2,25 @@ import '../imports/startup/server/system' import '../imports/startup/server/accounts' import '../imports/startup/server/plugins' import '../imports/startup/server/contexts' + +global.analyzeMigrations = () => { + const methods = Object.entries(Meteor.server.publish_handlers) + let count = 0 + for (const [name, method] of methods) { + const isAsync = method.constructor.name === 'AsyncFunction' + if (!isAsync) { + console.log(name, isAsync) + count++ + } + } + + const asyncReady = { + count: methods.length - count, + perc: 100 * (methods.length - count) / (methods.length || 1) + } + const toMigrate = { count, perc: 100 * count / (methods.length || 1) } + + console.debug('Methods sumary:', methods.length, 'methods') + console.debug('async-ready', asyncReady.count, `(${asyncReady.perc}%)`) + console.debug('to migrate', toMigrate.count, `(${toMigrate.perc}%)`) +} \ No newline at end of file