add parameter json string to pending task for saving additional infos like blockchain type in db

- add set and getter functions for param in controller::PendingTask and model::table::PendingTask
- call it inside transactions to save blockchainType as param and in load Transaction from db
This commit is contained in:
einhornimmond 2021-04-07 12:37:02 +02:00
parent d659dfb886
commit ad147cd8da
7 changed files with 109 additions and 13 deletions

View File

@ -6,6 +6,7 @@ CREATE TABLE `pending_tasks` (
`created` datetime NOT NULL,
`finished` datetime DEFAULT '2000-01-01 000000',
`result_json` varchar(255) DEFAULT NULL,
`param_json` text NULL,
`task_type_id` int UNSIGNED NOT NULL,
`child_pending_task_id` int UNSIGNED DEFAULT 0,
`parent_pending_task_id` int UNSIGNED DEFAULT 0,

View File

@ -156,4 +156,27 @@ namespace controller {
}
timer.restart(interval);
}
bool PendingTask::setParam(const std::string& key, const Poco::Dynamic::Var& value, bool saveIntoDB/* = false*/)
{
auto model = getModel();
auto param = model->getParamJson();
param->set(key, value);
model->setParamJson(param);
if (saveIntoDB) {
return model->updateParam();
}
return true;
}
int PendingTask::getIntParam(const std::string& key)
{
auto model = getModel();
auto param = model->getParamJson();
auto paramVar = param->get(key);
if (!paramVar.isEmpty() && paramVar.isInteger()) {
return paramVar.extract<int>();
}
return -1;
}
}

View File

@ -5,6 +5,7 @@
#include "Poco/SharedPtr.h"
#include "Poco/Timer.h"
#include "Poco/Dynamic/Var.h"
#include "TableControllerBase.h"
#include "User.h"
@ -42,6 +43,9 @@ namespace controller {
void calledFromTimer(Poco::Timer& timer);
Poco::AutoPtr<controller::User> getUser();
bool setParam(const std::string& key, const Poco::Dynamic::Var& value, bool saveIntoDB = false);
int getIntParam(const std::string& key);
protected:
static Poco::AutoPtr<PendingTask> loadCorrectDerivedClass(model::table::PendingTask* dbModel);

View File

@ -42,6 +42,10 @@ namespace model {
throw new Poco::Exception("error parse from string");
}
mTransactionBody = TransactionBody::load(mProtoTransaction.body_bytes());
auto blockchain_type = getIntParam("blockchain_type");
if (blockchain_type >= 0) {
mTransactionBody->setBlockchainType((BlockchainType)blockchain_type);
}
auto body_bytes = mTransactionBody->getBodyBytes();
mBodyBytesHash = DRMakeStringHash(body_bytes.data(), body_bytes.size());
}
@ -106,6 +110,7 @@ namespace model {
}
auto body = TransactionBody::create(memo, receiver, amount, targetDate, blockchainType);
Poco::AutoPtr<Transaction> result = new Transaction(body);
result->setParam("blockchain_type", blockchainType);
auto model = result->getModel();
model->setHederaId(topic_id->getModel()->getID());
result->insertPendingTaskIntoDB(receiver, model::table::TASK_TYPE_CREATION);
@ -119,7 +124,7 @@ namespace model {
Poco::AutoPtr<controller::Group> receiverGroup,
Poco::UInt32 amount,
const std::string& memo,
BlockchainType blockhainType,
BlockchainType blockchainType,
bool inbound/* = true*/
)
{
@ -137,7 +142,7 @@ namespace model {
// LOCAL Transfer
if (receiverGroup.isNull() || sender_model->getGroupId() == receiverGroup->getModel()->getID())
{
auto body = TransactionBody::create(memo, sender, receiverPubkey, amount, blockhainType);
auto body = TransactionBody::create(memo, sender, receiverPubkey, amount, blockchainType);
Poco::AutoPtr<Transaction> transaction = new Transaction(body);
auto topic_id = controller::HederaId::find(sender_model->getGroupId(), network_type);
if (topic_id.isNull()) {
@ -186,9 +191,10 @@ namespace model {
return results;
}
auto body = TransactionBody::create(memo, sender, receiverPubkey, amount, blockhainType, pairedTransactionId, transaction_group);
auto body = TransactionBody::create(memo, sender, receiverPubkey, amount, blockchainType, pairedTransactionId, transaction_group);
Poco::AutoPtr<Transaction> transaction = new Transaction(body);
transaction->getModel()->setHederaId(topic_id->getModel()->getID());
auto transfer_transaction = transaction->getTransactionBody()->getTransferTransaction();
transfer_transaction->setOwnGroupAlias(sender_group->getModel()->getAlias());
transfer_transaction->setTargetGroupAlias(receiverGroup->getModel()->getAlias());
@ -200,6 +206,7 @@ namespace model {
for (auto it = results.begin(); it != results.end(); it++) {
if (!(*it)->getTransactionBody()->getTransferTransaction()->isInbound()) {
(*it)->setParam("blockchain_type", blockchainType);
(*it)->insertPendingTaskIntoDB(sender, model::table::TASK_TYPE_TRANSFER);
PendingTasksManager::getInstance()->addTask(*it);
}
@ -290,9 +297,9 @@ namespace model {
result->getModel()->setHederaId(topic_id->getModel()->getID());
// }
//result->insertPendingTaskIntoDB(receiver, model::table::TASK_TYPE_TRANSFER);
//PendingTasksManager::getInstance()->addTask(result);
result->setParam("blockchain_type", blockchainType);
result->insertPendingTaskIntoDB(receiver, model::table::TASK_TYPE_TRANSFER);
PendingTasksManager::getInstance()->addTask(result);
return result;
}

View File

@ -81,6 +81,7 @@ namespace model {
TransactionBase* getTransactionBase();
static BlockchainType blockchainTypeFromString(const std::string& blockainTypeString);
inline void setBlockchainType(BlockchainType blockchainType) { mBlockchainType = blockchainType;}
protected:
TransactionBody();

View File

@ -23,7 +23,7 @@ namespace model
PendingTask::PendingTask(const PendingTaskTuple& tuple)
: ModelBase(tuple.get<0>()), mUserId(tuple.get<1>()), mHederaId(tuple.get<2>()),
mRequest(tuple.get<3>()), mCreated(tuple.get<4>()), mFinished(tuple.get<5>()),
mResultJsonString(tuple.get<6>()), mTaskTypeId(tuple.get<7>()), mChildPendingTaskId(tuple.get<8>()), mParentPendingTaskId(tuple.get<9>())
mResultJsonString(tuple.get<6>()), mParamJsonString(tuple.get<7>()), mTaskTypeId(tuple.get<8>()), mChildPendingTaskId(tuple.get<9>()), mParentPendingTaskId(tuple.get<10>())
{
}
@ -46,6 +46,14 @@ namespace model
mResultJsonString = ss.str();
}
void PendingTask::setParamJson(Poco::JSON::Object::Ptr param)
{
UNIQUE_LOCK;
std::stringstream ss;
param->stringify(ss);
mResultJsonString = ss.str();
}
Poco::JSON::Object::Ptr PendingTask::getResultJson() const
{
std::string temp;
@ -68,6 +76,28 @@ namespace model
}
Poco::JSON::Object::Ptr PendingTask::getParamJson() const
{
std::string temp;
{
SHARED_LOCK;
temp = mParamJsonString;
}
Poco::JSON::Parser parser;
Poco::Dynamic::Var result;
try
{
result = parser.parse(temp);
}
catch (Poco::JSON::JSONException& jsone)
{
return nullptr;
}
return result.extract<Poco::JSON::Object::Ptr>();
}
bool PendingTask::updateRequest()
{
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
@ -120,6 +150,32 @@ namespace model
return 0;
}
bool PendingTask::updateParam()
{
Poco::ScopedLock<Poco::Mutex> _poco_lock(mWorkMutex);
SHARED_LOCK;
if (!mID) {
return 0;
}
auto cm = ConnectionManager::getInstance();
auto session = cm->getConnection(CONNECTION_MYSQL_LOGIN_SERVER);
Poco::Data::Statement update(session);
update << "UPDATE " << getTableName() << " SET param_json = ? where id = ?;",
use(mParamJsonString), use(mID);
try {
return 1 == update.execute();
}
catch (Poco::Exception& ex) {
addError(new ParamError(getTableName(), "[updateParam] mysql error by update", ex.displayText().data()));
addError(new ParamError(getTableName(), "data set: \n", toString().data()));
}
//printf("data valid: %s\n", toString().data());
return false;
}
std::string PendingTask::toString()
@ -178,9 +234,9 @@ namespace model
{
Poco::Data::Statement select(session);
select << "SELECT id, user_id, hedera_id, request, created, finished, result_json, task_type_id, child_pending_task_id, parent_pending_task_id FROM " << getTableName()
select << "SELECT id, user_id, hedera_id, request, created, finished, result_json, param_json, task_type_id, child_pending_task_id, parent_pending_task_id FROM " << getTableName()
<< " where " << fieldName << " = ?"
, into(mID), into(mUserId), into(mHederaId), into(mRequest), into(mCreated), into(mFinished), into(mResultJsonString),
, into(mID), into(mUserId), into(mHederaId), into(mRequest), into(mCreated), into(mFinished), into(mResultJsonString), into(mParamJsonString),
into(mTaskTypeId), into(mChildPendingTaskId), into(mParentPendingTaskId);
return select;
@ -190,7 +246,7 @@ namespace model
{
Poco::Data::Statement select(session);
select << "SELECT id, user_id, hedera_id, request, created, finished, result_json, task_type_id, child_pending_task_id, parent_pending_task_id FROM "
select << "SELECT id, user_id, hedera_id, request, created, finished, result_json, param_json, task_type_id, child_pending_task_id, parent_pending_task_id FROM "
<< getTableName() << " order by id";
return select;
@ -217,8 +273,8 @@ namespace model
Poco::Data::Statement insert(session);
insert << "INSERT INTO " << getTableName()
<< " (user_id, hedera_id, request, created, task_type_id, child_pending_task_id, parent_pending_task_id) VALUES(?,?,?,?,?,?,?)"
, use(mUserId), use(mHederaId), use(mRequest), use(mCreated), use(mTaskTypeId), use(mChildPendingTaskId), use(mParentPendingTaskId);
<< " (user_id, hedera_id, request, created, param_json, task_type_id, child_pending_task_id, parent_pending_task_id) VALUES(?,?,?,?,?,?,?,?)"
, use(mUserId), use(mHederaId), use(mRequest), use(mCreated), use(mParamJsonString), use(mTaskTypeId), use(mChildPendingTaskId), use(mParentPendingTaskId);
return insert;
}

View File

@ -22,7 +22,7 @@ namespace model {
};
typedef Poco::Tuple<int, int, int, Poco::Data::BLOB, Poco::DateTime, Poco::DateTime, std::string, int, int, int> PendingTaskTuple;
typedef Poco::Tuple<int, int, int, Poco::Data::BLOB, Poco::DateTime, Poco::DateTime, std::string, std::string, int, int, int> PendingTaskTuple;
class PendingTask : public ModelBase
{
@ -42,12 +42,14 @@ namespace model {
bool updateRequest();
bool updateFinishedAndResult();
bool updateParam();
inline int getUserId() const { SHARED_LOCK; return mUserId; }
inline int getHederaId() const { SHARED_LOCK; return mHederaId; }
inline const std::vector<unsigned char>& getRequest() const { SHARED_LOCK; return mRequest.content(); }
inline std::string getRequestCopy() const { SHARED_LOCK; return std::string((const char*)mRequest.content().data(), mRequest.content().size()); }
Poco::JSON::Object::Ptr getResultJson() const;
Poco::JSON::Object::Ptr getParamJson() const;
inline Poco::DateTime getCreated() const { SHARED_LOCK; return mCreated; }
inline TaskType getTaskType() const { SHARED_LOCK; return (TaskType)mTaskTypeId; }
inline const char* getTaskTypeString() const { SHARED_LOCK; return typeToString((TaskType)mTaskTypeId); }
@ -59,6 +61,7 @@ namespace model {
void setRequest(const std::string& serializedProto);
inline void setFinished(Poco::DateTime date) { UNIQUE_LOCK; mFinished = date; }
void setResultJson(Poco::JSON::Object::Ptr result);
void setParamJson(Poco::JSON::Object::Ptr param);
inline void setTaskType(TaskType type) { UNIQUE_LOCK; mTaskTypeId = type; }
inline void setChildPendingTaskId(int childPendingTaskId) {UNIQUE_LOCK; mChildPendingTaskId = childPendingTaskId;}
inline void setParentPendingTaskId(int parentPendingTaskId) { UNIQUE_LOCK; mParentPendingTaskId = parentPendingTaskId; }
@ -81,6 +84,7 @@ namespace model {
Poco::DateTime mCreated;
Poco::DateTime mFinished;
std::string mResultJsonString;
std::string mParamJsonString;
int mTaskTypeId;
int mChildPendingTaskId;
int mParentPendingTaskId;