
= Design

== Job activity

[plantuml, job_activity_puml, svg]
....
title __Job activity__
control     Timer       as timer
actor       Scheduler   as scheduler
database    MetadataStorage  as storage
boundary    JobActivationQueue as jobQueue
boundary    NotifyQueue as notify
actor       Runner as runner
entity      JobCode as jobStorage
actor       NotifyRunner as notifyRunner
database    NotifyStorage as notifyStorage
entity      NotifyCode as notifyCode

note over scheduler
Schedules tasks
end note

/ note over storage
Stores job metadata
end note

/ note over jobQueue
A queue that receives the job GUID
for jobs that needs to be run
end note

/ note over notify
A queue that receives the job GUID
and the job state it needs to be
notified of
end note


/ note over runner
Runs the job code
end note

/ note over jobStorage
Converts a job GUID to
runnable code
end note

/ note over notifyRunner
Runs notification code
end note

/ note over notifyStorage
Stores notification metadata
end note

/ note over notifyCode
Converts a notification GUID
to runnable code
end note

== Scheduling ==
timer -> scheduler      : Every 1 sec
activate scheduler
scheduler -> storage    : Get job guid list
storage -> scheduler
scheduler -> storage    : For each guid get metadata
storage -> scheduler
scheduler -> scheduler  : Filter next tick
scheduler -> storage    : Next tick, last tick\nfor scheduled guids
scheduler -> jobQueue   : GUIDs of next job
scheduler -> notify     : GUID + SCHEDULED
deactivate scheduler

== Run the job ==
jobQueue -> runner      : GUID of scheduled GUID
activate runner
runner -> jobStorage    : Get runnable code\nvs GUID
jobStorage -> runner
runner -> notify        : GUID + STARTED
runner -> runner        : Run job
runner -> notify        : GUID + DONE
deactivate runner

== Notification ==
notify -> notifyRunner          : GUID + <JOB STATE>
activate notifyRunner
notifyRunner -> notifyStorage   : Get notify GUIDs for\njob GUID + job state
notifyStorage -> notifyRunner
notifyRunner -> notifyCode    : Get notify code to run\nusing Notify GUID
notifyCode -> notifyRunner
notifyRunner -> notifyRunner    : Run notification code
deactivate notifyRunner

....

== Create job

[plantuml, job_creation_puml, svg]
....
title __Create job__
actor       CreateJob as createJob
boundary    JobCreateQueue as notify
boundary    JobCreatedQueue as done
actor       JobCreator  as jobCreator
database    MetadataStorage  as storage
entity      JobCode as runStorage

note over createJob
Starts the create job
process
end note

/ note over notify
Queue that produces
job metadata
end note

/ note over jobCreator
Handles the creation
message from the queue
end note

/ note over storage
Stores job metadata
end note

/ note over runStorage
    Converts a job GUID
    to runnable code
end note

/ note over done
    Queue that produces
    job metadata when a job
    is done being made
end note

createJob -> notify : New Job
activate createJob
notify -> runStorage
notify -> jobCreator
activate jobCreator
jobCreator -> jobCreator    : Generate next tick
jobCreator -> storage       : Save metadata
jobCreator -> storage       : Get list of GUIDs
storage -> jobCreator
jobCreator -> storage       : Update list of GUIDs
jobCreator -> done          : Job GUID
deactivate jobCreator
done -> createJob
deactivate createJob

....


== Create notification

[plantuml, create_notification_puml, svg]
....
title __Create notification__
actor       CreateNotification as create
boundary    NotifyCreateQueue as notify
boundary    NotifyCreatedQueue as notified
actor       NotificationCreator  as creator
database    NotifyStorage  as notifyStorage
entity      NotifyCode as notifyCode

create -> notify : Job GUID +\nNotify GUID +\nJob State
activate create
notify -> notifyCode
notify -> creator
activate creator
creator -> notifyStorage    : Get notification data
notifyStorage -> creator
creator -> creator          : Update job metadata\nwith new data
creator -> notifyStorage    : Save
creator -> notified         : Notify GUID + Job State
deactivate creator
notified -> create
deactivate create


....

== Delete notification

[plantuml, delete_notification_puml, svg]
....
title __Delete notification__

actor       DeleteNotification as deleter
boundary    NotifyDeleteQueue as notify
boundary    NotifyDeletedQueue as notified
actor       NotificationDeleter as delete
database    NotifyStorage as storage
entity      NotifyCode as notifyCode

deleter -> notify   : Notify GUID + Job State
notify -> delete
activate delete
delete -> storage   : Get notification\nmetadata
delete -> delete    : Update metadata\nwith new data
delete -> storage   : Save metadata
delete -> notified  : Notify GUID + Job State
notified -> notifyCode
notified -> deleter

deactivate delete
....

== Delete job

[plantuml, job_deletion_puml, svg]
....
title __Delete job__

actor       DeleteJob as deleter
boundary    DeleteJobQueue as deleteQueue
boundary    DeletedJobQueue as deletedQueue
actor       Deleter as delete
database    MetadataStorage  as storage
entity      JobCode as jobStorage
actor       NotifyDeleter as notifyDelete
database    NotifyStorage as notifyStorage
boundary    NotifyDeletedQueue as notifyDeleted
entity      NotifyCode as notifyCode

deleter -> deleteQueue  : Job GUID
activate deleteQueue
deleteQueue -> delete
activate delete

group Delete Job
    delete -> storage : Delete metadata
    storage -> delete
    delete -> deletedQueue : GUID
    deletedQueue -> jobStorage
    deactivate delete
    deletedQueue -> deleter
end

deleteQueue -> notifyDelete
deactivate deleteQueue
group Delete notifications
    activate notifyDelete
    notifyDelete -> notifyStorage : Get notifications for Job ID
    notifyStorage -> notifyDelete
    notifyDelete -> notifyStorage : For each,\ndelete notification
    notifyDelete -> notifyDeleted : For each\ndeleted notification,\nNotification GUID +\nJob GUID + State
    notifyDeleted -> notifyCode
    deactivate notifyDelete
end


....
