Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ $(HARDWARE_I2C_O): src/hardware/hardware_i2c.c src/hardware/hardware_i2c.h
$(HARDWARE_CAMERA_O): src/hardware/hardware_camera.c src/hardware/hardware_camera.h src/hardware/board_detect.h
$(CC) $(CFLAGS) $(INC) -c -o $@ src/hardware/hardware_camera.c

$(CRON_O): src/tools/cron.c src/tools/cron.h src/crypto/crypto.h src/core/agent.h src/core/memory.h src/channels/channel.h src/core/config.h
$(CRON_O): src/tools/cron.c src/tools/cron.h src/crypto/crypto.h src/core/agent.h src/core/memory.h src/channels/channel.h src/channels/heartbeat.h src/core/config.h
$(CC) $(CFLAGS) $(INC) -c -o $@ src/tools/cron.c

$(ASAP_INVOKE_O): src/tools/asap_invoke.c src/tools/asap_invoke.h src/tools/tool.h src/asap/envelope.h src/asap/client.h src/asap/registry.h src/asap/ulid.h src/asap/asap_version.h src/core/config.h vendor/cJSON/cJSON.h
Expand Down Expand Up @@ -642,9 +642,9 @@ test_web_search: tests/test_web_search.c $(WEBSEARCH_O) $(CONFIG_O) $(TOML_O) $(
$(CC) $(CFLAGS) $(LDFLAGS) $(INC) -o $(BINDIR)/$@ tests/test_web_search.c $(WEBSEARCH_O) $(CONFIG_O) $(TOML_O) $(CJSON_O) $(LDLIBS)
$(DSYM_SCRIPT)

test_cron: tests/test_cron.c $(CRON_O) $(CRYPTO_LINK) $(MEMORY_O) $(SQLITE3_O) $(CHANNEL_COMMON_O) $(CJSON_O) $(AGENT_O) $(SKILL_O) $(CONFIG_O) $(TOML_O) $(PROVIDER_COMMON_O)
test_cron: tests/test_cron.c $(CRON_O) $(CRYPTO_LINK) $(MEMORY_O) $(SQLITE3_O) $(CHANNEL_COMMON_O) $(CHANNEL_HEARTBEAT_O) $(CHANNEL_CLI_O) $(CJSON_O) $(AGENT_O) $(SKILL_O) $(CONFIG_O) $(TOML_O) $(PROVIDER_COMMON_O)
@mkdir -p $(BINDIR)
$(CC) $(CFLAGS) $(LDFLAGS) $(INC) -o $(BINDIR)/$@ tests/test_cron.c $(CRON_O) $(CRYPTO_LINK) $(MEMORY_O) $(SQLITE3_O) $(CHANNEL_COMMON_O) $(CJSON_O) $(AGENT_O) $(SKILL_O) $(CONFIG_O) $(TOML_O) $(PROVIDER_COMMON_O) $(LDLIBS) -pthread
$(CC) $(CFLAGS) $(LDFLAGS) $(INC) -o $(BINDIR)/$@ $^ $(LDLIBS) -pthread
$(DSYM_SCRIPT)

test_ws: tests/test_ws.c $(WS_TEST_O)
Expand All @@ -657,9 +657,9 @@ test_context: tests/test_context.c $(CONTEXT_TEST_OBJS) $(CONFIG_O) $(TOML_O) $(
$(CC) $(CFLAGS) $(LDFLAGS) $(INC) -DSHELLCLAW_CONTEXT_TEST -o $(BINDIR)/$@ tests/test_context.c $(CONTEXT_TEST_OBJS) $(CONFIG_O) $(TOML_O) $(CJSON_O) $(LDLIBS) -pthread
$(DSYM_SCRIPT)

test_dispatch: tests/test_dispatch.c $(DISPATCH_O) $(BOOTSTRAP_DISPATCH_STUB_O) $(AGENT_O) $(ROUTER_O) $(STUB_O) $(ANTHROPIC_O) $(OPENAI_COMPAT_O) $(OPENAI_O) $(LOCAL_O) $(PROVIDER_COMMON_O) $(MEMORY_O) $(SQLITE3_O) $(SKILL_O) $(CONFIG_O) $(TOML_O) $(CJSON_O) $(CRON_O) $(CRYPTO_LINK) $(CHANNEL_COMMON_O)
test_dispatch: tests/test_dispatch.c $(DISPATCH_O) $(BOOTSTRAP_DISPATCH_STUB_O) $(AGENT_O) $(ROUTER_O) $(STUB_O) $(ANTHROPIC_O) $(OPENAI_COMPAT_O) $(OPENAI_O) $(LOCAL_O) $(PROVIDER_COMMON_O) $(MEMORY_O) $(SQLITE3_O) $(SKILL_O) $(CONFIG_O) $(TOML_O) $(CJSON_O) $(CRON_O) $(CRYPTO_LINK) $(CHANNEL_COMMON_O) $(CHANNEL_HEARTBEAT_O) $(CHANNEL_CLI_O)
@mkdir -p $(BINDIR)
$(CC) $(CFLAGS) $(LDFLAGS) $(INC) -o $(BINDIR)/$@ tests/test_dispatch.c $(DISPATCH_O) $(BOOTSTRAP_DISPATCH_STUB_O) $(AGENT_O) $(ROUTER_O) $(STUB_O) $(ANTHROPIC_O) $(OPENAI_COMPAT_O) $(OPENAI_O) $(LOCAL_O) $(PROVIDER_COMMON_O) $(MEMORY_O) $(SQLITE3_O) $(SKILL_O) $(CONFIG_O) $(TOML_O) $(CJSON_O) $(CRON_O) $(CRYPTO_LINK) $(CHANNEL_COMMON_O) $(LDLIBS) -pthread
$(CC) $(CFLAGS) $(LDFLAGS) $(INC) -o $(BINDIR)/$@ $^ $(LDLIBS) -pthread
$(DSYM_SCRIPT)

RELOAD_TEST_OBJS := $(RELOAD_O) $(BOOTSTRAP_DISPATCH_STUB_O) $(TOOL_RELOAD_STUB_O) $(RELOAD_CHANNEL_STUB_O) $(HTTP_RELOAD_STUB_O) $(CONFIG_O) $(TOML_O) \
Expand Down
49 changes: 37 additions & 12 deletions src/channels/heartbeat.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,50 @@ static int heartbeat_poll(channel_incoming_msg_t *out, int timeout_ms)
return 1;
}

static int heartbeat_log_send(const char *recipient, const char *text,
const channel_attachment_t *attachments, size_t att_count)
{
(void)recipient;
(void)attachments;
(void)att_count;
if (!text) return -1;
fprintf(stderr, "[heartbeat] %s\n", text);
return 0;
}

static const channel_t heartbeat_log_channel = {.send = heartbeat_log_send};

static const char *heartbeat_destination(const config_t *cfg)
{
if (!cfg) return "log";
const char *name = config_heartbeat_default_channel(cfg);
return name && name[0] ? name : "cli";
}

const channel_t *heartbeat_local_delivery(void)
{
if (!config_heartbeat_enabled(g_heartbeat_cfg))
return NULL;
const char *target = heartbeat_destination(g_heartbeat_cfg);
if (strcmp(target, "log") == 0)
return &heartbeat_log_channel;
if (strcmp(target, "cli") == 0)
return channel_get_by_name("cli");
return NULL;
}

static int heartbeat_send(const char *recipient, const char *text,
const channel_attachment_t *attachments, size_t att_count)
const channel_attachment_t *attachments, size_t att_count)
{
(void)recipient;
(void)attachments;
(void)att_count;
if (!text) return -1;
if (!g_heartbeat_cfg) {
fprintf(stderr, "[heartbeat] %s\n", text);
return 0;
}
const char *ch = config_heartbeat_default_channel(g_heartbeat_cfg);
if (ch && strcmp(ch, "log") == 0) {
fprintf(stderr, "[heartbeat] %s\n", text);
return 0;
}
const char *target = (ch && ch[0]) ? ch : "cli";
const char *target = heartbeat_destination(g_heartbeat_cfg);
if (strcmp(target, "log") == 0)
return heartbeat_log_send("default", text, NULL, 0);
const channel_t *dest = channel_get_by_name(target);
if (!dest || !dest->send) return -1;
if (!dest || dest == channel_heartbeat_get() || !dest->send) return -1;
return dest->send("default", text, NULL, 0);
}

Expand Down
5 changes: 5 additions & 0 deletions src/channels/heartbeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ typedef struct channel channel_t;
/** Get the heartbeat channel (poll returns tick when interval elapsed, send routes to default or log). */
const channel_t *channel_heartbeat_get(void);

/** Return the configured log or CLI delivery channel for cron, or NULL.
* Call with the agent lock held to serialize access with configuration reload.
*/
const channel_t *heartbeat_local_delivery(void);

/** After config reload without channel re-init (SIGHUP); updates live pointers only. */
void heartbeat_set_live_config(const config_t *cfg);

Expand Down
44 changes: 37 additions & 7 deletions src/core/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ static int fill_cron_job_row(sqlite3_stmt *stmt, cron_job_row_t *out)
copy_str_bounded(out->id, sizeof(out->id), (const char *)sqlite3_column_text(stmt, 0));
out->schedule = dup_sqlite_text(stmt, 1);
out->message = dup_sqlite_text(stmt, 2);
out->channel_truncated = (size_t)sqlite3_column_bytes(stmt, 3) >= sizeof(out->channel);
out->recipient_truncated = (size_t)sqlite3_column_bytes(stmt, 4) >= sizeof(out->recipient);
copy_str_bounded(out->channel, sizeof(out->channel), (const char *)sqlite3_column_text(stmt, 3));
copy_str_bounded(out->recipient, sizeof(out->recipient), (const char *)sqlite3_column_text(stmt, 4));
out->next_run = sqlite3_column_int64(stmt, 5);
Expand Down Expand Up @@ -449,19 +451,32 @@ int cron_job_delete(const char *id)
return ret;
}

int cron_job_toggle(const char *id)
static int cron_job_set_enabled(const char *id, int disable)
{
if (!g_db || !id) return -1;
memory_sqlite_entry_probe();
const char *sql = "UPDATE cron_jobs SET enabled = 1 - enabled WHERE id = ?1";
const char *sql = disable
? "UPDATE cron_jobs SET enabled = 0 WHERE id = ?1 AND enabled = 1"
: "UPDATE cron_jobs SET enabled = 1 - enabled WHERE id = ?1";
sqlite3_stmt *stmt = NULL;
if (sqlite3_prepare_v2(g_db, sql, -1, &stmt, NULL) != SQLITE_OK) return -1;
sqlite3_bind_text(stmt, 1, id, -1, SQLITE_TRANSIENT);
int ret = (sqlite3_step(stmt) == SQLITE_DONE && sqlite3_changes(g_db) > 0) ? 0 : -1;
int ret = (sqlite3_step(stmt) == SQLITE_DONE &&
(disable || sqlite3_changes(g_db) > 0)) ? 0 : -1;
sqlite3_finalize(stmt);
return ret;
}

int cron_job_toggle(const char *id)
{
return cron_job_set_enabled(id, 0);
}

int cron_job_disable(const char *id)
{
return cron_job_set_enabled(id, 1);
}

int cron_job_update_next_run(const char *id, long long next_run)
{
if (!g_db || !id) return -1;
Expand Down Expand Up @@ -497,22 +512,37 @@ int cron_job_list(cron_job_row_t *out, int max_count)
return count;
}

int cron_job_list_due(long long now, cron_due_key_t *out, int max_count)
int cron_job_list_due(long long now, const cron_due_key_t *after,
cron_due_key_t *out, int max_count)
{
const char *sql;
const char *sql = "SELECT id, next_run, channel, recipient FROM cron_jobs "
"WHERE next_run <= ?1 AND enabled = 1 "
"AND (?2 IS NULL OR next_run > ?2 OR (next_run = ?2 AND id > ?3)) "
"ORDER BY next_run ASC, id ASC LIMIT ?4";
sqlite3_stmt *stmt = NULL;
int count = 0;

if (!g_db || !out || max_count <= 0) return -1;
memory_sqlite_entry_probe();
sql = "SELECT id, next_run FROM cron_jobs WHERE next_run <= ?1 AND enabled = 1 "
"ORDER BY next_run ASC, id ASC";
if (sqlite3_prepare_v2(g_db, sql, -1, &stmt, NULL) != SQLITE_OK) return -1;
sqlite3_bind_int64(stmt, 1, now);
if (after) {
sqlite3_bind_int64(stmt, 2, after->next_run);
sqlite3_bind_text(stmt, 3, after->id, -1, SQLITE_TRANSIENT);
}
sqlite3_bind_int(stmt, 4, max_count);
while (count < max_count && sqlite3_step(stmt) == SQLITE_ROW) {
copy_str_bounded(out[count].id, sizeof(out[count].id),
(const char *)sqlite3_column_text(stmt, 0));
out[count].next_run = sqlite3_column_int64(stmt, 1);
out[count].channel_truncated =
(size_t)sqlite3_column_bytes(stmt, 2) >= sizeof(out[count].channel);
out[count].recipient_truncated =
(size_t)sqlite3_column_bytes(stmt, 3) >= sizeof(out[count].recipient);
copy_str_bounded(out[count].channel, sizeof(out[count].channel),
(const char *)sqlite3_column_text(stmt, 2));
copy_str_bounded(out[count].recipient, sizeof(out[count].recipient),
(const char *)sqlite3_column_text(stmt, 3));
count++;
}
sqlite3_finalize(stmt);
Expand Down
15 changes: 13 additions & 2 deletions src/core/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ typedef struct cron_job_row {
char *message;
char channel[64];
char recipient[64];
int channel_truncated;
int recipient_truncated;
long long next_run;
int enabled;
} cron_job_row_t;
Expand Down Expand Up @@ -181,6 +183,9 @@ int cron_job_delete(const char *id);
*/
int cron_job_toggle(const char *id);

/** Disable the stored job. Returns 0 if the update succeeds. */
int cron_job_disable(const char *id);

/**
* Update next_run for a job.
*
Expand All @@ -191,21 +196,27 @@ int cron_job_update_next_run(const char *id, long long next_run);
/**
* Key of a due cron job. No heap fields. Example:
* cron_due_key_t keys[8];
* int n = cron_job_list_due(now, keys, 8);
* int n = cron_job_list_due(now, NULL, keys, 8);
*/
typedef struct cron_due_key {
char id[128];
long long next_run;
char channel[sizeof(((cron_job_row_t *)0)->channel)];
char recipient[sizeof(((cron_job_row_t *)0)->recipient)];
int channel_truncated;
int recipient_truncated;
} cron_due_key_t;

/**
* List enabled jobs with next_run <= now, ordered by next_run then id.
*
* @param after Optional last key from the previous page.
* @param out Array to fill (caller-allocated).
* @param max_count Maximum keys to return.
* @return Number of keys written, or -1 on error.
*/
int cron_job_list_due(long long now, cron_due_key_t *out, int max_count);
int cron_job_list_due(long long now, const cron_due_key_t *after,
cron_due_key_t *out, int max_count);

/**
* List cron jobs into output array.
Expand Down
3 changes: 2 additions & 1 deletion src/gateway/routes.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,8 @@ static void handle_cron_create(const char *body, size_t body_len, char *buf, siz
agent_unlock();
cJSON_Delete(root);
if (ret != 0) {
json_error(buf, size, status, 400, "Failed to create job");
json_error(buf, size, status, 400, ret == CRON_INVALID_DESTINATION
? "Invalid cron destination." : "Failed to create job");
return;
}
cJSON *resp_obj = cJSON_CreateObject();
Expand Down
Loading
Loading