1 /**
2 Copyright: Copyright (c) 2019, Joakim Brännström. All rights reserved.
3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 */
6 module distssh.types;
7 
8 import core.thread : Thread;
9 import std.datetime : SysTime, dur;
10 
11 public import my.path : Path;
12 
13 immutable globalEnvHostKey = "DISTSSH_HOSTS";
14 immutable globalEnvFileKey = "DISTSSH_IMPORT_ENV";
15 immutable globalEnvFilterKey = "DISTSSH_ENV_EXPORT_FILTER";
16 immutable globalEnvPurge = "DISTSSH_AUTO_PURGE";
17 immutable globalEnvPurgeWhiteList = "DISTSSH_PURGE_WLIST";
18 immutable distShell = "distshell";
19 immutable distCmd = "distcmd";
20 immutable distsshEnvExport = "distssh_env.export";
21 immutable ulong defaultTimeout_s = 2;
22 /// Number of the top X candidates to choose a server from to put the work on.
23 immutable topCandidades = 3;
24 
25 @safe:
26 
27 struct HostLoad {
28     Host host;
29     Load load;
30     /// When the host was last updated
31     SysTime updated;
32 }
33 
34 struct Host {
35     string payload;
36     alias payload this;
37 }
38 
39 /// The load of a host.
40 struct Load {
41     import std.datetime : Duration;
42 
43     double loadAvg = int.max;
44     Duration accessTime = 1.dur!"hours";
45     bool unknown = true;
46 
47     bool opEquals(const typeof(this) o) nothrow @safe pure @nogc {
48         if (unknown && o.unknown)
49             return true;
50         return loadAvg == o.loadAvg && accessTime == o.accessTime;
51     }
52 
53     int opCmp(const typeof(this) o) pure @safe @nogc nothrow {
54         if (unknown && o.unknown)
55             return 0;
56         else if (unknown)
57             return 1;
58         else if (o.unknown)
59             return -1;
60 
61         if (loadAvg < o.loadAvg)
62             return -1;
63         else if (loadAvg > o.loadAvg)
64             return 1;
65 
66         if (accessTime < o.accessTime)
67             return -1;
68         else if (accessTime > o.accessTime)
69             return 1;
70 
71         return this == o ? 0 : 1;
72     }
73 }
74 
75 @("shall sort the loads")
76 unittest {
77     import std.algorithm : sort;
78     import std.array : array;
79     import core.time : dur;
80     import std.conv : to;
81 
82     {
83         auto raw = [
84             Load(0.6, 500.dur!"msecs", false), Load(0.5, 500.dur!"msecs", false)
85         ].sort.array;
86         assert(raw[0].loadAvg == 0.5, raw[0].to!string);
87     }
88 
89     {
90         auto raw = [
91             Load(0.5, 600.dur!"msecs", false), Load(0.5, 500.dur!"msecs", false)
92         ].sort.array;
93         assert(raw[0].accessTime == 500.dur!"msecs");
94     }
95 }
96 
97 /// Mirror of an environment.
98 struct Env {
99     string[string] payload;
100     alias payload this;
101 }