Description

Background Job/Worker Associates

Author

Kon Lovett

Usage

(require-extension job-worker)

Download

job-worker.egg

Requires

Documentation

NOTE: This is a work in progress.

Runs a background task (Associate) upon an event (Circumstance). An Associate is a work assignment - a composition of a Chore and a Laborer. A Circumstance is something that can detect a state change and return any facts about the state change.

The actual task is defined by a Chore, which needs to be performed not at all, once, some number of times, or always. The chore is handled by a Laborer. Via the associate a chore and a laborer can query each other for cooperative task completion.

Associates, Chores, and Laborers have an optional, fixed at construction time, number of arbitrary object slots available. As a convenience, when chore/laborer cooperation is not required, so-called 'pass-thru' versions can be constructed.

The 'pass-thru' chore doesn't have any extra state, and is performed by a 0-arity procedure. The 'pass-thru' laborer doesn't have any extra state, and just calls the associated chore's procedure.

Circumstance

procedure: (make-circumstance ID WAITER [DATA...])

Returns a fresh circumstance object.

ID The name of the circumstance. A symbol.
WAITER Event detection. A procedure. Should return an object representing any facts about the particular circumstance detected.
DATA Data for this circumstance. 0 or more objects. Reference is indexed from 0.
procedure: (circumstance? OBJECT)

Is this OBJECT an circumstance?

procedure: (circumstance-id CIRCUMSTANCE)

Returns the field value.

procedure: (circumstance-waiter CIRCUMSTANCE)

Returns the field value.

procedure: (circumstance-data-ref CIRCUMSTANCE IDX)

Returns the field value.

procedure: (circumstance-data-set! CIRCUMSTANCE IDX OBJECT)

Sets the field value.

procedure: (circumstance-data-override! ASSOCIATE [DATA...])

Sets all the field data.

Chore

procedure: (make-chore ID AMOUNT CIRCUMSTANCE ACTION [DATA...])

Returns a fresh chore object.

ID The name of the chore. A symbol.
AMOUNT Number of times to perform this chore. A positive integer, can be +inf.0.
CIRCUMSTANCE What triggers this chore. An circumstance.
ACTION (-> ASSOCIATE FACTS UNDEFINED). A procedure taking the assigned position and the facts about the particular triggering circumstance.
DATA Data for this chore. 0 or more objects. Reference is indexed from 0
procedure: (make-anonymous-chore AMOUNT CIRCUMSTANCE ACTION [DATA...])

Returns a fresh chore object, with a generated id.

procedure: (make-pass-thru-chore AMOUNT CIRCUMSTANCE THUNK [ID 'pass-thru])

Returns a fresh chore object, with an action around THUNK, a procedure of 0-arity, which ignores away any arguments.

procedure: (chore? OBJECT)

Is this OBJECT a chore?

procedure: (chore-id CHORE)

Returns the field value.

procedure: (chore-amount CHORE)

Returns the field value.

procedure: (chore-circumstance CHORE)

Returns the field value.

procedure: (chore-action CHORE)

Returns the field value.

procedure: (chore-data-ref CHORE IDX)

Returns the field value.

procedure: (chore-data-set! CHORE IDX OBJECT)

Sets the field value.

procedure: (chore-data-override! ASSOCIATE [DATA...])

Sets all the field data.

Laborer

procedure: (make-laborer ID ACTION [DATA...])

Returns a fresh laborer object.

ID The name of the laborer. A symbol.
ACTION (-> ASSOCIATE FACTS UNDEFINED). A procedure taking the assigned position and the facts about the particular triggering circumstance. Must invoke the associated chore action with assigned position and circumstance facts.
DATA Data for this laborer. 0 or more objects. Reference is indexed from 0
procedure: (make-anonymous-laborer ACTION [DATA...])

Returns a fresh laborer object, with a generated id.

procedure: (make-pass-thru-laborer (ID 'pass-thru))

Returns a fresh laborer object which directly invokes the associated chore action.

procedure: (laborer? OBJECT)

Is this OBJECT a laborer?

procedure: (laborer-id LABORER)

Returns the field value.

procedure: (laborer-action LABORER)

Returns the field value.

procedure: (laborer-data-ref LABORER IDX)

Returns the field value.

procedure: (laborer-data-set! LABORER IDX OBJECT)

Sets the field value.

procedure: (laborer-data-override! ASSOCIATE [DATA...])

Sets all the field data.

Associate

parameter: (default-associate-quantum [QUANTUM])

Gets or sets the default QUANTUM milliseconds for associates.

procedure: (make-associate CHORE LABORER [DATA...])

Returns a fresh assigned position object.

CHORE The chore to perform. A chore.
LABORER The laborer to perform the chore. A laborer.
DATA Data for this associate. 0 or more objects.
procedure: (associate? OBJECT)

Is this OBJECT an associate?

procedure: (associate-id ASSOCIATE)

Returns the field value.

procedure: (associate-chore ASSOCIATE)

Returns the field value.

procedure: (associate-laborer ASSOCIATE)

Returns the field value.

procedure: (associate-circumstance ASSOCIATE)

The circumstance for the associated chore.

procedure: (associate-amount ASSOCIATE)

The amount of work for the associated chore.

procedure: (associate-remaining-amount ASSOCIATE)

The remaining amount of work to perform.

procedure: (associate-remaining-amount-reset! ASSOCIATE)

Sets the remaining amount of work to perform to the amount of work of the associated chore.

procedure: (associate-data-ref ASSOCIATE IDX)

Returns the field value.

procedure: (associate-data-set! ASSOCIATE IDX OBJECT)

Sets the field value.

procedure: (associate-data-override! ASSOCIATE [DATA...])

Sets all the field data.

procedure: (associate-waiter ASSOCIATE)

Returns the field value.

procedure: (associate-start! ASSOCIATE)

Starts the associate running. Any subsequent attempt to start the associate will be silently ignored.

procedure: (associate-stop! ASSOCIATE)

Causes the associate to finish work early. Can be called from a chore or laborer action.

procedure: (associate-next-stop! ASSOCIATE)

Causes the associate to finish work early, after one more work session. Can be called from a chore or laborer action.

procedure: (associate-quantum ASSOCIATE)

The associate's time quantum.

procedure: (associate-quantum-set! ASSOCIATE QUANTUM)

Set the associate's time quantum.

procedure: (associate-quantum-adjust! ASSOCIATE [BIAS 0] | [FACTOR 1.0])

Raises or lowers the associate's time quantum by the BIAS milliseconds amount, or by the FACTOR percentage flonum value.

procedure: (associate-reap! ASSOCIATE)

Schedules the associate for "reaping."

procedure: (associate-unblock! ASSOCIATE)

Unblocks a blocked associate.

procedure: (associate-terminate! ASSOCIATE)

Abnormally terminates the associate.

procedure: (associate-suspend! ASSOCIATE)

Suspends the associate.

procedure: (associate-resume! ASSOCIATE)

Resumes a suspended associate.

parameter: (current-associate-reaper-quantum [QUANTUM])

Gets or sets the default QUANTUM milliseconds for the associate "reaper."

The reaper is the background task that handles waiting for the associates to finish. The reaper itself is automatically reaped upon process exit.

parameter: (current-associate-reaping [FLAG #t])

Gets or sets whether associates are automatically scheduled for "reaping" upon start.

Should automatic reaping be turned off it is the users' responsibility to explicitly reap the associate. See associate-reap! above.

procedure: (start-associate-reaper!)

Normally the reaper is not started until needed. However, chaining the implicit-exit-handler with code that attempts to force an associate reap will cause problems with the delayed reaper start. So this must be called before performing any such chaining.

Periodic Jobs

procedure: (run-timeout-job! ID THUNK [SECONDS 10.0] [AMOUNT +inf.0] [LABORER])

Creates and starts a timeout associate. Returns the associate.

ID The name of the chore. A symbol.
THUNK The action to perform upon timeout. A procedure of 0-arity.
SECONDS Perform chore every how many seconds. A positive number.
LABORER Who should perform the chore. A laborer. Defaults to a 'pass-thru' laborer.
procedure: (make-timeout-associate ID THUNK [SECONDS 10.0] [AMOUNT +inf.0] [LABORER])

Returns a timeout associate.

procedure: (make-timeout-chore ID THUNK [SECONDS 10.0] [AMOUNT +inf.0])

Returns a timeout chore.

procedure: (make-timeout-circumstance [SECONDS 10.0])

Returns a timeout circumstance.

Version

License

Copyright (c) 2005, Kon Lovett.  All rights reserved.

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the Software),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.