-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.fs
81 lines (66 loc) · 2.6 KB
/
Program.fs
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
open System
open System.Text
open System.Threading.Tasks
open Azure.Messaging.ServiceBus
open Argu
open Types
open Attendee
open Configuration
let receiveHandler (args: ProcessMessageEventArgs) =
task {
let api = Encoding.UTF8.GetString(args.Message.Body)
let! attendee = getAttendee api
printfn "%A %b" attendee (Printer.print attendee)
do! args.CompleteMessageAsync(args.Message);
} :> Task
let exceptionReceivedhandler (args: ProcessErrorEventArgs) =
task {
printfn "*** %A" args.Exception
} :> Task
let server () =
task {
use client = new ServiceBusClient(cfg.ServiceBusConnection);
use processor = client.CreateProcessor(cfg.ServiceBusQueue, ServiceBusProcessorOptions(AutoCompleteMessages = false, MaxConcurrentCalls = 1))
processor.add_ProcessMessageAsync receiveHandler
processor.add_ProcessErrorAsync exceptionReceivedhandler
do! processor.StartProcessingAsync()
printfn "Waiting for icoming events. Press <Enter> to quit..."
Console.ReadLine() |> ignore
}
[<CliPrefix(CliPrefix.Dash)>]
type PrintArgs =
| [<AltCommandLine("-f")>] FirstName of string
| [<AltCommandLine("-l")>] LastName of string
| [<AltCommandLine("-t")>] TicketType of string option
interface IArgParserTemplate with
member this.Usage =
match this with
| FirstName _ -> "First Name"
| LastName _ -> "Last Name"
| TicketType _ -> "Ticket Type"
and PrintServerArgs =
| [<CliPrefix(CliPrefix.None)>] Server
| [<CliPrefix(CliPrefix.None)>] Print of ParseResults<PrintArgs>
interface IArgParserTemplate with
member this.Usage =
match this with
| Server -> "Listener mode"
| Print _ -> "Print mode"
let parser = ArgumentParser.Create<PrintServerArgs>(programName = "print-server.exe")
[<EntryPoint>]
let main argv =
try
let res = parser.ParseCommandLine(inputs = argv, raiseOnUsage = true)
let arg = res.GetAllResults()
match arg with
| [Server] -> server().Wait()
| [Print printArgs] ->
let attendee = {
FirstName = printArgs.GetResult(FirstName)
LastName = printArgs.GetResult(LastName)
Ticket = printArgs.GetResult(TicketType)
}
printfn "%A %b" attendee (Printer.print attendee)
| _ -> printfn "%s" (parser.PrintUsage())
0
with :? ArguParseException as e -> eprintfn "%s" e.Message; 1