Skip to content

Commit

Permalink
Merge pull request apostrophecms#1853 from apostrophecms/dgad-677-avo…
Browse files Browse the repository at this point in the history
…id-race-condition

load the global doc in two steps, first the actual doc and then any j…
  • Loading branch information
Tom Boutell authored Mar 27, 2019
2 parents 66055b9 + 2ef6406 commit 8472c7b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
40 changes: 36 additions & 4 deletions lib/modules/apostrophe-global/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,42 @@ module.exports = {
// with `(null, global)`.

self.findGlobal = function(req, callback) {
return self.find(req, { slug: self.slug })
.permission(false)
.sort(false)
.toObject(callback);
var global;
var cursor;
return async.series([
find, after
], function(err) {
if (err) {
return callback(err);
}
return callback(null, global);
});
function find(callback) {
cursor = self.find(req, { slug: self.slug })
.permission(false)
.sort(false)
.joins(false)
.areas(false);
return cursor.toObject(function(err, doc) {
global = doc;
// Make this available early, sans joins and area loaders,
// to avoid race conditions for modules like
// apostrophe-pieces-orderings-bundle if we wait
// for joins that might also need the global doc to find their
// default orderings, etc.
req.aposGlobalCore = global;
return callback(err);
});
}
function after(callback) {
if (!global) {
return callback(null);
}
cursor = cursor.clone();
cursor.joins(true);
cursor.areas(true);
return cursor.after([ global ], callback);
}
};

self.modulesReady = function(callback) {
Expand Down
42 changes: 42 additions & 0 deletions test/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,23 @@ describe('Global', function() {
secret: 'xxx',
port: 7900
},
'products': {
alias: 'products',
extend: 'apostrophe-pieces',
name: 'product'
},
'apostrophe-global': {
whileBusyDelay: 0.5,
addFields: [
{
name: 'testString',
type: 'string',
def: 'populated def'
},
{
name: '_featuredProducts',
type: 'joinByArray',
withType: 'product'
}
]
}
Expand Down Expand Up @@ -91,6 +101,38 @@ describe('Global', function() {
});
});

it('insert products via task', function() {
return apos.tasks.invoke('products:generate', [], {});
});

var product;

it('set a product join up in the global doc', function() {
var req = apos.tasks.getReq();
return apos.products.find(req).sort({ sortTitle: 1}).limit(1).toObject().then(function(object) {
assert(object);
product = object;
}).then(function() {
return apos.docs.db.update({
slug: 'global'
}, {
$set: {
featuredProductsIds: [ product._id ]
}
});
});
});

it('fetch the global doc, verify join', function() {
var req = apos.tasks.getAnonReq();
return apos.global.addGlobalToData(req).then(function() {
assert(req.data.global);
assert(req.data.global._featuredProducts);
assert(req.data.global._featuredProducts.length === 1);
assert(req.data.global._featuredProducts[0].slug === product.slug);
});
});

it('give global doc a workflowLocale property to simulate use with workflow', function() {
return apos.docs.db.update({
type: 'apostrophe-global'
Expand Down

0 comments on commit 8472c7b

Please sign in to comment.