IGNITE-5906 Fixed race on activities merge.

Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/6bdad4dd
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/6bdad4dd
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/6bdad4dd

Branch: refs/heads/ignite-5872
Commit: 6bdad4dd6a5d6f3210051f47af5a51f0bd52f73c
Parents: 20969c0
Author: Andrey Novikov <anovi...@gridgain.com>
Authored: Thu Aug 3 10:45:59 2017 +0700
Committer: Alexey Kuznetsov <akuznet...@apache.org>
Committed: Thu Aug 3 10:45:59 2017 +0700

----------------------------------------------------------------------
 modules/web-console/backend/app/mongo.js           |  2 +-
 modules/web-console/backend/middlewares/api.js     |  9 ++++-----
 modules/web-console/backend/services/activities.js | 12 ++----------
 3 files changed, 7 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/6bdad4dd/modules/web-console/backend/app/mongo.js
----------------------------------------------------------------------
diff --git a/modules/web-console/backend/app/mongo.js 
b/modules/web-console/backend/app/mongo.js
index 57af928..0bc0e5c 100644
--- a/modules/web-console/backend/app/mongo.js
+++ b/modules/web-console/backend/app/mongo.js
@@ -1055,7 +1055,7 @@ module.exports.factory = function(passportMongo, 
settings, pluginMongo, mongoose
         date: Date,
         group: String,
         action: String,
-        amount: { type: Number, default: 1 }
+        amount: { type: Number, default: 0 }
     });
 
     ActivitiesSchema.index({ owner: 1, group: 1, action: 1, date: 1}, { 
unique: true });

http://git-wip-us.apache.org/repos/asf/ignite/blob/6bdad4dd/modules/web-console/backend/middlewares/api.js
----------------------------------------------------------------------
diff --git a/modules/web-console/backend/middlewares/api.js 
b/modules/web-console/backend/middlewares/api.js
index 2c4c1bd..aa599ae 100644
--- a/modules/web-console/backend/middlewares/api.js
+++ b/modules/web-console/backend/middlewares/api.js
@@ -20,16 +20,15 @@
 // Fire me up!
 
 module.exports = {
-    implements: 'middlewares:api',
-    inject: ['require("mongodb-core")']
+    implements: 'middlewares:api'
 };
 
-module.exports.factory = (mongodb) => {
+module.exports.factory = () => {
     return (req, res, next) => {
         res.api = {
             error(err) {
-                if (err instanceof mongodb.MongoError)
-                    res.status(500).send(err.message);
+                if (err.name === 'MongoError')
+                    return res.status(500).send(err.message);
 
                 res.status(err.httpCode || err.code || 500).send(err.message);
             },

http://git-wip-us.apache.org/repos/asf/ignite/blob/6bdad4dd/modules/web-console/backend/services/activities.js
----------------------------------------------------------------------
diff --git a/modules/web-console/backend/services/activities.js 
b/modules/web-console/backend/services/activities.js
index afde8e7..a049f65 100644
--- a/modules/web-console/backend/services/activities.js
+++ b/modules/web-console/backend/services/activities.js
@@ -50,16 +50,8 @@ module.exports.factory = (_, mongo) => {
 
             const date = Date.UTC(now.getFullYear(), now.getMonth(), 1);
 
-            return mongo.Activities.findOne({owner, action, date}).exec()
-                .then((activity) => {
-                    if (activity) {
-                        activity.amount++;
-
-                        return activity.save();
-                    }
-
-                    return mongo.Activities.create({owner, action, group, 
date});
-                });
+            return mongo.Activities.findOneAndUpdate({owner, action, date},
+                {$set: {owner, group, action, date}, $inc: {amount: 1}}, {new: 
true, upsert: true}).exec();
         }
 
         static total({startDate, endDate}) {

Reply via email to