Disable orleans reminder for deploy and add whitelisted addresses
This commit is contained in:
@@ -238,7 +238,7 @@ builder.Services.AddScoped<IJwtUtils, JwtUtils>();
|
||||
|
||||
builder.Services.RegisterApiDependencies(builder.Configuration);
|
||||
|
||||
// Orleans Configuration
|
||||
// Orleans is always configured, but grains can be controlled
|
||||
builder.Host.ConfigureOrleans(builder.Configuration, builder.Environment.IsProduction());
|
||||
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
|
||||
116
src/Managing.Api/README-ORLEANS-CONFIGURATION.md
Normal file
116
src/Managing.Api/README-ORLEANS-CONFIGURATION.md
Normal file
@@ -0,0 +1,116 @@
|
||||
# Orleans Configuration
|
||||
|
||||
This document explains how to configure Orleans usage in the Managing API.
|
||||
|
||||
## Overview
|
||||
|
||||
The Managing API now always runs Orleans for infrastructure, but supports configurable grain execution through the `RunOrleansGrains` configuration option. This allows you to control whether Orleans grains (bots, timers, reminders) are active during runtime.
|
||||
|
||||
## Configuration Options
|
||||
|
||||
### 1. Configuration File (appsettings.json)
|
||||
|
||||
Add the Orleans grain control parameter to your appsettings file:
|
||||
|
||||
```json
|
||||
{
|
||||
"RunOrleansGrains": true
|
||||
}
|
||||
```
|
||||
|
||||
- `RunOrleansGrains`: Boolean value that controls whether Orleans grains (bots, timers, reminders) are active
|
||||
- `true` (default): Grains are active and can run timers/reminders
|
||||
- `false`: Grains are inactive, no timers or reminders will execute
|
||||
|
||||
**Note**: Orleans infrastructure is always enabled and configured. This flag only controls grain execution.
|
||||
|
||||
### 2. Environment Variables
|
||||
|
||||
You can also control Orleans grain execution through environment variables:
|
||||
|
||||
```bash
|
||||
# Enable Orleans grains (bots, timers, reminders)
|
||||
export RUN_ORLEANS_GRAINS=true
|
||||
|
||||
# Disable Orleans grains (infrastructure still runs)
|
||||
export RUN_ORLEANS_GRAINS=false
|
||||
```
|
||||
|
||||
**Note**: Environment variables take precedence over configuration file settings.
|
||||
|
||||
## Configuration Files
|
||||
|
||||
The following configuration files have been updated with Orleans grain control settings:
|
||||
|
||||
- `appsettings.json` - Base configuration (RunOrleansGrains: true)
|
||||
- `appsettings.Development.json` - Development configuration (RunOrleansGrains: false)
|
||||
- `appsettings.Oda.json` - Oda environment (RunOrleansGrains: true)
|
||||
- `appsettings.Oda-docker.json` - Oda Docker environment (RunOrleansGrains: true)
|
||||
- `appsettings.Sandbox.json` - Sandbox environment (RunOrleansGrains: true)
|
||||
- `appsettings.SandboxLocal.json` - Local Sandbox environment (RunOrleansGrains: true)
|
||||
- `appsettings.Production.json` - Production environment (RunOrleansGrains: true)
|
||||
|
||||
## Use Cases
|
||||
|
||||
### Development Environment
|
||||
- Set `RunOrleansGrains: false` to run Orleans infrastructure without active grains
|
||||
- Useful for development and testing without bot execution overhead
|
||||
|
||||
### Testing Environment
|
||||
- Set `RunOrleansGrains: false` to test Orleans infrastructure without running bots
|
||||
- Useful for integration testing without triggering actual trading operations
|
||||
|
||||
### Production Environment
|
||||
- Set `RunOrleansGrains: true` to enable full Orleans grain functionality
|
||||
- Required for production trading bot operations
|
||||
|
||||
### Docker/Container Environment
|
||||
- Use environment variables for easy configuration
|
||||
- Example: `docker run -e RUN_ORLEANS_GRAINS=false ...`
|
||||
|
||||
## Implementation Details
|
||||
|
||||
The Orleans configuration is implemented in:
|
||||
|
||||
1. **ApiBootstrap.cs** - Orleans configuration logic
|
||||
2. **Program.cs** - Application startup Orleans configuration
|
||||
|
||||
## Security Considerations
|
||||
|
||||
- Orleans configuration is not sensitive and can be included in configuration files
|
||||
- Environment variables provide runtime flexibility without code changes
|
||||
- Default behavior maintains backward compatibility (Orleans enabled by default)
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
### Orleans Not Starting
|
||||
1. Orleans infrastructure is always enabled
|
||||
2. Ensure PostgreSQL connection string for Orleans is configured
|
||||
3. Check application logs for connection errors
|
||||
|
||||
### Grains Not Running
|
||||
1. Check `RunOrleansGrains` configuration value
|
||||
2. Verify environment variable `RUN_ORLEANS_GRAINS` is not set to `false`
|
||||
3. Ensure Orleans infrastructure is running properly
|
||||
|
||||
### Configuration Not Applied
|
||||
1. Verify configuration file syntax
|
||||
2. Check environment variable spelling (`RUN_ORLEANS_GRAINS`)
|
||||
3. Restart the application after configuration changes
|
||||
|
||||
## Migration
|
||||
|
||||
Existing deployments will continue to work as Orleans grains are enabled by default. To control Orleans grain behavior:
|
||||
|
||||
### Disable Grains (Keep Orleans Infrastructure)
|
||||
1. Add `"RunOrleansGrains": false` to your appsettings file, or
|
||||
2. Set environment variable `RUN_ORLEANS_GRAINS=false`
|
||||
|
||||
### Development/Testing Setup
|
||||
```json
|
||||
{
|
||||
"RunOrleansGrains": false
|
||||
}
|
||||
```
|
||||
|
||||
**Note**: Orleans infrastructure is always enabled and cannot be disabled.
|
||||
9
src/Managing.Api/appsettings.Development.json
Normal file
9
src/Managing.Api/appsettings.Development.json
Normal file
@@ -0,0 +1,9 @@
|
||||
{
|
||||
"RunOrleansGrains": false,
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,5 +30,6 @@
|
||||
"RequestsChannelId": 1018589494968078356,
|
||||
"ButtonExpirationMinutes": 10
|
||||
},
|
||||
"RunOrleansGrains": true,
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -30,5 +30,6 @@
|
||||
"RequestsChannelId": 1018589494968078356,
|
||||
"ButtonExpirationMinutes": 2
|
||||
},
|
||||
"RunOrleansGrains": true,
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -37,6 +37,7 @@
|
||||
"RequestsChannelId": 1018589494968078356,
|
||||
"ButtonExpirationMinutes": 2
|
||||
},
|
||||
"RunOrleansGrains": true,
|
||||
"AllowedHosts": "*",
|
||||
"WorkerBotManager": true,
|
||||
"WorkerBalancesTracking": false,
|
||||
|
||||
@@ -30,6 +30,7 @@
|
||||
"Sentry": {
|
||||
"Dsn": "https://698e00d7cb404b049aff3881e5a47f6b@bugcenter.apps.managing.live/1"
|
||||
},
|
||||
"RunOrleansGrains": true,
|
||||
"AllowedHosts": "*",
|
||||
"WorkerBotManager": true,
|
||||
"WorkerBalancesTracking": true,
|
||||
|
||||
@@ -35,5 +35,6 @@
|
||||
"Sentry": {
|
||||
"Dsn": "https://698e00d7cb404b049aff3881e5a47f6b@bugcenter.apps.managing.live/1"
|
||||
},
|
||||
"RunOrleansGrains": true,
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -24,6 +24,7 @@
|
||||
"ElasticConfiguration": {
|
||||
"Uri": "http://elasticsearch:9200"
|
||||
},
|
||||
"RunOrleansGrains": true,
|
||||
"AllowedHosts": "*",
|
||||
"WorkerBotManager": false,
|
||||
"WorkerBalancesTracking": false
|
||||
|
||||
@@ -66,5 +66,6 @@
|
||||
"FundingRateChannelId": 1263566138709774336,
|
||||
"ButtonExpirationMinutes": 10
|
||||
},
|
||||
"RunOrleansGrains": true,
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using Managing.Application.Abstractions.Services;
|
||||
using Managing.Common;
|
||||
using Managing.Domain.Accounts;
|
||||
using Managing.Domain.Users;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Managing.Application.Users;
|
||||
@@ -17,20 +18,7 @@ public class UserService : IUserService
|
||||
private readonly ILogger<UserService> _logger;
|
||||
private readonly ICacheService _cacheService;
|
||||
private readonly IGrainFactory _grainFactory;
|
||||
|
||||
private string[] authorizedAddresses =
|
||||
[
|
||||
"0x6781920674dA695aa5120d95D80c4B1788046806", // Macbook
|
||||
"0xA2B43AFF0992a47838DF2e6099A8439981f0B717", // Phone
|
||||
"0xAD4bcf258852e9d47E580798d312E1a52D59E721", // Razil
|
||||
"0xAd6D6c80910096b40e45690506a9f1052e072dCB", // Teru
|
||||
"0x309b9235edbe1C6f840816771c6C21aDa6c275EE", // Cowchain
|
||||
"0x23AA99254cfaA2c374bE2bA5B55C68018cCdFCb3", // Local optiflex
|
||||
"0x932167388dD9aad41149b3cA23eBD489E2E2DD78", // Embedded wallet
|
||||
"0x66CB57Fe3f53cE57376421106dFDa2D39186cBd0", // Embedded wallet optiflex
|
||||
"0x7baBf95621f22bEf2DB67E500D022Ca110722FaD", // DevCowchain
|
||||
"0xc8bC497534d0A43bAb2BBA9BA94d46D9Ddfaea6B" // DevCowchain2
|
||||
];
|
||||
private readonly string[] _authorizedAddresses;
|
||||
|
||||
public UserService(
|
||||
IEvmManager evmManager,
|
||||
@@ -38,7 +26,8 @@ public class UserService : IUserService
|
||||
IAccountService accountService,
|
||||
ILogger<UserService> logger,
|
||||
ICacheService cacheService,
|
||||
IGrainFactory grainFactory)
|
||||
IGrainFactory grainFactory,
|
||||
IConfiguration configuration)
|
||||
{
|
||||
_evmManager = evmManager;
|
||||
_userRepository = userRepository;
|
||||
@@ -46,6 +35,11 @@ public class UserService : IUserService
|
||||
_logger = logger;
|
||||
_cacheService = cacheService;
|
||||
_grainFactory = grainFactory;
|
||||
|
||||
var authorizedAddressesString = configuration["AUTHORIZED_ADDRESSES"] ?? string.Empty;
|
||||
_authorizedAddresses = string.IsNullOrEmpty(authorizedAddressesString)
|
||||
? Array.Empty<string>()
|
||||
: authorizedAddressesString.Split(';', StringSplitOptions.RemoveEmptyEntries);
|
||||
}
|
||||
|
||||
public async Task<User> Authenticate(string name, string address, string message, string signature)
|
||||
@@ -60,11 +54,11 @@ public class UserService : IUserService
|
||||
$"Message not good : {message} - Address : {address} - User : {name} - Signature : {signature}");
|
||||
}
|
||||
|
||||
// if (!authorizedAddresses.Contains(recoveredAddress))
|
||||
// {
|
||||
// _logger.LogWarning($"Address {recoveredAddress} not authorized");
|
||||
// throw new Exception("Address not authorized");
|
||||
// }
|
||||
if (!_authorizedAddresses.Contains(recoveredAddress.ToLower()))
|
||||
{
|
||||
_logger.LogWarning($"Address {recoveredAddress} not authorized");
|
||||
throw new Exception("Address not authorized");
|
||||
}
|
||||
|
||||
if (recoveredAddress == null || !recoveredAddress.Equals(address))
|
||||
{
|
||||
|
||||
@@ -75,6 +75,19 @@ public static class ApiBootstrap
|
||||
public static IHostBuilder ConfigureOrleans(this IHostBuilder hostBuilder, IConfiguration configuration,
|
||||
bool isProduction)
|
||||
{
|
||||
// Check if Orleans grains should be active using root-level configuration
|
||||
var runOrleansGrains = configuration.GetValue<bool>("RunOrleansGrains", true);
|
||||
|
||||
// Check environment variable as override
|
||||
var runOrleansGrainsEnv = Environment.GetEnvironmentVariable("RUN_ORLEANS_GRAINS");
|
||||
|
||||
if (!string.IsNullOrEmpty(runOrleansGrainsEnv) &&
|
||||
bool.TryParse(runOrleansGrainsEnv, out var runOrleansGrainsFromEnv))
|
||||
{
|
||||
runOrleansGrains = runOrleansGrainsFromEnv;
|
||||
}
|
||||
|
||||
|
||||
var postgreSqlConnectionString = configuration.GetSection("PostgreSql")["Orleans"];
|
||||
|
||||
return hostBuilder.UseOrleans(siloBuilder =>
|
||||
@@ -85,12 +98,17 @@ public static class ApiBootstrap
|
||||
{
|
||||
options.ConnectionString = postgreSqlConnectionString;
|
||||
options.Invariant = "Npgsql";
|
||||
})
|
||||
.UseAdoNetReminderService(options =>
|
||||
});
|
||||
|
||||
// Conditionally configure reminder service based on flag
|
||||
if (runOrleansGrains)
|
||||
{
|
||||
siloBuilder.UseAdoNetReminderService(options =>
|
||||
{
|
||||
options.ConnectionString = postgreSqlConnectionString;
|
||||
options.Invariant = "Npgsql";
|
||||
});
|
||||
}
|
||||
|
||||
// Configure networking for better silo communication
|
||||
siloBuilder
|
||||
@@ -101,18 +119,33 @@ public static class ApiBootstrap
|
||||
options.ServiceId = "ManagingApp";
|
||||
options.ClusterId = configuration["ASPNETCORE_ENVIRONMENT"] ?? "Development";
|
||||
})
|
||||
.Configure<GrainCollectionOptions>(options =>
|
||||
{
|
||||
// Configure grain collection to prevent memory issues
|
||||
options.CollectionAge = TimeSpan.FromMinutes(10);
|
||||
options.CollectionQuantum = TimeSpan.FromMinutes(1);
|
||||
})
|
||||
.Configure<MessagingOptions>(options =>
|
||||
{
|
||||
// Configure messaging for better reliability
|
||||
options.ResponseTimeout = TimeSpan.FromSeconds(30);
|
||||
});
|
||||
|
||||
// Conditionally configure grain execution based on flag
|
||||
if (runOrleansGrains)
|
||||
{
|
||||
siloBuilder.Configure<GrainCollectionOptions>(options =>
|
||||
{
|
||||
// Enable grain collection for active grains
|
||||
options.CollectionAge = TimeSpan.FromMinutes(10);
|
||||
options.CollectionQuantum = TimeSpan.FromMinutes(1);
|
||||
});
|
||||
}
|
||||
else
|
||||
{
|
||||
// Disable grain execution completely
|
||||
siloBuilder.Configure<GrainCollectionOptions>(options =>
|
||||
{
|
||||
// Disable grain collection to prevent grains from running
|
||||
options.CollectionAge = TimeSpan.FromDays(365); // Very long collection age
|
||||
options.CollectionQuantum = TimeSpan.FromDays(1); // Very long quantum
|
||||
});
|
||||
}
|
||||
|
||||
siloBuilder
|
||||
.ConfigureLogging(logging => logging.SetMinimumLevel(LogLevel.Information));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user