Skip to content

Commit

Permalink
chore: switched Future.delayed to Timer and implemented jsonserializable
Browse files Browse the repository at this point in the history
  • Loading branch information
khatruong2009 committed Aug 30, 2023
1 parent 8633906 commit 54983b6
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

import 'package:aws_common/aws_common.dart';
import 'package:aws_logging_cloudwatch/aws_logging_cloudwatch.dart';
import 'package:json_annotation/json_annotation.dart';

part 'plugin_config.g.dart';

/// {@template aws_logging_cloudwatch.cloudwatch_logger_plugin_configuration}
/// The configuration for `CloudWatchLoggerPlugin`.
Expand Down Expand Up @@ -47,19 +50,24 @@ class CloudWatchLoggerPluginConfiguration with AWSDebuggable {
/// {@template aws_logging_cloudwatch.logging_constraint}
/// The logging constraint for sending logs to CloudWatch.
/// {@endtemplate}
@JsonSerializable()
class LoggingConstraint with AWSDebuggable {
/// {@macro aws_logging_cloudwatch.logging_constraint}
const LoggingConstraint({this.defaultLogLevel = LogLevel.error});

/// Converts a [Map] to an [LoggingConstraint] instance.
factory LoggingConstraint.fromJson(Map<String, dynamic> json) {
return LoggingConstraint(
defaultLogLevel: LogLevel.values.firstWhere(
(e) => e.toString() == 'LogLevel.${json['defaultLogLevel']}',
orElse: () => LogLevel.error, // Default value if not found
),
);
}
// factory LoggingConstraint.fromJson(Map<String, dynamic> json) {
// return LoggingConstraint(
// defaultLogLevel: LogLevel.values.firstWhere(
// (e) => e.toString() == 'LogLevel.${json['defaultLogLevel']}',
// orElse: () => LogLevel.error, // Default value if not found
// ),
// );
// }

factory LoggingConstraint.fromJson(Map<String, dynamic> json) =>
_$LoggingConstraintFromJson(json);

/// The default [LogLevel] for sending logs to CloudWatch.
final LogLevel defaultLogLevel;
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

import 'dart:async';
import 'dart:convert';
import 'dart:io';

Expand Down Expand Up @@ -49,6 +50,8 @@ class DefaultRemoteLoggingConstraintProvider

static final _logger = AmplifyLogger('default-remote-config');

Timer? _timer;

Future<void> _saveConstraintLocally(LoggingConstraint constraint) async {
final file = File('logging_constraint.json');
await file.writeAsString(jsonEncode(constraint));
Expand Down Expand Up @@ -134,7 +137,10 @@ class DefaultRemoteLoggingConstraintProvider
/// Refreshes the constraint from the endpoint periodically.
Future<void> _refreshConstraintPeriodically() async {
while (true) {
await Future<void>.delayed(_config.refreshIntervalInSeconds);
_timer?.cancel();
_timer = Timer.periodic(_config.refreshIntervalInSeconds, (timer) async {
await _fetchAndCacheConstraintFromEndpoint();
});
await _fetchAndCacheConstraintFromEndpoint();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ dependencies:
fixnum: ^1.1.0
http: ^1.0.0
intl: ">=0.18.0 <1.0.0"
json_annotation: ^4.8.1
meta: ^1.9.1
smithy: ^0.5.0+3
smithy_aws: ^0.5.0+3
Expand Down

0 comments on commit 54983b6

Please sign in to comment.