Skip to content

Commit

Permalink
ScriptScheduler: added @once special schedule
Browse files Browse the repository at this point in the history
  • Loading branch information
obiltschnig committed Sep 23, 2016
1 parent d5a54f9 commit 40e188d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
2 changes: 2 additions & 0 deletions doc/00240-MacchinaScriptScheduler.page
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ In addition to the standard cron notation, the following special values can also
* @hourly: Run once an hour at the beginning of the hour (0 * * * *)
* @start: Run the script when the bundle containing it is started.
* @stop: Run the script when the bundle containing it is stopped.
* @once: Run the script at most once, when the bundle containing it
is started the first time.

Once a script has been started by the scheduler, it continues to run until it exits. By specifying the @start schedule, a script can run continuously. A script can also use the setTimeout() and setInterval() functions provided by the macchina.io platform to schedule tasks internally. For example, using setInterval(), a script can perform a certain task every 30 seconds:

Expand Down
39 changes: 36 additions & 3 deletions platform/OSP/JS/Scheduler/src/SchedulerExtensionPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include "Poco/Delegate.h"
#include "Poco/Event.h"
#include "Poco/String.h"
#include "Poco/Path.h"
#include "Poco/File.h"
#include "Poco/FileStream.h"
#include "Poco/MD5Engine.h"
#include "v8.h"
#include <memory>

Expand Down Expand Up @@ -289,14 +293,43 @@ void SchedulerExtensionPoint::handleExtension(Poco::OSP::Bundle::ConstPtr pBundl
_tasks.push_back(task);
}

task.pExecutor->run();

if (task.schedule.expression == "@start")
{
task.pExecutor->run();

CallExportedFunctionTask::Ptr pStartTask = new CallExportedFunctionTask(task.pExecutor, "start");
task.pExecutor->schedule(pStartTask);
pStartTask->wait();
}
else if (task.schedule.expression == "@once")
{
Poco::MD5Engine md5;
md5.update(scriptPath);

std::string onceFileName = ".once_";
onceFileName += Poco::DigestEngine::digestToHex(md5.digest());

Poco::OSP::BundleContext::Ptr pBundleContext = _pContext->contextForBundle(pBundle);
Poco::Path oncePath = pBundleContext->persistentDirectory();
oncePath.makeDirectory();
oncePath.setFileName(onceFileName);
Poco::File onceFile(oncePath.toString());
if (!onceFile.exists())
{
Poco::FileOutputStream ostr(onceFile.path());
ostr << scriptPath << "\n";
ostr.close();

task.pExecutor->run();

CallExportedFunctionTask::Ptr pStartTask = new CallExportedFunctionTask(task.pExecutor, "start");
task.pExecutor->schedule(pStartTask);
}
}
else
{
task.pExecutor->run();
}
}


Expand Down Expand Up @@ -354,7 +387,7 @@ void SchedulerExtensionPoint::parseSchedule(Schedule& schedule, const std::strin
std::string::const_iterator end = HOURLY.end();
parseSchedule(schedule, it, end);
}
else if (expr != "@start")
else if (expr != "@start" && expr != "@once")
{
std::string::const_iterator it = expr.begin();
std::string::const_iterator end = expr.end();
Expand Down

0 comments on commit 40e188d

Please sign in to comment.