-
Notifications
You must be signed in to change notification settings - Fork 580
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* support cron scheduling
- Loading branch information
Showing
12 changed files
with
170 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
select | ||
cron.schedule( | ||
'invoke-function-every-half-minute', | ||
'30 seconds', | ||
$$ | ||
select | ||
net.http_post( | ||
url:='https://{DIGGER_HOSTNAME}/_internal/process_drift', | ||
headers:=jsonb_build_object('Content-Type','application/json', 'Authorization', 'Bearer ' || 'abc123'), | ||
body:=jsonb_build_object('time', now() ), | ||
timeout_milliseconds:=5000 | ||
) as request_id; | ||
$$ | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
select | ||
cron.schedule( | ||
'invoke-function-every-half-minute', | ||
'30 seconds', | ||
$$ | ||
select | ||
net.http_post( | ||
url:='https://{DIGGER_HOSTNAME}/_internal/process_runs_queue', | ||
headers:='{"Content-Type": "application/json", "Authorization": "Bearer abc123"}'::jsonb, | ||
body:='{}'::jsonb | ||
) as request_id; | ||
$$ | ||
); | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"github.com/robfig/cron/v3" | ||
"time" | ||
) | ||
|
||
func MatchesCrontab(cronString string, timestamp time.Time) (bool, error) { | ||
// Parse the crontab string | ||
schedule, err := cron.ParseStandard(cronString) | ||
if err != nil { | ||
return false, fmt.Errorf("failed to parse crontab string: %w", err) | ||
} | ||
|
||
// Round down the timestamp to the nearest minute | ||
roundedTime := timestamp.Truncate(time.Minute) | ||
|
||
// Check if the rounded time matches the schedule | ||
nextTime := schedule.Next(roundedTime.Add(-time.Minute)) | ||
return nextTime.Equal(roundedTime), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestCrontTabMatching(t *testing.T) { | ||
cronString := "*/15 * * * *" // Every 15 minutes | ||
timestamp := time.Date(2023, 5, 1, 12, 30, 30, 0, time.UTC) | ||
|
||
matches, err := MatchesCrontab(cronString, timestamp) | ||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
return | ||
} | ||
assert.True(t, matches) | ||
|
||
cronString = "*/15 * * * *" // Every 15 minutes | ||
timestamp = time.Date(2022, 5, 1, 12, 12, 30, 0, time.UTC) | ||
|
||
matches, err = MatchesCrontab(cronString, timestamp) | ||
if err != nil { | ||
fmt.Printf("Error: %v\n", err) | ||
return | ||
} | ||
assert.False(t, matches) | ||
|
||
} |