Recover the board and close owners

Bob leaves the pending revision-two cues unread. He unsubscribes, then queries the board and gets the complete current state. This is the same recovery path a reconnecting player can use. Hand cursors can start again from fresh publications.

The example checks this final board:

edn238 B
{:revision 2
 :turn     "red"
 :pieces   {:red7   {:color  "red"
                     :square 15
                     :king   false}
            :black1 {:color  "black"
                     :square 4
                     :king   false}}}

Closing the owners releases their subscriptions and registrations and cancels their remaining exchanges. The example checks that all plugin resource counts are zero. Each language version also closes owners if an earlier operation fails. Its scope or cleanup block releases database and document handles on either path.

Run the complete progression

The entry point runs the steps in order against one database. Every chapter above displays code taken from that executable program. Run the downloaded program to check the assertions, then change a landing or expected revision to explore the stored mutations.

Use the Events reference for operation contracts, queue limits and cancellation behavior. Read Event coordination for the separation between stored facts and temporary messages. A durable work queue needs stored work records and a recovery policy beyond these temporary exchanges.

Code

odin938 B
recover_and_close :: proc(
	game: ^Game,
	bob: runtime.Owner,
	subscription: runtime.Subscription(events.Checkers_Table_Cursor_Data),
	expected: Board,
) -> sd.Error {
	runtime.unsubscribe(subscription) or_return
	current := board(game) or_return
	assert(
		current.revision == expected.revision &&
		current.turn == expected.turn &&
		len(current.pieces) == len(expected.pieces),
	)
	for id, piece in expected.pieces {assert(current.pieces[id] == piece)}
	close_owners(game) or_return
	status_document := sd.events_status(game.db) or_return
	defer sd.close(&status_document)

	status := typed.decode(status_document, Status) or_return
	assert(
		status.owners == 0 &&
		status.subscriptions == 0 &&
		status.registrations == 0 &&
		status.rpcs == 0 &&
		status.queued == 0,
	)
	result := typed.encode(current) or_return
	defer sd.close(&result)

	output := sd.serialize(result, .DUST) or_return
	fmt.println(string(output))
	return nil
}
odin1.9 kB
main :: proc() {
	arena: virtual.Arena
	if error := virtual.arena_init_growing(&arena); error != nil {fmt.eprintln(error); os.exit(1)}
	defer virtual.arena_destroy(&arena)

	context.allocator = virtual.arena_allocator(&arena)
	if error := run(); error != nil {
		fmt.eprintln(error)
		sd.close(&error)
		os.exit(1)
	}
}

run :: proc() -> sd.Error {
	sd.activate(
		strings.trim_space(os.get_env("STARDUST_TEST_LICENSE", context.allocator)),
	) or_return
	assert(len(os.args) == 3, "Pass fixture path and format")
	bytes, read_ok := os.read_entire_file(os.args[1], context.allocator)
	assert(read_ok == nil, "Cannot read fixture")
	formats := map[string]sd.Source_Format {
		"dust" = .DUST,
		"json" = .JSON,
		"edn"  = .EDN,
		"tron" = .TRON,
	}
	format, found := formats[os.args[2]]
	assert(found, "Choose dust, json, edn or tron")
	document := sd.parse(string(bytes), format) or_return
	defer sd.close(&document)

	fixture := typed.decode(document, Fixture) or_return
	directory, directory_error := os.make_directory_temp(
		"",
		"stardust-checkers-",
		context.allocator,
	)
	assert(directory_error == nil, "Cannot create temporary directory")
	defer os.remove_all(directory)

	db := open_checkers(
		fmt.aprintf("%s/checkers.db", directory),
		fmt.aprintf("%s/sdk", directory),
	) or_return
	defer sd.close(&db)

	game := Game {
		db = db,
	}
	defer {
		cleanup := close_owners(&game)
		if cleanup != nil {fmt.eprintln(cleanup); sd.close(&cleanup)}
	}
	prepare(&game, fixture) or_return
	alice := owner(&game) or_return
	bob := owner(&game) or_return
	rules := owner(&game) or_return
	subscription := share_cursors(&game, bob) or_return
	move_one_piece(&game, alice, bob, rules, subscription) or_return
	list_legal_moves(&game, bob, rules) or_return
	make_two_jumps(&game, bob, rules) or_return
	preview_a_drag(&game, alice, rules) or_return
	return recover_and_close(&game, bob, subscription, fixture.expected)
}