Skip to content

Commit 5221a94

Browse files
committed
Adds job whitelist and worker eligibility
1 parent a3b66a8 commit 5221a94

File tree

7 files changed

+42
-1
lines changed

7 files changed

+42
-1
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DROP table IF EXISTS `whitelists`
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
CREATE TABLE `whitelists` (
2+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
3+
`job_id` int(11) unsigned NOT NULL,
4+
`worker_id` int(11) unsigned NOT NULL,
5+
PRIMARY KEY (`id`),
6+
UNIQUE KEY `job_worker` (`job_id`,`worker_id`)
7+
)

migrations/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2
1+
3

pkg/assignment/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,9 @@ type WorkerNotEnoughFunds struct{}
2929
func (err WorkerNotEnoughFunds) Error() string {
3030
return "Worker doesn't have enough funds"
3131
}
32+
33+
type WorkerNotWhitelisted struct{}
34+
35+
func (err WorkerNotWhitelisted) Error() string {
36+
return "Worker has not been whitelisted"
37+
}

pkg/datastore/assignment.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/gemsorg/assignment/pkg/assignment"
9+
"github.com/gemsorg/assignment/pkg/whitelist"
910
"github.com/jmoiron/sqlx"
1011
)
1112

@@ -14,6 +15,7 @@ type Storage interface {
1415
GetAssignment(id string) (*assignment.Assignment, error)
1516
CreateAssignment(assignment.NewAssignment) (*assignment.Assignment, error)
1617
GetSettings(jobID uint64) (*assignment.Settings, error)
18+
GetWhitelist(jobID uint64, workerID uint64) (*whitelist.Whitelist, error)
1719
}
1820

1921
type AssignmentStore struct {
@@ -105,3 +107,14 @@ func (as *AssignmentStore) GetSettings(jobID uint64) (*assignment.Settings, erro
105107

106108
return set, nil
107109
}
110+
111+
func (as *AssignmentStore) GetWhitelist(jobID uint64, workerID uint64) (*whitelist.Whitelist, error) {
112+
wl := &whitelist.Whitelist{}
113+
err := as.DB.Get(wl, "SELECT * FROM whitelists WHERE job_id = ? AND worker_id = ?", jobID, workerID)
114+
115+
if err != nil {
116+
return nil, err
117+
}
118+
119+
return wl, nil
120+
}

pkg/service/service.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,13 @@ func (s *service) GetAssignment(id string) (*assignment.Assignment, error) {
4545
}
4646

4747
func (s *service) CreateAssignment(a assignment.NewAssignment, set *assignment.Settings) (*assignment.Assignment, error) {
48+
// if job has a whitelist, check if worker is part of it
49+
if set.Whitelist {
50+
wl, err := s.store.GetWhitelist(a.JobID, a.WorkerID)
51+
if wl == nil || err != nil {
52+
return nil, assignment.WorkerNotWhitelisted{}
53+
}
54+
}
4855
// Check if the assignment is allowed
4956
allowed, err := a.IsAllowed(set)
5057
if !allowed {

pkg/whitelist/whitelist.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package whitelist
2+
3+
type Whitelist struct {
4+
ID uint64 `db:"id"`
5+
JobID uint64 `db:"job_id"`
6+
WorkerID uint64 `db:"worker_id"`
7+
}

0 commit comments

Comments
 (0)