Skip to content

Commit

Permalink
Explicitly encode job data
Browse files Browse the repository at this point in the history
  • Loading branch information
infomiho committed Nov 22, 2024
1 parent 4a88a94 commit 7347058
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
20 changes: 19 additions & 1 deletion waspc/data/Generator/templates/sdk/wasp/server/jobs/_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,28 @@ const entities = {
// PUBLIC API
export type {= typeName =}<Input extends JSONObject, Output extends JSONValue | void> = JobFn<Input, Output, typeof entities>

{=# jobSchedule.isDefined =}
const jobSchedule = {
cron: "{= jobSchedule.cron =}",
{=# jobSchedule.args.isDefined =}
args: {=& jobSchedule.args.json =},
{=/ jobSchedule.args.isDefined =}
{=# jobSchedule.options.isDefined =}
options: {=& jobSchedule.options.json =},
{=/ jobSchedule.options.isDefined =}
{=^ jobSchedule.options.isDefined =}
options: {},
{=/ jobSchedule.options.isDefined =}
}
{=/ jobSchedule.isDefined =}
{=^ jobSchedule.isDefined =}
const jobSchedule = null
{=/ jobSchedule.isDefined =}

// PUBLIC API
export const {= jobName =} = createJobDefinition({
jobName: '{= jobName =}',
defaultJobOptions: {=& jobPerformOptions =},
jobSchedule: {=& jobSchedule =},
jobSchedule,
entities,
})
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export const PG_BOSS_EXECUTOR_NAME = Symbol('PgBoss')

type JobSchedule = {
cron: Parameters<PgBoss['schedule']>[1]
args: Parameters<PgBoss['schedule']>[2]
options: Parameters<PgBoss['schedule']>[3]
args?: NonNullable<Parameters<PgBoss['schedule']>[2]>
}

// PRIVATE API
Expand Down
30 changes: 22 additions & 8 deletions waspc/src/Wasp/Generator/SdkGenerator/Server/JobGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,34 @@ genJob (jobName, job) =
-- NOTE: You cannot directly input an Aeson.object for Mustache to substitute.
-- This is why we must get a text representation of the object, either by
-- `Aeson.Text.encodeToLazyText` on an Aeson.Object, or `show` on an AS.JSON.
"jobSchedule" .= Aeson.Text.encodeToLazyText (fromMaybe Aeson.Null maybeJobSchedule),
"jobSchedule" .= getJobScheduleData (J.schedule job),
"jobPerformOptions" .= show (fromMaybe AS.JSON.emptyObject maybeJobPerformOptions)
]

jobExecutorImportPath = getJobExecutorImportPath (J.executor job)

maybeJobPerformOptions = J.performExecutorOptionsJson job
jobScheduleTmplData s =
object
[ "cron" .= J.cron s,
"args" .= J.args s,
"options" .= fromMaybe AS.JSON.emptyObject (J.scheduleExecutorOptionsJson job)
]
maybeJobSchedule = jobScheduleTmplData <$> J.schedule job

getJobScheduleData =
maybe
(object ["isDefined" .= False])
( \schedule ->
object
[ "isDefined" .= True,
"cron" .= J.cron schedule,
"args" .= getJobScheduleArgs (J.args schedule),
"options" .= getJobSchduleOptions (J.scheduleExecutorOptionsJson job)
]
)
getJobScheduleArgs =
maybe
(object ["isDefined" .= False])
(\args -> object ["isDefined" .= True, "json" .= Aeson.Text.encodeToLazyText args])

getJobSchduleOptions =
maybe
(object ["isDefined" .= False])
(\options -> object ["isDefined" .= True, "json" .= Aeson.Text.encodeToLazyText options])

-- | We are importing relevant functions and types per executor e.g. JobFn or registerJob,
-- this functions maps the executor to the import path from SDK.
Expand Down

0 comments on commit 7347058

Please sign in to comment.