Skip to content

Commit

Permalink
refactor: change tasks to use neat_periodic_tasks & add db backups on…
Browse files Browse the repository at this point in the history
… interval
  • Loading branch information
lukehmcc committed Aug 13, 2024
1 parent 744d3e3 commit e84a03b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
2 changes: 1 addition & 1 deletion lib/functions/s5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Future<S5> _initS5() async {
],
logger: kDebugMode
// logger: false
? DebugLogger(
? FileLogger(
file: join(await getLogPath(),
'log-$lastEightDigits.txt')) // If in debug mode I want everything dumped to stdout
: FileLogger(
Expand Down
53 changes: 28 additions & 25 deletions lib/messenger/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import 'package:image_picker/image_picker.dart';
import 'package:http/http.dart' as http;
import 'package:lib5/identity.dart';
import 'package:lib5/node.dart';
import 'package:neat_periodic_task/neat_periodic_task.dart';
import 'package:s5/s5.dart' as s5lib;
import 'package:universal_io/io.dart' as io;
import 'package:vup_chat/bsky/chat_actions.dart';
Expand Down Expand Up @@ -41,7 +42,11 @@ class MsgCore {
n.FlutterLocalNotificationsPlugin? notifier;
// Keeps track of notification channels
Map<String, int> messageNotifChannels = {};
// Keeps track if app is in foreground or background
StreamSubscription<FGBGType>? subscription;
// Allows canceling and starting of background tasks
NeatPeriodicTaskScheduler? backgroundChatFetchScheduler;
NeatPeriodicTaskScheduler? dbBackupScheduler;

// Named constructor
MsgCore.custom({
Expand All @@ -62,7 +67,29 @@ class MsgCore {
void init() async {
await attemptLogin(null, null);

_startBackgroundTask();
backgroundChatFetchScheduler = NeatPeriodicTaskScheduler(
interval: const Duration(seconds: 5),
name: 'background-chat-fetch-task',
timeout: const Duration(seconds: 30),
task: () async {
await _populateListViewDBATProto();
await Future.delayed(const Duration(seconds: 5));
await _fetchAllChats();
},
minCycle: const Duration(seconds: 1),
);

dbBackupScheduler = NeatPeriodicTaskScheduler(
interval: const Duration(hours: 2),
name: 'db-backup-task',
timeout: const Duration(hours: 1),
task: backupSQLiteToS5,
minCycle: const Duration(hours: 1),
);

backgroundChatFetchScheduler?.start();
dbBackupScheduler?.start();

if (!kIsWeb) {
_initNotifications();
}
Expand All @@ -72,30 +99,6 @@ class MsgCore {
s5 = await initS5();
}

void _startBackgroundTask() async {
while (true) {
// grab message lsit
await _populateListViewDBATProto();
await Future.delayed(const Duration(seconds: 5));
// grab all chats, add lock to make sure to only insert from here when
// background task is running
await _fetchAllChats();
// also make sure the sessions are logged in
await Future.delayed(const Duration(seconds: 5));
if (bskySession == null ||
bskySession!.session == null ||
bskySession!.session!.active == false) {
final Session? session = await tryLogIn(null, null);
if (session != null) {
bskySession = Bluesky.fromSession(
session,
);
bskyChatSession = BlueskyChat.fromSession(session);
}
}
}
}

void _initNotifications() async {
notifier = n.FlutterLocalNotificationsPlugin();
// initialise the plugin. app_icon needs to be a added as a drawable resource to the Android head project
Expand Down
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -912,6 +912,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.2.3"
neat_periodic_task:
dependency: "direct main"
description:
name: neat_periodic_task
sha256: e0dda74c996781e154f6145028dbacbcd9dbef242f5a140fa774e39381c2bf97
url: "https://pub.dev"
source: hosted
version: "2.0.1"
nested:
dependency: transitive
description:
Expand Down Expand Up @@ -1088,6 +1096,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.1.0"
retry:
dependency: transitive
description:
name: retry
sha256: "822e118d5b3aafed083109c72d5f484c6dc66707885e07c0fbcb8b986bba7efc"
url: "https://pub.dev"
source: hosted
version: "3.1.2"
rxdart:
dependency: transitive
description:
Expand Down Expand Up @@ -1205,6 +1221,14 @@ packages:
description: flutter
source: sdk
version: "0.0.99"
slugid:
dependency: transitive
description:
name: slugid
sha256: e0cc54637b666c9c590f0d76df76e5e2bbf6234ae398a182aac82fd70ddd60ab
url: "https://pub.dev"
source: hosted
version: "1.1.2"
source_gen:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ dependencies:
flutter_local_notifications: ^17.2.1+2
flutter_fgbg: ^0.3.0
universal_io: ^2.2.2
neat_periodic_task: ^2.0.1

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit e84a03b

Please sign in to comment.