Skip to content

Instantly share code, notes, and snippets.

@larsrh
Created March 19, 2018 11:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save larsrh/2265f283d5f555e57bb675881f6fc341 to your computer and use it in GitHub Desktop.
Save larsrh/2265f283d5f555e57bb675881f6fc341 to your computer and use it in GitHub Desktop.
Tiny job scheduler in SWI Prolog, for tutorial purposes.
:- dynamic running/2.
:- dynamic jobid/1.
jobid(0).
resource(node1, 1, linux).
resource(node2, 2, linux).
resource(node3, 1, macos).
capacity(Node, Capacity) :- resource(Node, Capacity, _).
current_jobs(Node, Jobs) :-
findall(J, running(Node, J), Jobs).
current_capacity(Node, Capacity) :-
capacity(Node, C),
current_jobs(Node, Jobs),
length(Jobs, J),
Capacity is C - J.
find_available(Tag, Node) :-
resource(Node, _, Tag),
current_capacity(Node, C),
C >= 1.
next_id(Id) :-
jobid(OldId),
retract(jobid(OldId)),
Id is OldId + 1,
assert(jobid(Id)).
schedule(Tag, Handle) :-
find_available(Tag, Node), !,
next_id(Id),
Handle = running(Node, Id),
assert(Handle).
shutdown :-
retractall(running(_, _)),
retractall(jobid(_)),
assert(jobid(0)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment