feat(cron): add list endpoint to show active cron tasks #399
+172
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Cron Device Task List Enhancement
Overview
This PR enhances the
[email protected]device to provide comprehensive task listing functionality with detailed metadata about active cron tasks. The/[email protected]/listendpoint now returns rich information about each scheduled task, including type, path, interval, and creation timestamp.Motivation
Previously, operators had limited visibility into active cron tasks on their HyperBEAM nodes. This enhancement addresses the need for better observability and debugging capabilities by providing detailed information about all scheduled tasks.
Changes Made
1. Added List Endpoint (
list/3)File:
src/dev_cron.erlAdded a new
list/3function that:hb_name:all/0{<<"[email protected]">>, TaskID}patternerlang:process_info/22. Enhanced Worker Processes with Metadata
Once Worker (
once_worker/4)CreatedAttimestamp parameter (in milliseconds)type,path,created_atEvery Worker Loop (
every_worker_loop/6)CreatedAttimestamp (milliseconds) andIntervalStringparameterswait_with_info/1for responsive info handling during sleeptype,path,interval,interval_ms,created_at3. Info Request Handling
New Functions:
wait_with_info/1: Allowseveryworkers to respond to info requests while sleeping, using monotonic time for accuracy4. Updated Module Exports and Documentation
listto module exportsinfo/0to includelistin exportsinfo/3to document the list endpoint:<<"list">> => <<"List all active cron tasks">>5. Comprehensive Test Coverage
Added
list_tasks_test/0that:everytask (sinceoncetasks execute immediately)API Response Format
Before
The list endpoint did not exist.
After
{ "status": 200, "body": [ { "task_id": "ks4Gebwlhafrv89iOAYpEtJkg9QgEiEC80RH0xr-A_o", "pid": "<0.1184.0>", "type": "every", "path": "/[email protected]/now", "interval": "30-seconds", "interval_ms": 30000, "created_at": 1735251600000 }, { "task_id": "bXjtHTVYw3CznxqpB6xBrJpBKvBX0-rPloG-Gffei9A", "pid": "<0.1185.0>", "type": "once", "path": "/[email protected]/update_state", "created_at": 1735251605000 } ] }Technical Implementation Details
Process Dictionary Approach
To ensure responsive metadata retrieval even when workers are busy executing tasks, metadata is stored in the process dictionary. This allows the list function to retrieve information without blocking, using
erlang:process_info(Pid, dictionary). This approach follows patterns used in other parts of the codebase likehb_ao.erl.Time Management
erlang:system_time(millisecond)for consistency with the codebasewait_with_info/1function useserlang:monotonic_time(millisecond)to accurately track elapsed time when handling info requestsGraceful Degradation
If metadata cannot be retrieved (process busy, not yet initialized, etc.), the response includes minimal information with
"type": "unknown"and"path": "unknown"to indicate the issue.Benefits
Testing
All existing tests pass, plus new test coverage for:
onceandeveryworkersRun tests with:
Key Implementation Decisions
Timestamp Consistency
hb:now()and all timing operationsUnified Worker Behavior
onceandeveryworkers store metadata identically in process dictionaryNon-blocking Design
erlang:process_info(Pid, dictionary)- instant and non-blockingMigration Notes
No migration required. The enhancement is fully backward compatible and adds new functionality without modifying existing behavior.