Skip to content

Commit

Permalink
🎨 do not run model listeners on import (#8720)
Browse files Browse the repository at this point in the history
no issue

- if you upload a huge import file, parallel operations can throw errors e.g. lock wait exceeds
- this can happen if multiple transactions run in parallel
- there is no need to run:
  1. the removal of active tokens on import, because imported users have no active session
  2. rescheduling logic on timezone, because importing scheduled posts works out of the box via the model layer (if a published date is detected and it's in the future, the post get's scheduled)
  • Loading branch information
kirrg001 authored and kevinansfield committed Jul 21, 2017
1 parent 60558a7 commit d6aaf2d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
21 changes: 16 additions & 5 deletions core/server/models/base/listeners.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ events.on('token.added', function (tokenModel) {
* - if an active user get's deleted, we have to access the previous attributes, because this is how bookshelf works
* if you delete a user.
*/
events.on('user.deactivated', function (userModel) {
var options = {id: userModel.id || userModel.previousAttributes().id};
events.on('user.deactivated', function (userModel, options) {
options = options || {};
options = _.merge({}, options, {id: userModel.id || userModel.previousAttributes().id});

if (options.importing) {
return;
}

models.Accesstoken.destroyByUser(options)
.then(function () {
Expand All @@ -46,17 +51,23 @@ events.on('user.deactivated', function (userModel) {
* - reschedule all scheduled posts
* - draft scheduled posts, when the published_at would be in the past
*/
events.on('settings.active_timezone.edited', function (settingModel) {
events.on('settings.active_timezone.edited', function (settingModel, options) {
options = options || {};
options = _.merge({}, options, {context: {internal: true}});

var newTimezone = settingModel.attributes.value,
previousTimezone = settingModel._updatedAttributes.value,
timezoneOffsetDiff = moment.tz(previousTimezone).utcOffset() - moment.tz(newTimezone).utcOffset(),
options = {context: {internal: true}};
timezoneOffsetDiff = moment.tz(previousTimezone).utcOffset() - moment.tz(newTimezone).utcOffset();

// CASE: TZ was updated, but did not change
if (previousTimezone === newTimezone) {
return;
}

if (options.importing) {
return;
}

/**
* CASE:
* `Post.findAll` and the Post.edit` must run in one single transaction.
Expand Down
16 changes: 8 additions & 8 deletions core/server/models/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,23 +57,23 @@ Settings = ghostBookshelf.Model.extend({
};
},

emitChange: function emitChange(event) {
events.emit('settings' + '.' + event, this);
emitChange: function emitChange(event, options) {
events.emit('settings' + '.' + event, this, options);
},

onDestroyed: function onDestroyed(model) {
onDestroyed: function onDestroyed(model, response, options) {
model.emitChange('deleted');
model.emitChange(model.attributes.key + '.' + 'deleted');
model.emitChange(model.attributes.key + '.' + 'deleted', options);
},

onCreated: function onCreated(model) {
onCreated: function onCreated(model, response, options) {
model.emitChange('added');
model.emitChange(model.attributes.key + '.' + 'added');
model.emitChange(model.attributes.key + '.' + 'added', options);
},

onUpdated: function onUpdated(model) {
onUpdated: function onUpdated(model, response, options) {
model.emitChange('edited');
model.emitChange(model.attributes.key + '.' + 'edited');
model.emitChange(model.attributes.key + '.' + 'edited', options);
},

onValidate: function onValidate() {
Expand Down
12 changes: 6 additions & 6 deletions core/server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ User = ghostBookshelf.Model.extend({
}, baseDefaults);
},

emitChange: function emitChange(event) {
events.emit('user' + '.' + event, this);
emitChange: function emitChange(event, options) {
events.emit('user' + '.' + event, this, options);
},

onDestroyed: function onDestroyed(model) {
onDestroyed: function onDestroyed(model, response, options) {
if (_.includes(activeStates, model.previous('status'))) {
model.emitChange('deactivated');
model.emitChange('deactivated', options);
}

model.emitChange('deleted');
Expand All @@ -73,12 +73,12 @@ User = ghostBookshelf.Model.extend({
}
},

onUpdated: function onUpdated(model) {
onUpdated: function onUpdated(model, response, options) {
model.statusChanging = model.get('status') !== model.updated('status');
model.isActive = _.includes(activeStates, model.get('status'));

if (model.statusChanging) {
model.emitChange(model.isActive ? 'activated' : 'deactivated');
model.emitChange(model.isActive ? 'activated' : 'deactivated', options);
} else {
if (model.isActive) {
model.emitChange('activated.edited');
Expand Down

0 comments on commit d6aaf2d

Please sign in to comment.