1
0
Fork 0
arangodb/arangod/V8Server/V8TimerTask.cpp

111 lines
3.9 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// DISCLAIMER
///
/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany
/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
/// Copyright holder is ArangoDB GmbH, Cologne, Germany
///
/// @author Dr. Frank Celler
////////////////////////////////////////////////////////////////////////////////
#include "V8TimerTask.h"
#include "Basics/json.h"
#include "Dispatcher/Dispatcher.h"
#include "Scheduler/Scheduler.h"
#include "V8/v8-conv.h"
#include "V8Server/V8Job.h"
#include "VocBase/server.h"
using namespace std;
using namespace triagens::rest;
using namespace triagens::arango;
V8TimerTask::V8TimerTask(std::string const& id, std::string const& name,
TRI_vocbase_t* vocbase, ApplicationV8* v8Dealer,
Scheduler* scheduler, Dispatcher* dispatcher,
double offset, std::string const& command,
TRI_json_t* parameters, bool allowUseDatabase)
: Task(id, name),
TimerTask(id, (offset <= 0.0 ? 0.00001 : offset)), // offset must be (at
// least slightly)
// greater than zero,
// otherwise
// the timertask will not execute the task at all
_vocbase(vocbase),
_v8Dealer(v8Dealer),
_dispatcher(dispatcher),
_command(command),
_parameters(parameters),
_created(TRI_microtime()),
_allowUseDatabase(allowUseDatabase) {
TRI_ASSERT(vocbase != nullptr);
// increase reference counter for the database used
TRI_UseVocBase(_vocbase);
}
V8TimerTask::~V8TimerTask() {
// decrease reference counter for the database used
TRI_ReleaseVocBase(_vocbase);
if (_parameters != nullptr) {
TRI_FreeJson(TRI_UNKNOWN_MEM_ZONE, _parameters);
}
}
////////////////////////////////////////////////////////////////////////////////
/// @brief get a task specific description in JSON format
////////////////////////////////////////////////////////////////////////////////
void V8TimerTask::getDescription(TRI_json_t* json) const {
TimerTask::getDescription(json);
TRI_json_t* created = TRI_CreateNumberJson(TRI_UNKNOWN_MEM_ZONE, _created);
TRI_Insert3ObjectJson(TRI_UNKNOWN_MEM_ZONE, json, "created", created);
TRI_json_t* cmd = TRI_CreateStringCopyJson(TRI_UNKNOWN_MEM_ZONE,
_command.c_str(), _command.size());
TRI_Insert3ObjectJson(TRI_UNKNOWN_MEM_ZONE, json, "command", cmd);
TRI_json_t* db = TRI_CreateStringCopyJson(
TRI_UNKNOWN_MEM_ZONE, _vocbase->_name, strlen(_vocbase->_name));
TRI_Insert3ObjectJson(TRI_UNKNOWN_MEM_ZONE, json, "database", db);
}
////////////////////////////////////////////////////////////////////////////////
/// @brief handles the timer event
////////////////////////////////////////////////////////////////////////////////
bool V8TimerTask::handleTimeout() {
std::unique_ptr<Job> job(new V8Job(
_vocbase, _v8Dealer, "(function (params) { " + _command + " } )(params);",
_parameters, _allowUseDatabase));
_dispatcher->addJob(job);
// note: this will destroy the task (i.e. ourselves!!)
_scheduler->destroyTask(this);
return true;
}