Search code examples
swiftclient-servervaporserver-side-swift

Access a service from a Job in Vapor 3


I'm very new to Vapor and server-side swift, with a background in iOS development (6 years developing apps). I'm trying to build a simple API for device management which amongst other things includes a "job" (Timer whatever you want to call it) which checks every hour or so for the last time a device "Checked In".

To do this I've setup the Jobs package, however I'm struggling to work out how I can access a service (Ferno) to hit my Firebase Realtime database to get info about when devices last logged in. It seems that according to the docs you need an implementer of Container to create/access a Service but there doesn't seem to be any kind of global container I can use.

Should I be attempting to create my own Request just to access a Ferno service? Or is there a way I can access the global app?

The below is what I have so far, but what I'm doing seems horribly wrong! Surely there must be a way to achieve this?

Jobs.add(interval: (60*30).seconds) { [weak devicesController] in
    devicesController?.checkForNotSeenDevices()
}
func checkForNotSeenDevices() throws {

    let client = try app(.detect()).make(FernoClient.self)
    client.ferno.retrieve(req: ???, queryItems: [], appendedPath: ["devices"])
}

Solution

  • I've struggled with Ferno too.

    Inside boot.swift, use global app as container for ferno client and request.

        let client = try! app.make(FernoClient.self)
        let request = Request(using: app)
    

    Now your client is ready for Database Actions, example post action.Which can be repeated each time using your own Jobs.

        let inputData = try! client.ferno.overwrite(req: request, appendedPath: ["users/john"], body: yourOwnContentModel)