feat(job):enable priority option (#11608)

- priority option is supported when doing job registration
- the priority is defined by a unique priority sampler
- the default priority is 1000 (max is 10000)

Signed-off-by: Steven Zou <szou@vmware.com>
This commit is contained in:
Steven Zou 2020-04-14 14:54:44 +08:00 committed by GitHub
parent fb3da503ea
commit adb305e4e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 107 additions and 0 deletions

View File

@ -0,0 +1,56 @@
// Copyright Project Harbor Authors
//
// 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.
package job
const (
defaultPriority uint = 1000
)
// PrioritySampler define the job priority generation method
type PrioritySampler interface {
// Priority for the given job.
// Job with high priority has the more probabilities to execute.
// e.g.:
// always process X jobs before Y jobs if priorityX > priority Y
//
// Arguments:
// job string: the job type
//
// Returns:
// uint: the priority value (between 1 and 10000)
For(job string) uint
}
// defaultSampler is default implementation of PrioritySampler
type defaultSampler struct{}
// For the given job
func (ps *defaultSampler) For(job string) uint {
switch job {
// As an example, sample job has the lowest priority
case SampleJob:
return 1
// add more cases here if specified job priority is required
// case XXX:
// return 2000
default:
return defaultPriority
}
}
// Priority returns the default job priority sampler implementation.
func Priority() PrioritySampler {
return &defaultSampler{}
}

View File

@ -0,0 +1,50 @@
// Copyright Project Harbor Authors
//
// 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.
package job
import (
"testing"
"github.com/stretchr/testify/suite"
)
// PrioritySamplerSuite is test suite for PrioritySampler.
type PrioritySamplerSuite struct {
suite.Suite
sampler *defaultSampler
}
// TestPrioritySampler is entry point of PrioritySamplerSuite.
func TestPrioritySampler(t *testing.T) {
suite.Run(t, &PrioritySamplerSuite{})
}
// SetupSuite prepares the testing env
func (suite *PrioritySamplerSuite) SetupSuite() {
suite.sampler = &defaultSampler{}
}
// Test for method
func (suite *PrioritySamplerSuite) Test() {
p1 := suite.sampler.For(SampleJob)
suite.Equal((uint)(1), p1, "Job priority for %s", SampleJob)
p2 := suite.sampler.For(Retention)
suite.Equal(defaultPriority, p2, "Job priority for %s", Retention)
p3 := suite.sampler.For(Replication)
suite.Equal(defaultPriority, p3, "Job priority for %s", Replication)
}

View File

@ -412,6 +412,7 @@ func (w *basicWorker) registerJob(name string, j interface{}) (err error) {
work.JobOptions{
MaxFails: theJ.MaxFails(),
MaxConcurrency: theJ.MaxCurrency(),
Priority: job.Priority().For(name),
SkipDead: true,
},
// Use generic handler to handle as we do not accept context with this way.