feat(linux): workmanager_linux — systemd-based background execution (fixes #324) - #716
Draft
ened wants to merge 3 commits into
Draft
feat(linux): workmanager_linux — systemd-based background execution (fixes #324)#716ened wants to merge 3 commits into
ened wants to merge 3 commits into
Conversation
|
To preview the documentation for this pull request, visit the following URL: docs.page/fluttercommunity/flutter_workmanager~716
|
On Linux the platform initialize() now succeeds (workmanager_linux), so the flow reaches _prepareInProcessExecution -> WorkmanagerFlutterApi.setUp, which requires ServicesBinding. Ensure the binding like other tests do.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the Linux desktop port for
flutter_workmanager(fixes #324) as anew federated package,
workmanager_linux, following the design indocs/desktop-support.mdx(systemd user units). Pure Dart — no Pigeon, nonative code: scheduling drives
systemctl --user/systemd-run --userthrough an injectable
ProcessRunner, so the whole test suite runs withoutsystemd.
What's implemented
systemd-run --user --unit=workmanager-<hash> --on-active=<delay>(
--no-blockfor immediate runs;--collectso finished units don'tlinger).
.timer/.servicepair written to~/.config/systemd/user/:OnUnitActiveSecfor the frequency,OnStartupSecfor the initial delay,Persistent=trueforWorkManager-style catch-up of runs missed while the system was off.
(
Platform.resolvedExecutable) with--background-task <taskName> --payload <path>. The app'smain()callsWorkmanagerLinux.maybeRunBackgroundTask(args, callbackDispatcher)whichruns the dispatcher, invokes the handler and exits
0/1(failed runsshow up in the journal).
inputData: JSON filesunder
$XDG_DATA_HOME/workmanager/payloads/, named by a deterministichash of the
uniqueName(same hash derives unit names, so no registry isneeded for query/cancel).
registerOneOffTask,registerPeriodicTask,cancelByUniqueName,cancelAll,isScheduledByUniqueName,printScheduledTasks, plusWorkmanagerLinux.executeTask(mirrorsWorkmanagerWeb.executeTask/workmanager_web'sexecution.dartregistry pattern).
workmanager_impl.dartplatformselection, path dependency in
workmanager/pubspec.yaml,workmanager_linuxadded tomelos.yaml, docs pagedocs/linux.mdx+sidebar entry + README platform list.
Honest unsupported surface (v1)
gating in v1.
retried; failed periodic tasks wait for the next interval.
existingWorkPolicy: effectivelyREPLACE;KEEPnot implemented.cancelByTagthrowsUnsupportedError(needs a tag registry; cancel by unique name orcancelAll).registerProcessingTask, health research,continued processing):
UnsupportedError.Headless dispatcher pattern
The dispatcher registers via
WorkmanagerLinux.executeTask(aWorkmanagerExecution-style registry mirroringworkmanager_web), notWorkmanager().executeTask— the latter awaits a platform-channel handshakethat has no native counterpart on Linux and would fail in a headless process.
Testing
Pure Dart unit tests (no systemd): command construction (systemd-run args,
unit file contents, cancel/query commands) against a fake
ProcessRunner,payload round-trip,
--background-taskarg parsing, and the platformimplementation.
melos run test,dart analyzeanddart format --set-exit-if-changedall pass locally (fvm, Flutter 3.44.8).Note on the runner: the platform-level test file imports
WorkmanagerLinux→workmanager_platform_interface→ Flutter, which theplain VM can't load (
dart:ui), so the suite runs underflutter test(like
workmanager_web); the pure command/payload/runner tests also passunder
dart test.Follow-ups (not in this PR)
BackgroundTaskResultmigration: this package implements thecurrent
Future<bool>main API, as do the other desktop/web packages; itwill migrate to the enum with feat!: BackgroundTaskResult enum instead of bool (fixes #23) #712.
Platform.isWindowsbranchto the same
_ensurePlatformImplementationblock — expect a mergeconflict in
workmanager/lib/src/workmanager_impl.dart, that's fine.workmanager/test/backward_compatibility_test.dartstill expects aplaceholder (
UnimplementedError) on Linux test hosts; once both desktopbranches land, that expectation should be updated (it passes unchanged on
macOS/iOS/Android hosts).
workmanager_linuxis apath:dependency of the publishableworkmanagerpackage for now (pub lint warninginvalid_dependency); itshould switch to
^0.1.0once the package is released — the melosgitignored
pubspec_overrides.yamlalready wires the path locally.dart test/flutter testinworkmanager_linux(themelos
testscript already picks it up viaflutter_test).