gorealis/job.go

220 lines
6.3 KiB
Go
Raw Normal View History

2016-08-02 11:42:00 -07:00
/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
2016-08-02 11:42:00 -07:00
package realis
import (
"gen-go/apache/aurora"
"strconv"
)
// Structure to collect all information pertaining to an Aurora job.
2016-08-02 11:42:00 -07:00
type Job struct {
jobConfig *aurora.JobConfiguration
numCpus *aurora.Resource
ramMb *aurora.Resource
diskMb *aurora.Resource
portCount int
}
// Create a Job object with everything initialized.
2016-08-02 11:42:00 -07:00
func NewJob() *Job {
jobConfig := aurora.NewJobConfiguration()
taskConfig := aurora.NewTaskConfig()
jobKey := aurora.NewJobKey()
//Job Config
jobConfig.Key = jobKey
jobConfig.TaskConfig = taskConfig
//Task Config
taskConfig.Job = jobKey
taskConfig.Container = aurora.NewContainer()
taskConfig.Container.Mesos = aurora.NewMesosContainer()
taskConfig.ExecutorConfig = aurora.NewExecutorConfig()
taskConfig.MesosFetcherUris = make(map[*aurora.MesosFetcherURI]bool)
taskConfig.Metadata = make(map[*aurora.Metadata]bool)
taskConfig.Constraints = make(map[*aurora.Constraint]bool)
//Resources
numCpus := aurora.NewResource()
ramMb := aurora.NewResource()
diskMb := aurora.NewResource()
taskConfig.Resources = make(map[*aurora.Resource]bool)
taskConfig.Resources[numCpus] = true
taskConfig.Resources[ramMb] = true
taskConfig.Resources[diskMb] = true
return &Job{jobConfig, numCpus, ramMb, diskMb, 0}
}
// Set Job Key environment.
func (a *Job) Environment(env string) *Job {
2016-08-02 11:42:00 -07:00
a.jobConfig.Key.Environment = env
return a
}
// Set Job Key Role.
func (a *Job) Role(role string) *Job {
2016-08-02 11:42:00 -07:00
a.jobConfig.Key.Role = role
//Will be deprecated
identity := &aurora.Identity{role}
a.jobConfig.Owner = identity
a.jobConfig.TaskConfig.Owner = identity
return a
}
// Set Job Key Name.
func (a *Job) Name(name string) *Job {
2016-08-02 11:42:00 -07:00
a.jobConfig.Key.Name = name
return a
}
// Set name of the executor that will the task will be configured to.
func (a *Job) ExecutorName(name string) *Job {
2016-08-02 11:42:00 -07:00
a.jobConfig.TaskConfig.ExecutorConfig.Name = name
return a
}
// Will be included as part of entire task inside the scheduler that will be serialized.
func (a *Job) ExecutorData(data string) *Job {
2016-08-02 11:42:00 -07:00
a.jobConfig.TaskConfig.ExecutorConfig.Data = data
return a
}
2016-08-02 13:06:36 -07:00
func (a *Job) CPU(cpus float64) *Job {
2016-08-02 11:42:00 -07:00
a.numCpus.NumCpus = &cpus
a.jobConfig.TaskConfig.NumCpus = cpus //Will be deprecated soon
return a
}
2016-08-09 13:31:15 -07:00
func (a *Job) RAM(ram int64) *Job {
2016-08-02 11:42:00 -07:00
a.ramMb.RamMb = &ram
a.jobConfig.TaskConfig.RamMb = ram //Will be deprecated soon
return a
}
func (a *Job) Disk(disk int64) *Job {
2016-08-02 11:42:00 -07:00
a.diskMb.DiskMb = &disk
a.jobConfig.TaskConfig.DiskMb = disk //Will be deprecated
return a
}
// How many failures to tolerate before giving up.
func (a *Job) MaxFailure(maxFail int32) *Job {
2016-08-02 11:42:00 -07:00
a.jobConfig.TaskConfig.MaxTaskFailures = maxFail
return a
}
// How many instances of the job to run
func (a *Job) InstanceCount(instCount int32) *Job {
2016-08-02 11:42:00 -07:00
a.jobConfig.InstanceCount = instCount
return a
}
// Restart the job's tasks if they fail
func (a *Job) IsService(isService bool) *Job {
2016-08-02 11:42:00 -07:00
a.jobConfig.TaskConfig.IsService = isService
return a
}
// Get the current job configurations key to use for some realis calls.
func (a *Job) JobKey() *aurora.JobKey {
2016-08-02 11:42:00 -07:00
return a.jobConfig.Key
}
// Add URI to fetch using the mesos fetcher. Scheduler must have --enable_mesos_fetcher flag
// enabled.
2016-08-02 11:42:00 -07:00
func (a *Job) AddURI(value string, extract bool, cache bool) *Job {
a.jobConfig.TaskConfig.MesosFetcherUris[&aurora.MesosFetcherURI{value, &extract, &cache}] = true
return a
}
// Add a list of URIs with the same extract and cache configuration.
2016-08-02 11:42:00 -07:00
func (a *Job) AddURIs(extract bool, cache bool, values ...string) *Job {
for _, value := range values {
a.jobConfig.TaskConfig.MesosFetcherUris[&aurora.MesosFetcherURI{value, &extract, &cache}] = true
}
return a
}
// Adds a Mesos label to the job. Note that as of Aurora 0.15.0, Aurora will add the
// prefix "org.apache.aurora.metadata." to the beginning of each key.
2016-08-02 11:42:00 -07:00
func (a *Job) AddLabel(key string, value string) *Job {
a.jobConfig.TaskConfig.Metadata[&aurora.Metadata{key, value}] = true
return a
}
// Add a named port to the job configuration These are random ports as it's
// not currently possible to request specific ports using Aurora.
func (a *Job) AddNamedPorts(names ...string) *Job {
a.portCount += len(names)
for _, name := range names {
a.jobConfig.TaskConfig.Resources[&aurora.Resource{NamedPort: &name}] = true
}
return a
}
// Adds a request for a number of ports to the job configuration. The names chosen for these ports
// will be org.apache.aurora.portX, where X is the current port count for the job configuration
// starting at 0. These are random ports as it's not currently possible to request
// specific ports using Aurora.
2016-08-02 11:42:00 -07:00
func (a *Job) AddPorts(num int) *Job {
start := a.portCount
a.portCount += num
for i := start; i < a.portCount; i++ {
portName := "gorealis.port" + strconv.Itoa(i)
2016-08-02 11:42:00 -07:00
a.jobConfig.TaskConfig.Resources[&aurora.Resource{NamedPort: &portName}] = true
}
return a
}
// From Aurora Docs:
// Add a Value constraint
2016-08-02 11:42:00 -07:00
// name - Mesos slave attribute that the constraint is matched against.
// If negated = true , treat this as a 'not' - to avoid specific values.
// Values - list of values we look for in attribute name
2016-08-02 11:42:00 -07:00
func (a *Job) AddValueConstraint(name string,
negated bool,
values ...string) *Job {
constraintValues := make(map[string]bool)
for _, value := range values {
constraintValues[value] = true
}
a.jobConfig.TaskConfig.Constraints[&aurora.Constraint{name,
&aurora.TaskConstraint{&aurora.ValueConstraint{negated, constraintValues}, nil}}] = true
return a
}
// From Aurora Docs:
// A constraint that specifies the maximum number of active tasks on a host with
2016-08-02 11:42:00 -07:00
// a matching attribute that may be scheduled simultaneously.
func (a *Job) AddLimitConstraint(name string, limit int32) *Job {
a.jobConfig.TaskConfig.Constraints[&aurora.Constraint{name,
&aurora.TaskConstraint{nil, &aurora.LimitConstraint{limit}}}] = true
return a
}