-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03-worker.fsx
More file actions
58 lines (48 loc) · 1.91 KB
/
03-worker.fsx
File metadata and controls
58 lines (48 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env -S dotnet fsi
// ============================================================================
// F# Worker — StackExchange.Redis
// ============================================================================
// C# 원본(03-worker.cs)은 BackgroundService + AddRedisClient
// F#은 env ConnectionStrings__cache 사용
// ============================================================================
#r "nuget: StackExchange.Redis, 2.8.16"
open System
open System.Threading
open System.Threading.Tasks
open StackExchange.Redis
let connectionString =
Environment.GetEnvironmentVariable("ConnectionStrings__cache")
|> Option.ofObj
|> Option.defaultValue "localhost:6379"
let conn = ConnectionMultiplexer.Connect(connectionString)
let database = conn.GetDatabase()
let keyMessage = RedisKey("Message")
let keyLastUpdated = RedisKey("LastUpdated")
// Message 키가 없으면 설정
let ensureMessage () =
task {
let! exists = database.KeyExistsAsync(keyMessage)
if not exists then
do! database.StringSetAsync(keyMessage, RedisValue("Hello, World!")) :> Task
}
let runLoop (ct: CancellationToken) =
let rec loop () =
async {
let now = DateTimeOffset.Now
let value = "Last updated: " + now.ToString()
do! (database.StringSetAsync(keyLastUpdated, RedisValue(value)) |> Async.AwaitTask) |> Async.Ignore
printfn "Worker running at: %O" now
do! Async.Sleep(1000)
if not ct.IsCancellationRequested then
return! loop ()
}
loop ()
// Message 키 초기화 후 루프
ensureMessage () |> Async.AwaitTask |> Async.RunSynchronously
let run () =
use cts = new CancellationTokenSource()
Console.CancelKeyPress.Add(fun _ -> cts.Cancel())
try
Async.RunSynchronously(runLoop cts.Token, cancellationToken = cts.Token)
with :? OperationCanceledException -> ()
run ()