38 Commits

Author SHA1 Message Date
Sybren A. Stüvel
b102b73a1f Refactor: convert more job functions to sqlc
No functional changes.
2024-03-03 23:23:51 +01:00
Sybren A. Stüvel
c046094880 Manager: start replacing GORM with SQLC
GORM has certain downsides:

- Code-first approach, where queries have to be translated to the Go code
  required to execute them.
- GORM comes with its own SQLite implementation, which doesn't provide an
  on-connect callback. This means that new connections cannot correctly
  enable foreign key constraints, causing database consistency issues.

[SQLC](https://sqlc.dev/) solves these issues for us.

This commit doesn't fully replace GORM with SQLC, but introduces it for
a few queries. Once all queries have been converted, GORM can be removed
completely.
2024-03-03 20:15:39 +01:00
Sybren A. Stüvel
b39f116b0e Manager: after deleting a job, perform a database consistency check
Deleting jobs from the database can still sometimes cause consistency
errors, as if foreign key constraints aren't enabled. This check is there
to try and get a grip on things.
2024-01-11 20:03:53 +01:00
Sybren A. Stüvel
a65f234bea Manager: replace GORM database migration with Goose
Replace GORM's auto-migration with Goose. The latter uses hand-written
SQL queries to apply database schema changes, which is safer and easier to
understand than what GORM is doing.
2023-12-14 10:13:40 +01:00
Sybren A. Stüvel
02fac6a4df Change Go package name from git.blender.org to projects.blender.org
Change the package base name of the Go code, from
`git.blender.org/flamenco` to `projects.blender.org/studio/flamenco`.

The old location, `git.blender.org`, has no longer been use since the
[migration to Gitea][1]. The new package names now reflect the actual
location where Flamenco is hosted.

[1]: https://code.blender.org/2023/02/new-blender-development-infrastructure/
2023-08-01 12:42:31 +02:00
Sybren A. Stüvel
4121c899c3 Manager: perform database integrity check every hour
Perform a database integrity check every hour. This check was already
performed at startup, in the main goroutine.
2023-07-18 16:10:17 +02:00
Sybren A. Stüvel
06738b8aa4 Manager: disable SQLite foreign key constraints when migrating the database
There is an issue with the GORM auto-migration, in that it doesn't
always disable foreign key constraints when it should. Due to
limitations of SQLite, not all 'alter table' commands you'd want to use
are available. As a workaround, these steps are performed:

1. create a new table with the desired schema,
2. copy the data over,
3. drop the old table,
4. rename the new table to the old name.

Step #3 will wreak havoc with the database when foreign key constraint
checks are active, so no we temporarily deactivate them while performing
database migration.
2023-07-10 14:06:21 +02:00
Sybren A. Stüvel
7a508c7e6b Manager: perform database integrity check at startup
Perform these two SQL calls & check their result:

- `PRAGMA integrity_check`
- `PRAGMA foreign_key_check`:

See  https: //www.sqlite.org/pragma.html for more info on these.

This also removes the unused `PeriodicMaintenanceLoop()` function.
Periodic checking while Flamenco Manager is running might be introduced
in a future commit, after the startup-time checks have been shown to not
get in the way.
2023-07-07 16:03:06 +02:00
Sybren A. Stüvel
7f588e6dbc Manager: close database connection on startup errors
When there is an error detected at startup, close the database connection.
Before, the connection could be kept open even when an error was returned,
causing the write-ahead log files to be kept around. These are now
properly integrated into the main database file before exiting.
2023-07-07 15:48:08 +02:00
Sybren A. Stüvel
aa1c6b8ff3 Close the database when Flamenco shuts down
This prevents SQLite journal files from lingering around.
2023-02-07 15:05:49 +01:00
Sybren A. Stüvel
15e3745820 Manager: SQLite WAL journal + NORMAL sync mode
Run `PRAGMA journal_mode = WAL` and `PRAGMA synchronous = normal` when
connecting to the SQLite database. This enables the write-ahead-log journal
mode, which makes it safe to enable "normal" synchronisation (instead of
the default "full" synchronisation).
2022-11-24 17:18:06 +01:00
Sybren A. Stüvel
e5d0e987e1 Manager: enforce DB foreign key checks at startup
SQLite disables foreign key checks by default, so Flamenco has to enable
them explicitly.
2022-06-13 18:41:19 +02:00
Sybren A. Stüvel
02bc03ae2b Manager: replace gorm.Model with our own persistence.Model struct
`persistence.Model` contains the common database fields for most model
structs. It is a copy of `gorm.Model`, but without the `DeletedAt`
field (which triggers Gorm's soft deletion).

Soft deletion is not used by Flamenco. If it ever becomes necessary to
support soft-deletion, see https://gorm.io/docs/delete.html#Soft-Delete
2022-06-13 18:40:42 +02:00
Sybren A. Stüvel
295891a17a Manager: ensure Gorm-generated timestamps are in UTC
SQLite should store all timestamps in UTC, as the database is woefully
unaware of timezones and will compare lexicographically.
2022-06-10 14:31:53 +02:00
Sybren A. Stüvel
d35ca9d98f Manager: limit database connections
Limit the database connection pool to only a single connection. I hope that
this will solve the intermittent `SQLITE_BUSY` errors I've been seeing.
2022-05-12 13:58:15 +02:00
Sybren A. Stüvel
d3e2638f84 Cleanup: rename uri to dsn
"DSN" (Data Source Name) is used to indicate which database to open, and
was intermixed with "URI". This is now consistent.

No functional changes.
2022-05-12 11:08:54 +02:00
Sybren A. Stüvel
98da20f1a9 Manager: vacuum the database at startup 2022-05-06 14:35:34 +02:00
Sybren A. Stüvel
930d7497d7 OAPI: Better 'SQLITE_BUSY' error handling
SQLite can return `SQLITE_BUSY` errors when it's doing too many things at
the same time. This is now improved a bit by setting a 5-second timeout,
during which the SQLite driver will wait for the database to become
available. If that doesn't happen, Flamenco Manager will return a
`503 Service Unavailable` response so that the client knows to back off
a little.
2022-04-08 12:02:30 +02:00
Sybren A. Stüvel
22ea599554 Manager: periodically run the SQL VACUUM command 2022-03-17 11:03:29 +01:00
Sybren A. Stüvel
9f5e4cc0cc License: license all code under "GPL-3.0-or-later"
The add-on code was copy-pasted from other addons and used the GPL v2
license, whereas by accident the LICENSE text file had the GNU "Affero" GPL
license v3 (instead of regular GPL v3).

This is now all streamlined, and all code is licensed as "GPL v3 or later".

Furthermore, the code comments just show a SPDX License Identifier
instead of an entire license block.
2022-03-07 15:26:46 +01:00
Sybren A. Stüvel
b9609f8866 Cleanup: remove unused code 2022-03-03 13:52:57 +01:00
Sybren A. Stüvel
641ed7ace9 Manager: make Gorm use Zerolog for logging
A wrapper for Zerolog implements the Gorm logger interface. This gives
us coloured output on Windows, and uniform-looking logs in production.
2022-03-03 13:52:50 +01:00
Sybren A. Stüvel
9b9c6bffff Replace self-hacked SQLite Gorm driver with 3rd party one
The new Gorm driver is made by the creators of the pure-Go SQLite library
we were already using.
2022-03-03 13:48:14 +01:00
Sybren A. Stüvel
47e36c927c Change package URL to the blender.org repository 2022-03-01 20:45:09 +01:00
Sybren A. Stüvel
e70a44a146 Manager: switch from PostgreSQL to SQLite
This includes a modified copy of the Gorm SQLite backend, adjusted to use
https://modernc.org/sqlite instead.
2022-03-01 18:50:31 +01:00
Sybren A. Stüvel
b1b73de4ee Manager: Actually load config, and use in a few places 2022-02-21 19:02:07 +01:00
Sybren A. Stüvel
97ab93d996 Initial task scheduler implementation 2022-02-01 17:17:19 +01:00
Sybren A. Stüvel
931fd1a24c Move db/jobs tests to persistence/jobs.go 2022-01-28 14:53:02 +01:00
Sybren A. Stüvel
3b1cb3c187 Remove sqlite library & run go mod tidy 2022-01-28 14:52:46 +01:00
Sybren A. Stüvel
0b3311b0a7 Change parameter format of blender-render command 2022-01-28 14:48:19 +01:00
Sybren A. Stüvel
30518ca3af Store tasks & commands in the database 2022-01-25 18:25:26 +01:00
Sybren A. Stüvel
edda8f21cf Make tests work on PostgreSQL 2022-01-25 18:12:01 +01:00
Sybren A. Stüvel
0f8bacd2e5 Store compiled jobs with GORM in PostgreSQL 2022-01-24 18:07:12 +01:00
Sybren A. Stüvel
b446b02739 Change package URL
Change package URL from `gitlab.com/blender/flamenco-goja-test` to
`gitlab.com/blender/flamenco-ng-poc` so that it matches the actual URL
on Gitlab.
2022-01-24 14:28:00 +01:00
Sybren A. Stüvel
0629728ce9 Add frame chunker and make unit test for simple blender render succeed 2022-01-13 16:32:08 +01:00
Sybren A. Stüvel
54933d0262 API: add Fetch Job endpoint
This is made with plain sqlite. It's probably a good idea to move to
something like GORM instead.
2022-01-11 18:38:14 +01:00
Sybren A. Stüvel
f0ae7d33e9 Connect API, job compiler, and persistence layer 2022-01-11 18:07:34 +01:00
Sybren A. Stüvel
0c9c99806b Start of database interface
Contains a dummy migration because the migrate package will error out if
it cannot find any migration files.
2022-01-11 18:07:34 +01:00