Skip to content

Commit

Permalink
Delay executions of triggers to help tackle (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgueret committed Dec 6, 2016
1 parent d521b3b commit 4b55abf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
11 changes: 10 additions & 1 deletion twine/common/db-schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* 1..DB_SCHEMA_VERSION must be handled individually in spindle_db_migrate_
* below.
*/
#define DB_SCHEMA_VERSION 24
#define DB_SCHEMA_VERSION 25

static int spindle_db_migrate_(SQL *restrict, const char *identifier, int newversion, void *restrict userdata);

Expand Down Expand Up @@ -498,6 +498,15 @@ spindle_db_migrate_(SQL *restrict sql, const char *identifier, int newversion, v
}
return 0;
}
// Version 25 introduces a timestamp to delay the execution of the triggers
if(newversion == 25)
{
if(sql_execute(sql, "ALTER TABLE \"triggers\" ADD COLUMN \"earliest_activation\" TIMESTAMP DEFAULT NULL"))
{
return -1;
}
return 0;
}
twine_logf(LOG_NOTICE, PLUGIN_NAME ": unsupported database schema version %d\n", newversion);
return -1;
}
24 changes: 20 additions & 4 deletions twine/generate/triggers.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@ spindle_trigger_apply(SPINDLEENTRY *entry)
SQL_STATEMENT *rs;
int flags;
char *id;
char nextactivationstr[32];
time_t now;
struct tm tm;

if(!entry->generate->db)
{
return 0;
}
rs = sql_queryf(entry->generate->db, "SELECT \"id\", \"flags\", \"triggerid\" FROM \"triggers\" WHERE \"triggerid\" = %Q AND \"triggerid\" <> \"id\"", entry->id);
rs = sql_queryf(entry->generate->db, "SELECT \"id\", \"flags\", \"triggerid\" FROM \"triggers\" WHERE \"triggerid\" = %Q AND \"triggerid\" <> \"id\" AND \"earliest_activation\" < NOW()", entry->id);
if(!rs)
{
return -1;
Expand All @@ -93,7 +96,7 @@ spindle_trigger_apply(SPINDLEENTRY *entry)
// Get the flags to apply
flags = (int) sql_stmt_long(rs, 1);

/* Trigger updates that have this entry's flag in scope */
// Trigger updates that have this entry's flag in scope
if (entry->flags & flags)
{
// Do a logical OR if there is already a trigger scheduled (status = DIRTY and flags <> 0)
Expand All @@ -105,6 +108,13 @@ spindle_trigger_apply(SPINDLEENTRY *entry)
// Set the target as DIRTY
sql_executef(entry->generate->db, "UPDATE \"state\" SET \"status\" = %Q WHERE \"id\" = %Q", "DIRTY", id);
}

// Set the earliest activation date some time in the future
now = time(NULL);
now += 10; // TODO As for the next fetch in Anansi, this should be made configurable.
gmtime_r(&now, &tm);
strftime(nextactivationstr, 32, "%Y-%m-%d %H:%M:%S", &tm);
sql_executef(entry->generate->db, "UPDATE \"triggers\" SET \"earliest_activation\" = %Q WHERE \"triggerid\" = %Q AND \"earliest_activation\" < NOW()", nextactivationstr, id);
}

sql_stmt_destroy(rs);
Expand Down Expand Up @@ -153,11 +163,17 @@ int
spindle_triggers_index(SQL *sql, const char *id, SPINDLEENTRY *data)
{
size_t c;
char nextactivationstr[32];
time_t now;
struct tm tm;

for(c = 0; c < data->ntriggers; c++)
{
if(sql_executef(sql, "INSERT INTO \"triggers\" (\"id\", \"uri\", \"flags\", \"triggerid\""") VALUES (%Q, %Q, '%d', %Q)",
id, data->triggers[c].uri, data->triggers[c].kind, data->triggers[c].id))
now = time(NULL);
gmtime_r(&now, &tm);
strftime(nextactivationstr, 32, "%Y-%m-%d %H:%M:%S", &tm);
if(sql_executef(sql, "INSERT INTO \"triggers\" (\"id\", \"uri\", \"flags\", \"triggerid\", \"earliest_activation\""") VALUES (%Q, %Q, '%d', %Q, %Q)",
id, data->triggers[c].uri, data->triggers[c].kind, data->triggers[c].id, nextactivationstr))
{
return -1;
}
Expand Down

0 comments on commit 4b55abf

Please sign in to comment.