-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from czechboy0/hd/heartbeat
Anonymous Heartbeat Sending
- Loading branch information
Showing
10 changed files
with
446 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// BuildaHeartbeatKit.h | ||
// BuildaHeartbeatKit | ||
// | ||
// Created by Honza Dvorsky on 17/09/2015. | ||
// Copyright © 2015 Honza Dvorsky. All rights reserved. | ||
// | ||
|
||
#import <Cocoa/Cocoa.h> | ||
|
||
//! Project version number for BuildaHeartbeatKit. | ||
FOUNDATION_EXPORT double BuildaHeartbeatKitVersionNumber; | ||
|
||
//! Project version string for BuildaHeartbeatKit. | ||
FOUNDATION_EXPORT const unsigned char BuildaHeartbeatKitVersionString[]; | ||
|
||
// In this header, you should import all the public headers of your framework using statements like #import <BuildaHeartbeatKit/PublicHeader.h> | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// | ||
// Heartbeat.swift | ||
// Buildasaur | ||
// | ||
// Created by Honza Dvorsky on 17/09/2015. | ||
// Copyright © 2015 Honza Dvorsky. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import ekgclient | ||
import BuildaUtils | ||
|
||
public protocol HeartbeatManagerDelegate { | ||
func numberOfRunningSyncers() -> Int | ||
} | ||
|
||
//READ: https://github.com/czechboy0/Buildasaur/tree/master#heartpulse-heartbeat | ||
@objc public class HeartbeatManager: NSObject { | ||
|
||
public var delegate: HeartbeatManagerDelegate? | ||
|
||
private let client: EkgClient | ||
private let creationTime: Double | ||
private var timer: NSTimer? | ||
private let interval: Double = 24 * 60 * 60 //send heartbeat once in 24 hours | ||
|
||
public init(server: String) { | ||
let bundle = NSBundle.mainBundle() | ||
let appIdentifier = EkgClientHelper.pullAppIdentifierFromBundle(bundle) ?? "Unknown app" | ||
let version = EkgClientHelper.pullVersionFromBundle(bundle) ?? "?" | ||
let buildNumber = EkgClientHelper.pullBuildNumberFromBundle(bundle) ?? "?" | ||
let appInfo = AppInfo(appIdentifier: appIdentifier, version: version, build: buildNumber) | ||
let host = NSURL(string: server)! | ||
let serverInfo = ServerInfo(host: host) | ||
let userDefaults = NSUserDefaults.standardUserDefaults() | ||
|
||
self.creationTime = NSDate().timeIntervalSince1970 | ||
let client = EkgClient(userDefaults: userDefaults, appInfo: appInfo, serverInfo: serverInfo) | ||
self.client = client | ||
} | ||
|
||
deinit { | ||
self.stop() | ||
} | ||
|
||
public func start() { | ||
self.sendLaunchedEvent() | ||
self.startSendingHeartbeat() | ||
} | ||
|
||
public func stop() { | ||
self.stopSendingHeartbeat() | ||
} | ||
|
||
private func sendEvent(event: Event) { | ||
|
||
var sendReal = true | ||
#if DEBUG | ||
sendReal = false | ||
#endif | ||
|
||
guard sendReal else { | ||
Log.info("Not sending events in debug environment") | ||
return | ||
} | ||
|
||
Log.info("Sending heartbeat event \(event.jsonify())") | ||
|
||
self.client.sendEvent(event) { | ||
if let error = $0 { | ||
Log.error("Failed to send a heartbeat event. Error \(error)") | ||
} | ||
} | ||
} | ||
|
||
private func sendLaunchedEvent() { | ||
self.sendEvent(LaunchEvent()) | ||
} | ||
|
||
private func sendHeartbeatEvent() { | ||
let uptime = NSDate().timeIntervalSince1970 - self.creationTime | ||
let numberOfRunningSyncers = self.delegate?.numberOfRunningSyncers() ?? 0 | ||
self.sendEvent(HeartbeatEvent(uptime: uptime, numberOfRunningSyncers: numberOfRunningSyncers)) | ||
} | ||
|
||
func _timerFired(timer: NSTimer?=nil) { | ||
self.sendHeartbeatEvent() | ||
} | ||
|
||
private func startSendingHeartbeat() { | ||
|
||
//send once now | ||
self._timerFired() | ||
|
||
self.timer?.invalidate() | ||
self.timer = NSTimer.scheduledTimerWithTimeInterval( | ||
self.interval, | ||
target: self, | ||
selector: "_timerFired:", | ||
userInfo: nil, | ||
repeats: true) | ||
} | ||
|
||
private func stopSendingHeartbeat() { | ||
timer?.invalidate() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
<plist version="1.0"> | ||
<dict> | ||
<key>CFBundleDevelopmentRegion</key> | ||
<string>en</string> | ||
<key>CFBundleExecutable</key> | ||
<string>$(EXECUTABLE_NAME)</string> | ||
<key>CFBundleIdentifier</key> | ||
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string> | ||
<key>CFBundleInfoDictionaryVersion</key> | ||
<string>6.0</string> | ||
<key>CFBundleName</key> | ||
<string>$(PRODUCT_NAME)</string> | ||
<key>CFBundlePackageType</key> | ||
<string>FMWK</string> | ||
<key>CFBundleShortVersionString</key> | ||
<string>1.0</string> | ||
<key>CFBundleSignature</key> | ||
<string>????</string> | ||
<key>CFBundleVersion</key> | ||
<string>$(CURRENT_PROJECT_VERSION)</string> | ||
<key>NSHumanReadableCopyright</key> | ||
<string>Copyright © 2015 Honza Dvorsky. All rights reserved.</string> | ||
<key>NSPrincipalClass</key> | ||
<string></string> | ||
</dict> | ||
</plist> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.