-
Notifications
You must be signed in to change notification settings - Fork 3
Logic
Legend T = team J = task (job)
A note about errors: errors must be judiciously caught within "work" functions. If you don't catch these errors, they WILL blow up your application. This is as a result of problems with errors within nested functions in the callback loop.
Items that potentially create memory leak or add data to store that needs removal are numbered. + indicates creation of instance/item, ~ indicates read, and - indicates removal.
When a team is created:
- Get team ID.
- (+T01): Add team to team set (NOT CURRENTLY IMPLEMENTED).
- (+T02): Add serialized team (NOT CURRENTLY IMPLEMENTED).
- Begin heartbeat.
When a team is added to a factory:
- Team is created.
- (+T03): Team is added to Factory.teams
- (+T04): Team "available" listener kicks in
- Team is started.
When a team checks in:
How a team is monitored:
When a team is destroyed:
When a task is saved:
0. Task is assumed to have ID assigned (from create task or otherwise)
- (~J04): If uid (unique id) present, ensure task with same uid doesn't exist.
- (+J01): Add serialized task to tasks queue: [factory]:tasks:[taskId]
- (+J02): Add task ID to waiting queue sorted set: :tasks:[type]:waiting
- (+J03): Push task ID to type queue :tasks:[type]
- (+J04): If uid is present, add to :tasks[type]:uids hash
Result: [J01, J02, J03, (J04)]
When task is ready:
This occurs via BLPOP
- (-J03): Pops task ID from type queue
- (-J02): Pops task ID from waiting queue
- Delegate task to team.
Result: [J01, (J04)]
When team asked to delegate:
- Create a worker.
- (+J05) Add worker to worker queue.
- (+J06) Listen to worker.
Result: [J01, (J04), J05, J06]
When worker asked to work:
- Task is retrieved.
- (+J07) Task is activated.
- (+J08) Set up local task listeners.
- (+J09) Set up global task listeners
- Start work.
Result: [J01, (J04), J05, J06, J07, J08, J09]
When a task terminates
Termination may represent success, failure, delays or retries. This logic occurs in Worker.work:ensure
- (-J06) Stop listening to worker. √
- (-J05) Remove worker from worker queue. √
- (-J08) Remove local task listeners. √
When a task succeeds
- Announce "success"
- (-J07) Remove from active.
- Finalize
- Terminate
Result: [J01, J09]
When a task fails without more attempts:
- Announce "failure"
- (-J07) Remove from active.
- Finalize
- Terminate
Result: [J01, J09]
When a task fails with more attempts
- Announce "retry"
- (-J07) Remove from active.
- (-J09): Remove global listeners
- (+J02): Add task ID to waiting queue sorted set: :tasks:[type]:waiting
- (+J03): Push task ID to type queue :tasks:[type]
- Terminate
Result: [J01, J02, J03, (J04)]
When a task is finalized
- Update metadata
- (-J04) Remove from uid
- Set expiration if necessary
When a task is removed
(-J09) Remove global task listeners.
(-J01) Remove task
How a task is monitored:
When a task emits an event:
When a task broadcasts a message:
When a listener is added to a task:
- 'on' should take an additional argument that specifies whether to be local or global.
- If listener is added within "work", events are added to localEvents only. This can be done in one of two ways: A. Create a preparation function that temporarily replaces task "on" with wrapper that includes boolean. Restore at end (fairly destructive) B. In addition to third argument, look at caller (arguments.callee.caller.toString())