common.go (2265B)
1 // GoToSocial 2 // Copyright (C) GoToSocial Authors admin@gotosocial.org 3 // SPDX-License-Identifier: AGPL-3.0-or-later 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package prune 19 20 import ( 21 "context" 22 "fmt" 23 24 "github.com/superseriousbusiness/gotosocial/internal/db" 25 "github.com/superseriousbusiness/gotosocial/internal/db/bundb" 26 "github.com/superseriousbusiness/gotosocial/internal/media" 27 "github.com/superseriousbusiness/gotosocial/internal/state" 28 gtsstorage "github.com/superseriousbusiness/gotosocial/internal/storage" 29 ) 30 31 type prune struct { 32 dbService db.DB 33 storage *gtsstorage.Driver 34 manager *media.Manager 35 state *state.State 36 } 37 38 func setupPrune(ctx context.Context) (*prune, error) { 39 var state state.State 40 41 state.Caches.Init() 42 state.Caches.Start() 43 44 state.Workers.Start() 45 46 dbService, err := bundb.NewBunDBService(ctx, &state) 47 if err != nil { 48 return nil, fmt.Errorf("error creating dbservice: %w", err) 49 } 50 state.DB = dbService 51 52 //nolint:contextcheck 53 storage, err := gtsstorage.AutoConfig() 54 if err != nil { 55 return nil, fmt.Errorf("error creating storage backend: %w", err) 56 } 57 state.Storage = storage 58 59 //nolint:contextcheck 60 manager := media.NewManager(&state) 61 62 return &prune{ 63 dbService: dbService, 64 storage: storage, 65 manager: manager, 66 state: &state, 67 }, nil 68 } 69 70 func (p *prune) shutdown(ctx context.Context) error { 71 if err := p.storage.Close(); err != nil { 72 return fmt.Errorf("error closing storage backend: %w", err) 73 } 74 75 if err := p.dbService.Stop(ctx); err != nil { 76 return fmt.Errorf("error closing dbservice: %w", err) 77 } 78 79 p.state.Workers.Stop() 80 p.state.Caches.Stop() 81 82 return nil 83 }