-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Worker architecture capability added #44
Open
gopito
wants to merge
1
commit into
avito-tech:master
Choose a base branch
from
gopito:WorkerCapabilityArchitecture
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
90 changes: 90 additions & 0 deletions
90
Sources/WorkerCapabilities/ConcreteCapabilities/WorkerArchitectureCapabilitiesProvider.swift
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,90 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by a.smolianin on 15.07.2022. | ||
// | ||
|
||
import Foundation | ||
import WorkerCapabilitiesModels | ||
import ProcessController | ||
import FileSystem | ||
import DateProvider | ||
import EmceeLogging | ||
|
||
public final class WorkerArchitectureCapabilitiesProvider: WorkerCapabilitiesProvider { | ||
private let logger: ContextualLogger | ||
private let commandExecutor: CommandExecutor | ||
|
||
public init(logger: ContextualLogger, commandExecutor: CommandExecutor) { | ||
self.logger = logger | ||
self.commandExecutor = commandExecutor | ||
} | ||
|
||
convenience public init(logger: ContextualLogger) { | ||
self.init(logger: logger, commandExecutor: DefaultComandExecutor()) | ||
} | ||
|
||
public func workerCapabilities() -> Set<WorkerCapability> { | ||
return Set([WorkerCapability(name: WorkerCapabilityName("emcee.arch"), value: "\(getArch())")]) | ||
} | ||
|
||
private func getArch() -> Arch { | ||
var result = "\(Arch.unknown)" | ||
do { | ||
result = try commandExecutor.executeCommandWithOutput(env: [String: String](), arguments: ["/usr/sbin/sysctl", "-n", "machdep.cpu.brand_string"]) | ||
logger.info("Command \"sysctl -n machdep.cpu.brand_string\" = \(result)") | ||
} catch { | ||
logger.error("Command \"sysctl -n machdep.cpu.brand_string\" could not be completed. Error: \(error.localizedDescription)") | ||
return .unknown | ||
} | ||
switch result { | ||
case let arch where arch.contains(Arch.m1.rawValue): | ||
return .m1 | ||
case let arch where arch.contains(Arch.x86.rawValue): | ||
return .x86 | ||
default: | ||
return .unknown | ||
} | ||
} | ||
|
||
enum Arch: String { | ||
case m1 = "Apple M1" | ||
case x86 = "Intel" | ||
case unknown //for m2 .etc | ||
} | ||
} | ||
|
||
public protocol CommandExecutor { | ||
func executeCommandWithOutput(env: Dictionary<String,String>, arguments: [String]) throws -> String | ||
} | ||
|
||
extension CommandExecutor { | ||
public func executeCommandWithOutput(env: Dictionary<String,String>, arguments: [String]) throws -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is better to use |
||
let dateProvider = SystemDateProvider() | ||
let filePropertiesProvider = FilePropertiesProviderImpl() | ||
let controller = try DefaultProcessController( | ||
dateProvider: dateProvider, | ||
filePropertiesProvider: filePropertiesProvider, | ||
|
||
subprocess: Subprocess( | ||
arguments: arguments, | ||
environment: Environment(env) | ||
) | ||
) | ||
|
||
var stdoutData = Data() | ||
controller.onStdout { _, data, _ in stdoutData.append(contentsOf: data) } | ||
|
||
try controller.startAndListenUntilProcessDies() | ||
guard let stdOut = String(data: stdoutData, encoding: .utf8) else { | ||
return "" | ||
} | ||
return stdOut | ||
} | ||
} | ||
|
||
class DefaultComandExecutor: CommandExecutor { } | ||
|
||
|
||
|
47 changes: 47 additions & 0 deletions
47
Tests/WorkerCapabilitiesTests/WorkerArchitectureCapabilitiesProviderTests.swift
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,47 @@ | ||
// | ||
// File.swift | ||
// | ||
// | ||
// Created by a.smolianin on 25.08.2022. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
import WorkerCapabilities | ||
import WorkerCapabilitiesModels | ||
|
||
|
||
class WorkerArchitectureCapabilitiesProviderTests: XCTestCase { | ||
func test___x86_architecture() throws { | ||
class IntelXeon: CommandExecutor { | ||
func executeCommandWithOutput(env: Dictionary<String,String>, arguments: [String]) throws -> String { | ||
return "Intel(R) Xeon(R) W-3245 CPU @ 3.20GHz" | ||
} | ||
} | ||
|
||
let archProvider = WorkerArchitectureCapabilitiesProvider(logger: .noOp, commandExecutor: IntelXeon()) | ||
XCTAssertEqual(Set([WorkerCapability(name: WorkerCapabilityName("emcee.arch"), value: "x86")]), archProvider.workerCapabilities()) | ||
} | ||
|
||
func test___m1_architecture() throws { | ||
class M1: CommandExecutor { | ||
func executeCommandWithOutput(env: Dictionary<String,String>, arguments: [String]) throws -> String { | ||
return "Apple M1" | ||
} | ||
} | ||
|
||
let archProvider = WorkerArchitectureCapabilitiesProvider(logger: .noOp, commandExecutor: M1()) | ||
XCTAssertEqual(Set([WorkerCapability(name: WorkerCapabilityName("emcee.arch"), value: "m1")]), archProvider.workerCapabilities()) | ||
} | ||
|
||
func test___unknown_architecture() throws { | ||
class Unknown: CommandExecutor { | ||
func executeCommandWithOutput(env: Dictionary<String,String>, arguments: [String]) throws -> String { | ||
return "Something unknown" | ||
} | ||
} | ||
|
||
let archProvider = WorkerArchitectureCapabilitiesProvider(logger: .noOp, commandExecutor: Unknown()) | ||
XCTAssertEqual(Set([WorkerCapability(name: WorkerCapabilityName("emcee.arch"), value: "unknown")]), archProvider.workerCapabilities()) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you want to expose CPU arch (x86_64, arm64), or CPU type, or CPU manufacturer? It seems currently these all are messed up a bit.
I assume it is more beneficial to have arch rather than manufacturer or kind (i.e. Core i5 or i7 or M1 or M2 Pro vs x86_64 and arm64). Could you please update your PR to expose architecture only?