3
3
3ton2021-08-25 11:16:03
ruby
3ton, 2021-08-25 11:16:03

How to ensure that an object is stored in MONGO using ruby ​​Mongoid::Document?

The telephony project uses RUBY(Adhearsion).
Events and objects are stored in MongoDB.
To work, the Mongoid::Document library is used.
In some situations, I noticed that the object's status attribute does not change from EXECUTING to EXECUTED I
hung logging to a file by event in the after_save model and dumped it into the Session.inspect log

after_save do |session|
  MyLogger.add2log("after save session: #{session.inspect}")
end

it turns out that at the moment after_save is called, in my understanding, the session object will have the state of the attributes after saving. and judging by the log, status: "EXECUTED" is really indicated there ,
but the value status: "EXECUTING" remains in the database and this is not related to delays, it is the same after a minute and after 5 and after an hour.

where to look for the problem and how to solve it?

UPD: it should be noted that Adhearsion uses Celluloid, which implies multi-threaded application work and several threads can simultaneously access the same object in the base documents, but this happens (an attribute change in the object in ruby, but not saving the change in the mongo document) in less than 10% of cases

Answer the question

In order to leave comments, you need to log in

1 answer(s)
3
3ton, 2021-08-25
@3ton

here is the footcloth of changes

для надежности навесил трекинг на изменения объекта через include Mongoid::History::Trackable
каждая сессия может иметь один и более объектов call - Это ноги самой сессии, в нормальной ситуации их 2 - один звонок на один номер, другой на второй и потом их соеденяет в сессию
/* 1 createdAt:25.08.2021, 10:41:47*/
{
  "_id" : ObjectId("6125f43b25930e44e8f71eec"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f0a425930e44e8f71e75")
    }
  ],
  "scope" : "call",
  "original" : {
    
  },
  "modified" : {
    "duration" : NumberInt(897)
  },
  "version" : NumberInt(5),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:41:47.374+03:00"),
  "created_at" : ISODate("2021-08-25T10:41:47.374+03:00")
},

/* 2 createdAt:25.08.2021, 10:41:47*/
{
  "_id" : ObjectId("6125f43b25930e44e8f71eeb"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f0a425930e44e8f71e75")
    }
  ],
  "scope" : "call",
  "original" : {
    "status" : "CONFERENCE (-1)"
  },
  "modified" : {
    "stop" : ISODate("2021-08-25T10:41:47.369+03:00"),
    "status" : "OK (0)"
  },
  "version" : NumberInt(4),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:41:47.370+03:00"),
  "created_at" : ISODate("2021-08-25T10:41:47.370+03:00")
},

/* 3 createdAt:25.08.2021, 10:41:47*/
{
  "_id" : ObjectId("6125f43b25930e44e8f71eea"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    }
  ],
  "scope" : "session",
  "original" : {
    "status" : "EXECUTING"
  },
  "modified" : {
    "status" : "EXECUTED"
  },
  "version" : NumberInt(7),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:41:47.357+03:00"),
  "created_at" : ISODate("2021-08-25T10:41:47.357+03:00")
},

/* 4 createdAt:25.08.2021, 10:41:47*/
{
  "_id" : ObjectId("6125f43b25930e44e8f71ee9"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    }
  ],
  "scope" : "session",
  "original" : {
    
  },
  "modified" : {
    "duration" : NumberInt(920),
    "billing_duration" : NumberInt(897),
    "cost" : NumberInt(1659)
  },
  "version" : NumberInt(6),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:41:47.326+03:00"),
  "created_at" : ISODate("2021-08-25T10:41:47.326+03:00")
},

/* 5 createdAt:25.08.2021, 10:41:47*/
{
  "_id" : ObjectId("6125f43b25930e44e8f71ee8"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    }
  ],
  "scope" : "session",
  "original" : {
    
  },
  "modified" : {
    "stop" : ISODate("2021-08-25T10:41:47.320+03:00")
  },
  "version" : NumberInt(5),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:41:47.321+03:00"),
  "created_at" : ISODate("2021-08-25T10:41:47.321+03:00")
},

/* 6 createdAt:25.08.2021, 10:41:47*/
{
  "_id" : ObjectId("6125f43b25930e44e8f71ee7"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f09c25930e44e8f71e70")
    }
  ],
  "scope" : "call",
  "original" : {
    
  },
  "modified" : {
    "duration" : NumberInt(920)
  },
  "version" : NumberInt(6),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:41:47.316+03:00"),
  "created_at" : ISODate("2021-08-25T10:41:47.316+03:00")
},

/* 7 createdAt:25.08.2021, 10:41:47*/
{
  "_id" : ObjectId("6125f43b25930e44e8f71ee6"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f09c25930e44e8f71e70")
    }
  ],
  "scope" : "call",
  "original" : {
    "status" : "CONFERENCE (-1)"
  },
  "modified" : {
    "status" : "OK (0)",
    "stop" : ISODate("2021-08-25T10:41:47.313+03:00")
  },
  "version" : NumberInt(5),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:41:47.314+03:00"),
  "created_at" : ISODate("2021-08-25T10:41:47.314+03:00")
},

/* 8 createdAt:25.08.2021, 10:26:51*/
{
  "_id" : ObjectId("6125f0bb25930e44e8f71e7a"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    }
  ],
  "scope" : "session",
  "original" : {
    "status" : "WAITING"
  },
  "modified" : {
    "status" : "EXECUTING"
  },
  "version" : NumberInt(4),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:26:51.267+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:51.267+03:00")
},

/* 9 createdAt:25.08.2021, 10:26:51*/
{
  "_id" : ObjectId("6125f0bb25930e44e8f71e79"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f09c25930e44e8f71e70")
    }
  ],
  "scope" : "call",
  "original" : {
    "status" : "OK (0)"
  },
  "modified" : {
    "status" : "CONFERENCE (-1)"
  },
  "version" : NumberInt(4),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:26:51.217+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:51.217+03:00")
},

/* 10 createdAt:25.08.2021, 10:26:51*/
{
  "_id" : ObjectId("6125f0bb25930e44e8f71e78"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f0a425930e44e8f71e75")
    }
  ],
  "scope" : "call",
  "original" : {
    "status" : "{unknown}"
  },
  "modified" : {
    "status" : "CONFERENCE (-1)",
    "start" : ISODate("2021-08-25T10:26:51.213+03:00")
  },
  "version" : NumberInt(3),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:26:51.214+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:51.214+03:00")
},

/* 11 createdAt:25.08.2021, 10:26:28*/
{
  "_id" : ObjectId("6125f0a425930e44e8f71e76"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f0a425930e44e8f71e75")
    }
  ],
  "scope" : "call",
  "original" : {
    
  },
  "modified" : {
    "callstart" : ISODate("2021-08-25T10:26:28.315+03:00")
  },
  "version" : NumberInt(2),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:26:28.316+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:28.316+03:00")
},

/* 12 createdAt:25.08.2021, 10:26:28*/
{
  "_id" : ObjectId("6125f0a425930e44e8f71e74"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f0a425930e44e8f71e75")
    }
  ],
  "scope" : "call",
  "original" : {
    
  },
  "modified" : {
    "status" : "{unknown}"
  },
  "version" : NumberInt(1),
  "action" : "create",
  "updated_at" : ISODate("2021-08-25T10:26:28.312+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:28.312+03:00")
},

/* 13 createdAt:25.08.2021, 10:26:28*/
{
  "_id" : ObjectId("6125f0a425930e44e8f71e73"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f09c25930e44e8f71e70")
    }
  ],
  "scope" : "call",
  "original" : {
    "status" : "{unknown}"
  },
  "modified" : {
    "status" : "OK (0)",
    "start" : ISODate("2021-08-25T10:26:28.309+03:00")
  },
  "version" : NumberInt(3),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:26:28.310+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:28.310+03:00")
},

/* 14 createdAt:25.08.2021, 10:26:20*/
{
  "_id" : ObjectId("6125f09c25930e44e8f71e71"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f09c25930e44e8f71e70")
    }
  ],
  "scope" : "call",
  "original" : {
    
  },
  "modified" : {
    "callstart" : ISODate("2021-08-25T10:26:20.288+03:00")
  },
  "version" : NumberInt(2),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:26:20.289+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:20.289+03:00")
},

/* 15 createdAt:25.08.2021, 10:26:20*/
{
  "_id" : ObjectId("6125f09c25930e44e8f71e6f"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    },
    {
      "name" : "calls",
      "id" : ObjectId("6125f09c25930e44e8f71e70")
    }
  ],
  "scope" : "call",
  "original" : {
    
  },
  "modified" : {
    "status" : "{unknown}"
  },
  "version" : NumberInt(1),
  "action" : "create",
  "updated_at" : ISODate("2021-08-25T10:26:20.286+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:20.286+03:00")
},

/* 16 createdAt:25.08.2021, 10:26:20*/
{
  "_id" : ObjectId("6125f09c25930e44e8f71e6e"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    }
  ],
  "scope" : "session",
  "original" : {
    
  },
  "modified" : {
    "start" : ISODate("2021-08-25T10:26:20.281+03:00")
  },
  "version" : NumberInt(3),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:26:20.282+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:20.282+03:00")
},

/* 17 createdAt:25.08.2021, 10:26:20*/
{
  "_id" : ObjectId("6125f09c25930e44e8f71e6c"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    }
  ],
  "scope" : "session",
  "original" : {
    "status" : "CONFIRMED"
  },
  "modified" : {
    "status" : "WAITING"
  },
  "version" : NumberInt(2),
  "action" : "update",
  "updated_at" : ISODate("2021-08-25T10:26:20.272+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:20.272+03:00")
},

/* 18 createdAt:25.08.2021, 10:26:16*/
{
  "_id" : ObjectId("6125f09825930e44e8f71e6a"),
  "association_chain" : [
    {
      "name" : "Session",
      "id" : ObjectId("6125f09825930e44e8f71e69")
    }
  ],
  "scope" : "session",
  "original" : {
    
  },
  "modified" : {
    "status" : "CONFIRMED",
    "billing_type" : "b",
    "uid" : NumberInt(6252265)
  },
  "version" : NumberInt(1),
  "action" : "create",
  "updated_at" : ISODate("2021-08-25T10:26:16.523+03:00"),
  "created_at" : ISODate("2021-08-25T10:26:16.523+03:00")
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question