cleos push action cron schedule '["andrew", "mydapp", "cleanup", 42]' -p andrew
ACTION run(uint32_t polling_interval, uint32_t rows_count) {
// Make sure the transaction was created by the current contract
require_auth(get_self());
// Check whether execution should be stopped
if (stop_execution.get())
return;
// Record processing
scan_schedules(rows_count);
// Call run again in polling_interval seconds
create_transaction(_self, _self, "run", polling_interval, make_tuple(polling_interval, rows_count));
}
template<class ...TParams>
void create_transaction(name payer, name account, const string &action, uint32_t delay,
const std::tuple<TParams...>& args) {
// Create a deferred transaction with the required delay
eosio::transaction t;
t.actions.emplace_back(
permission_level(_code, "active"_n),
account,
name(action),
args);
t.delay_sec = delay;
// You will need a unique id for bug fixing
auto sender_id = unique_id.get();
t.send(sender_id, payer);
unique_id.set(sender_id + 1, _code);
}
// We get the unix-time of the next block creation
time_point_sec current_time(now());
// We check whether it’s time to execute the task
if (current_time >= item.next_run) {
const name& account_from = item.from;
// Make sure the user has enough funds on his balance account
if (get_balance(account_from) >= CALL_PRICE) {
reduce_balance(account_from, CALL_PRICE);
create_transaction(account_from, item.account, item.action, item.period, tuple<name>(account_from));
// Refresh the runtime
cron_table.modify(item, _self, [&](auto& row) {
row.next_run = item.next_run + item.period;
});
}