-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from msqd/janitor
Janitor
- Loading branch information
Showing
36 changed files
with
580 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
harp_apps.janitor | ||
================= | ||
|
||
.. automodule:: harp_apps.janitor | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: | ||
|
||
Submodules | ||
---------- | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
|
||
harp_apps.janitor.settings | ||
harp_apps.janitor.worker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
harp_apps.janitor.settings | ||
========================== | ||
|
||
.. automodule:: harp_apps.janitor.settings | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
harp_apps.janitor.worker | ||
======================== | ||
|
||
.. automodule:: harp_apps.janitor.worker | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
docs/reference/apps/harp_apps.sqlalchemy_storage.models.metrics.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
harp_apps.sqlalchemy_storage.models.metrics | ||
=========================================== | ||
|
||
.. automodule:: harp_apps.sqlalchemy_storage.models.metrics | ||
:members: | ||
:undoc-members: | ||
:show-inheritance: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { useSystemSettingsQuery } from "./useSystemSettingsQuery.ts" | ||
export { useSystemQuery } from "./useSystemQuery.ts" | ||
export { useSystemDependenciesQuery } from "./useSystemDependenciesQuery.ts" | ||
export { useSystemStorageQuery } from "./useSystemStorageQuery.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { useQuery } from "react-query" | ||
|
||
import { useApi } from "Domain/Api" | ||
|
||
export function useSystemStorageQuery() { | ||
const api = useApi() | ||
return useQuery(["system", "storage"], () => api.fetch("/system/storage").then((r) => r.json())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { OnQuerySuccess } from "Components/Utilities/OnQuerySuccess.tsx" | ||
import { useSystemStorageQuery } from "Domain/System" | ||
import { KeyValueSettings, Setting } from "Domain/System/useSystemSettingsQuery.ts" | ||
import { Pane } from "mkui/Components/Pane" | ||
import { Tab } from "mkui/Components/Tabs" | ||
|
||
import { SettingsTable } from "./Components" | ||
|
||
export function SystemStorageTabPanel() { | ||
const query = useSystemStorageQuery() | ||
|
||
return ( | ||
<Tab.Panel> | ||
<OnQuerySuccess query={query}> | ||
{(query) => ( | ||
<Pane hasDefaultPadding={false}> | ||
<SettingsTable settings={query.data as KeyValueSettings | Array<Setting>} /> | ||
</Pane> | ||
)} | ||
</OnQuerySuccess> | ||
</Tab.Panel> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import asyncio | ||
|
||
from harp.config import Application | ||
from harp.config.events import FactoryBindEvent, FactoryBoundEvent | ||
from harp.typing import Storage | ||
from harp_apps.janitor.worker import JanitorWorker | ||
|
||
|
||
class JanitorApplication(Application): | ||
async def on_bind(self, event: FactoryBindEvent): | ||
pass | ||
|
||
async def on_bound(self, event: FactoryBoundEvent): | ||
self.worker = JanitorWorker(event.provider.get(Storage)) | ||
self.worker_task = asyncio.create_task(self.worker.run()) | ||
|
||
async def unmount(self): | ||
self.worker.stop() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from datetime import timedelta | ||
|
||
#: How many seconds to wait between loop executions. | ||
PERIOD = 600 | ||
|
||
#: Number of days after we consider something as "old". | ||
OLD_AFTER = timedelta(days=60) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from datetime import UTC, datetime, timedelta | ||
|
||
from harp_apps.janitor.settings import OLD_AFTER | ||
from harp_apps.janitor.worker import JanitorWorker | ||
from harp_apps.sqlalchemy_storage.storage import SqlAlchemyStorage | ||
from harp_apps.sqlalchemy_storage.utils.testing.mixins import SqlalchemyStorageTestFixtureMixin | ||
|
||
|
||
class TestJanitorWorker(SqlalchemyStorageTestFixtureMixin): | ||
async def test_delete_old_transactions(self, storage: SqlAlchemyStorage): | ||
worker = JanitorWorker(storage) | ||
|
||
await self.create_transaction( | ||
storage, started_at=datetime.now(UTC).replace(tzinfo=None) - OLD_AFTER - timedelta(hours=1) | ||
) | ||
await self.create_transaction( | ||
storage, started_at=datetime.now(UTC).replace(tzinfo=None) - OLD_AFTER - timedelta(minutes=1) | ||
) | ||
await self.create_transaction( | ||
storage, started_at=datetime.now(UTC).replace(tzinfo=None) - OLD_AFTER + timedelta(minutes=1) | ||
) | ||
|
||
async with storage.session() as session: | ||
assert (await worker.compute_metrics(session))["storage.transactions"] == 3 | ||
|
||
await worker.delete_old_transactions() | ||
|
||
async with storage.session() as session: | ||
assert (await worker.compute_metrics(session))["storage.transactions"] == 1 | ||
|
||
async def test_delete_orphan_blobs(self, storage: SqlAlchemyStorage): | ||
worker = JanitorWorker(storage) | ||
|
||
b1 = await self.create_blob(storage, "foo") | ||
b2 = await self.create_blob(storage, "bar") | ||
await self.create_blob(storage, "baz") | ||
|
||
async with storage.session() as session: | ||
metrics = await worker.compute_metrics(session) | ||
assert metrics["storage.blobs"] == 3 | ||
assert metrics["storage.blobs.orphans"] == 3 | ||
|
||
t = await self.create_transaction(storage) | ||
await self.create_message(storage, transaction_id=t.id, kind="misc", summary="foo", headers=b1.id, body=b2.id) | ||
|
||
async with storage.session() as session: | ||
metrics = await worker.compute_metrics(session) | ||
assert metrics["storage.blobs"] == 3 | ||
assert metrics["storage.blobs.orphans"] == 1 | ||
|
||
await worker.delete_orphan_blobs() | ||
|
||
async with storage.session() as session: | ||
metrics = await worker.compute_metrics(session) | ||
assert metrics["storage.blobs"] == 2 | ||
assert metrics["storage.blobs.orphans"] == 0 |
Oops, something went wrong.