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]/list
endpoint 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.erl
Added a new
list/3
function that:hb_name:all/0
{<<"[email protected]">>, TaskID}
patternerlang:process_info/2
2. Enhanced Worker Processes with Metadata
Once Worker (
once_worker/4
)CreatedAt
timestamp parameter (in milliseconds)type
,path
,created_at
Every Worker Loop (
every_worker_loop/6
)CreatedAt
timestamp (milliseconds) andIntervalString
parameterswait_with_info/1
for responsive info handling during sleeptype
,path
,interval
,interval_ms
,created_at
3. Info Request Handling
New Functions:
wait_with_info/1
: Allowsevery
workers to respond to info requests while sleeping, using monotonic time for accuracy4. Updated Module Exports and Documentation
list
to module exportsinfo/0
to includelist
in exportsinfo/3
to document the list endpoint:<<"list">> => <<"List all active cron tasks">>
5. Comprehensive Test Coverage
Added
list_tasks_test/0
that:every
task (sinceonce
tasks execute immediately)API Response Format
Before
The list endpoint did not exist.
After
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/1
function 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:
once
andevery
workersRun tests with:
Key Implementation Decisions
Timestamp Consistency
hb:now()
and all timing operationsUnified Worker Behavior
once
andevery
workers 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.