From bf79b04018ed33f9a358e2c4a77ad7c13548487d Mon Sep 17 00:00:00 2001 From: Varun Rajput Date: Mon, 2 Dec 2024 15:12:34 +0530 Subject: [PATCH 1/4] docs: add scheduled job execution with runAt field Add documentation for scheduling future job execution using the runAt field. Reorganize job execution docs to clearly distinguish between immediate and scheduled runs. Note polling interval for scheduled jobs. --- Guide/jobs.markdown | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Guide/jobs.markdown b/Guide/jobs.markdown index 511ebc595..7c43a4fdf 100644 --- a/Guide/jobs.markdown +++ b/Guide/jobs.markdown @@ -44,11 +44,23 @@ instance Job EmailCustomersJob where ### Running the job -IHP watches the job table in the database for any new records and automatically runs the job asynchronously when a new job is added. So to run a job, simply create a new record: +IHP watches the job table in the database for any new records and automatically runs the job asynchronously when a new job is added. There are two ways to run a job: + +1. Run immediately: ```haskell newRecord @EmailCustomersJob |> create ``` +2. Schedule for future execution: + +```haskell +now <- getCurrentTime +newRecord @EmailCustomersJob + |> set #runAt (addUTCTime 86400 now) -- Schedule 24 hours in the future + |> create +``` + +The `runAt` field determines when the job should be executed. If not set, the job runs immediately. When set, IHP polls for scheduled jobs approximately every minute and executes any jobs whose runAt time has passed. This can be done in a controller action or in a script as will be shown below. From 6227076a3efa26c0d093b55556500b493a74b885 Mon Sep 17 00:00:00 2001 From: Varun Rajput Date: Mon, 2 Dec 2024 11:02:33 -0500 Subject: [PATCH 2/4] Update Guide/jobs.markdown Co-authored-by: Amitai Burstein --- Guide/jobs.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Guide/jobs.markdown b/Guide/jobs.markdown index 7c43a4fdf..a38ce98a9 100644 --- a/Guide/jobs.markdown +++ b/Guide/jobs.markdown @@ -46,7 +46,7 @@ instance Job EmailCustomersJob where IHP watches the job table in the database for any new records and automatically runs the job asynchronously when a new job is added. There are two ways to run a job: -1. Run immediately: +1. Run immediately (as soon as a job worker is available): ```haskell newRecord @EmailCustomersJob |> create From 9b264453bceee5a955ff590a0007ca163c140833 Mon Sep 17 00:00:00 2001 From: Varun Rajput Date: Mon, 2 Dec 2024 11:02:44 -0500 Subject: [PATCH 3/4] Update Guide/jobs.markdown Co-authored-by: Amitai Burstein --- Guide/jobs.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Guide/jobs.markdown b/Guide/jobs.markdown index a38ce98a9..7e3543bf9 100644 --- a/Guide/jobs.markdown +++ b/Guide/jobs.markdown @@ -60,7 +60,7 @@ newRecord @EmailCustomersJob |> create ``` -The `runAt` field determines when the job should be executed. If not set, the job runs immediately. When set, IHP polls for scheduled jobs approximately every minute and executes any jobs whose runAt time has passed. +The `runAt` field determines when the job should be executed. If not set, the job runs immediately. When set, IHP polls for scheduled jobs approximately every minute and executes any jobs whose `runAt` time has passed. This can be done in a controller action or in a script as will be shown below. From e49dc041a90ec41000acb1289bbcf58057ea0afc Mon Sep 17 00:00:00 2001 From: Varun Rajput Date: Mon, 2 Dec 2024 21:36:34 +0530 Subject: [PATCH 4/4] Update Guide/jobs.markdown --- Guide/jobs.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Guide/jobs.markdown b/Guide/jobs.markdown index 7e3543bf9..e67009311 100644 --- a/Guide/jobs.markdown +++ b/Guide/jobs.markdown @@ -54,9 +54,11 @@ newRecord @EmailCustomersJob |> create 2. Schedule for future execution: ```haskell +import Data.Time.Clock (addUTCTime, getCurrentTime, nominalDay) + now <- getCurrentTime newRecord @EmailCustomersJob - |> set #runAt (addUTCTime 86400 now) -- Schedule 24 hours in the future + |> set #runAt (addUTCTime nominalDay now) -- Schedule 24 hours in the future |> create ```