Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormatterException #5

Closed
techouse opened this issue Jun 8, 2023 · 8 comments
Closed

FormatterException #5

techouse opened this issue Jun 8, 2023 · 8 comments
Assignees
Labels
bug Something isn't working

Comments

@techouse
Copy link
Contributor

techouse commented Jun 8, 2023

Hey :)

Tried running the generator with squadron: 5.1.1 and squadron_builder: 2.2.0 and got this FormatterExceoption. Looks like it applies to squadron_builder: 2.2.0, as it fails as well with suqadron: 5.0.0.

[SEVERE] squadron_builder:worker_builder on lib/src/core/api/data/service/json_service.dart:
An error `FormatterException` occurred while formatting the generated source for
  `package:my_app/src/core/api/data/service/json_service.dart`
which was output to
  `lib/src/core/api/data/service/json_service.worker.g.dart`.
This may indicate an issue in the generator, the input source code, or in the
source formatter.
Could not format because the source could not be parsed:

line 160, column 79 of .: Expected to find ')'.
    ╷
160 │                 () => JsonServiceWorker(platformWorkerHook: platformWorkerHook}),

My json_service.dart is pretty basic

import 'dart:async';
import 'dart:convert' show jsonDecode;

import 'package:squadron/squadron.dart';
import 'package:squadron/squadron_annotations.dart';

import 'json_service.activator.g.dart';

part 'json_service.worker.g.dart';

@SquadronService(web: false)
class JsonService extends WorkerService with $JsonServiceOperations {
  @SquadronMethod()
  Future<dynamic> decode(String source) async => jsonDecode(source);
}

and the generated json_service.worker.g.dart file looks like this

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'json_service.dart';

// **************************************************************************
// Generator: WorkerGenerator 2.2.0
// **************************************************************************

// Operations map for JsonService
        mixin $JsonServiceOperations on WorkerService {
          @override
          late final Map<int, CommandHandler> operations = _getOperations(this as JsonService);

          static const int _$decodeId = 1;

          static Map<int, CommandHandler> _getOperations(JsonService svc) => {
            _$decodeId: (req) => svc.decode(req.args[0])
          };
        }

// Service initializer
        JsonService $JsonServiceInitializer(WorkerRequest startRequest)
            => JsonService();

// Worker for JsonService
        class _JsonServiceWorker
          extends Worker with $JsonServiceOperations
          implements JsonService {
          
          _JsonServiceWorker({PlatformWorkerHook? platformWorkerHook}) : super($JsonServiceActivator, platformWorkerHook: platformWorkerHook);

          

                @override
      Future<dynamic> decode(String source) => send(
            $JsonServiceOperations._$decodeId, 
            args: [ source ], 
            
            
            
          );
    

          @override
          Map<int, CommandHandler> get operations => WorkerService.noOperations;

          

          

          final Object _detachToken = Object();
        }

        // Finalizable worker wrapper for JsonService
        class JsonServiceWorker implements _JsonServiceWorker {
          
          JsonServiceWorker() : _worker = _JsonServiceWorker() {
            _finalizer.attach(this, _worker, detach: _worker._detachToken);
          }

          

          final _JsonServiceWorker _worker;

          static final Finalizer<_JsonServiceWorker> _finalizer = Finalizer<_JsonServiceWorker>((w) {
            try {
              _finalizer.detach(w._detachToken);
              w.stop();
            } catch (ex) {
              // finalizers must not throw
            }
          });

                @override
      Future<dynamic> decode(String source) => _worker.decode(source);
    

          @override
          Map<int, CommandHandler> get operations => _worker.operations;

          

          

                @override
      List get args => _worker.args;
    

      @override
      Channel? get channel => _worker.channel;
    

      @override
      Duration get idleTime => _worker.idleTime;
    

      @override
      bool get isStopped => _worker.isStopped;
    

      @override
      int get maxWorkload => _worker.maxWorkload;
    

      @override
      WorkerStat get stats => _worker.stats;
    

      @override
      String get status => _worker.status;
    

      @override
      int get totalErrors => _worker.totalErrors;
    

      @override
      int get totalWorkload => _worker.totalWorkload;
    

      @override
      Duration get upTime => _worker.upTime;
    

      @override
      String get workerId => _worker.workerId;
    

      @override
      int get workload => _worker.workload;
    

      @override
      Future<Channel> start() => _worker.start();
    

      @override
      void stop() => _worker.stop();
    

      @override
      Future<T> send<T>(int command, {List args = const [], CancellationToken? token, bool inspectRequest = false, bool inspectResponse = false}) => _worker.send<T>(command, args: args, token: token, inspectRequest: inspectRequest, inspectResponse: inspectResponse);
    

      @override
      Stream<T> stream<T>(int command, {List args = const [], CancellationToken? token, bool inspectRequest = false, bool inspectResponse = false}) => _worker.stream<T>(command, args: args, token: token, inspectRequest: inspectRequest, inspectResponse: inspectResponse);
    

      @override
      Object get _detachToken => _worker._detachToken;
    
        }

// Worker pool for JsonService
          class _JsonServiceWorkerPool
            extends WorkerPool<JsonServiceWorker> with $JsonServiceOperations
            implements JsonService {

            _JsonServiceWorkerPool({ConcurrencySettings? concurrencySettings, PlatformWorkerHook? platformWorkerHook}) : super(
                () => JsonServiceWorker(platformWorkerHook: platformWorkerHook}),
                concurrencySettings: concurrencySettings);

            

                  @override
      Future<dynamic> decode(String source) => execute(($w) => $w.decode(source));
    

            @override
            Map<int, CommandHandler> get operations => WorkerService.noOperations;

            

            

            final Object _detachToken = Object();
          }

        // Finalizable worker pool wrapper for JsonService
        class JsonServiceWorkerPool implements _JsonServiceWorkerPool {
          
          JsonServiceWorkerPool({ConcurrencySettings? concurrencySettings, PlatformWorkerHook? platformWorkerHook}) : _pool = _JsonServiceWorkerPool(concurrencySettings: concurrencySettings, platformWorkerHook: platformWorkerHook) {
            _finalizer.attach(this, _pool, detach: _pool._detachToken);
          }

          

          final _JsonServiceWorkerPool _pool;

          static final Finalizer<_JsonServiceWorkerPool> _finalizer = Finalizer<_JsonServiceWorkerPool>((p) {
            try {
              _finalizer.detach(p._detachToken);
              p.stop();
            } catch (ex) {
              // finalizers must not throw
            }
          });

                @override
      Future<dynamic> decode(String source) => _pool.decode(source);
    

          @override
          Map<int, CommandHandler> get operations => _pool.operations;

          

          

                @override
      ConcurrencySettings get concurrencySettings => _pool.concurrencySettings;
    

      @override
      Iterable<WorkerStat> get fullStats => _pool.fullStats;
    

      @override
      int get maxConcurrency => _pool.maxConcurrency;
    

      @override
      int get maxParallel => _pool.maxParallel;
    

      @override
      int get maxSize => _pool.maxSize;
    

      @override
      int get maxWorkers => _pool.maxWorkers;
    

      @override
      int get maxWorkload => _pool.maxWorkload;
    

      @override
      int get minWorkers => _pool.minWorkers;
    

      @override
      int get pendingWorkload => _pool.pendingWorkload;
    

      @override
      int get size => _pool.size;
    

      @override
      Iterable<WorkerStat> get stats => _pool.stats;
    

      @override
      bool get stopped => _pool.stopped;
    

      @override
      int get totalErrors => _pool.totalErrors;
    

      @override
      int get totalWorkload => _pool.totalWorkload;
    

      @override
      int get workload => _pool.workload;
    

      @override
      void cancel([Task? task, String? message]) => _pool.cancel(task, message);
    

      @override
      FutureOr start() => _pool.start();
    

      @override
      int stop([bool Function(JsonServiceWorker worker)? predicate]) => _pool.stop(predicate);
    

      @override
      Object registerWorkerPoolListener(void Function(JsonServiceWorker worker, bool removed) listener) => _pool.registerWorkerPoolListener(listener);
    

      @override
      void unregisterWorkerPoolListener({void Function(JsonServiceWorker worker, bool removed)? listener, Object? token}) => _pool.unregisterWorkerPoolListener(listener: listener, token: token);
    

      @override
      Future<T> execute<T>(Future<T> Function(JsonServiceWorker worker) task, {PerfCounter? counter}) => _pool.execute<T>(task, counter: counter);
    

      @override
      StreamTask<T> scheduleStream<T>(Stream<T> Function(JsonServiceWorker worker) task, {PerfCounter? counter}) => _pool.scheduleStream<T>(task, counter: counter);
    

      @override
      ValueTask<T> scheduleTask<T>(Future<T> Function(JsonServiceWorker worker) task, {PerfCounter? counter}) => _pool.scheduleTask<T>(task, counter: counter);
    

      @override
      Stream<T> stream<T>(Stream<T> Function(JsonServiceWorker worker) task, {PerfCounter? counter}) => _pool.stream<T>(task, counter: counter);
    

      @override
      Object get _detachToken => _pool._detachToken;
    
        }
@d-markey
Copy link
Owner

d-markey commented Jun 8, 2023

Hello, I see you've activated the finalization option, could you try with with_finalizers set to false?

@d-markey d-markey self-assigned this Jun 8, 2023
@d-markey d-markey added the bug Something isn't working label Jun 8, 2023
@techouse
Copy link
Contributor Author

techouse commented Jun 8, 2023

Hello, I see you've activated the finalization option, could you try with with_finalizers set to false?

This does work.

d-markey added a commit that referenced this issue Jun 8, 2023
…d worker/worker pool when `with_finalizers` is enabled (fix for #5)
@d-markey
Copy link
Owner

d-markey commented Jun 8, 2023

I've pushed a fix on GitHub, before I publish an updated package, would you mind adding a git reference to squadron_builder in your package's dependency_overrides and test again?

@techouse
Copy link
Contributor Author

techouse commented Jun 8, 2023

I've pushed a fix on GitHub, before I publish an updated package, would you mind adding a git reference to squadron_builder in your package's dependency_overrides and test again?

using

dependency_overrides:
  squadron_builder:
    git:
      url: [email protected]:d-markey/squadron_builder.git
      ref: a20b0a99e75f8df0ac54b1b2075c9fcb58faee4c

and with_finalizers: true works OK ✅

@d-markey
Copy link
Owner

d-markey commented Jun 8, 2023

I ended up refactoring the code to better manage service/method parameters. Lots of changes but I just couldn't leave the code as it was --plain awful.

If you confirm it still works via dependency_overrides, I'll publish a proper version to pub.dev.

@techouse
Copy link
Contributor Author

techouse commented Jun 9, 2023

Tested OK ✅ with

dependency_overrides:
  squadron_builder:
    git:
      url: [email protected]:d-markey/squadron_builder.git
      ref: fd4124c122c5ea6c9eabe8af09e9adbeb0c54cfa

and

squadron_builder:worker_builder:
  options:
    with_finalizers: true
    serialization_type: List

as well as

squadron_builder:worker_builder:
  options:
    with_finalizers: true
    #serialization_type: List

@d-markey
Copy link
Owner

d-markey commented Jun 9, 2023

Thanks, there were still a few corner-cases I had to handle so I pushed some more changes. I'll publish a new version later during the week-end I think.

@d-markey
Copy link
Owner

Fixed and published!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants