Mercurial > gemma
comparison pkg/imports/queue.go @ 1497:b41ad15cc55f
Backend: Documented the internal API of the global import queue.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 04 Dec 2018 18:54:50 +0100 |
parents | d26e3e1fcff1 |
children | b10aa02d7819 |
comparison
equal
deleted
inserted
replaced
1496:3bf1f0de0763 | 1497:b41ad15cc55f |
---|---|
29 "gemma.intevation.de/gemma/pkg/auth" | 29 "gemma.intevation.de/gemma/pkg/auth" |
30 "gemma.intevation.de/gemma/pkg/config" | 30 "gemma.intevation.de/gemma/pkg/config" |
31 ) | 31 ) |
32 | 32 |
33 type ( | 33 type ( |
34 // Feedback is passed to the Do method of a Job to log | |
35 // informations, warnings or errors. | |
34 Feedback interface { | 36 Feedback interface { |
37 // Info logs informations. | |
35 Info(fmt string, args ...interface{}) | 38 Info(fmt string, args ...interface{}) |
39 // Warn logs warnings. | |
36 Warn(fmt string, args ...interface{}) | 40 Warn(fmt string, args ...interface{}) |
41 // Error logs errors. | |
37 Error(fmt string, args ...interface{}) | 42 Error(fmt string, args ...interface{}) |
38 } | 43 } |
39 | 44 |
45 // Job is the central abstraction of an import job | |
46 // run by the import queue. | |
40 Job interface { | 47 Job interface { |
41 Do(context.Context, int64, *sql.Conn, Feedback) (interface{}, error) | 48 // Do is called to do the actual import. |
49 // Bind transactions to ctx and conn, please- | |
50 // id is the number of the import job. | |
51 // feedback can be used to log the import process. | |
52 // If no error is return the import is assumed to | |
53 // be successfull. The non-error return value is | |
54 // serialized as a JSON string into the database as | |
55 // a summary to the import to be used by the review process. | |
56 Do(ctx context.Context, id int64, conn *sql.Conn, feedback Feedback) (interface{}, error) | |
57 // CleanUp is called to clean up ressources hold by the import. | |
58 // It is called whether the import succeeded or not. | |
42 CleanUp() error | 59 CleanUp() error |
43 } | 60 } |
44 | 61 |
62 // JobKind is the type of an import. | |
63 // Choose a unique name for every import. | |
45 JobKind string | 64 JobKind string |
46 | 65 |
66 // JobCreator is used to bring a job to life as it is stored | |
67 // in pure meta-data form to the database. | |
47 JobCreator interface { | 68 JobCreator interface { |
69 // Create build the actual job. | |
70 // kind is the name of the import type. | |
71 // data is a free form string to pass arguments to the creation | |
72 // process. This is useful to tell e.g. where to find data | |
73 // in the file system to be used for importing. | |
48 Create(kind JobKind, data string) (Job, error) | 74 Create(kind JobKind, data string) (Job, error) |
75 // Depends returns a list of ressources locked by this type of import. | |
76 // Imports are run concurrently if they have disjoint sets | |
77 // of dependencies. | |
49 Depends() []string | 78 Depends() []string |
79 // StageDone is called if an import is positively reviewed | |
80 // (state = accepted). This can be used to finalize the imported | |
81 // data to move it e.g from the staging area. | |
50 StageDone(context.Context, *sql.Tx, int64) error | 82 StageDone(context.Context, *sql.Tx, int64) error |
51 } | 83 } |
52 | 84 |
53 idJob struct { | 85 idJob struct { |
54 id int64 | 86 id int64 |
72 creators: map[JobKind]JobCreator{}, | 104 creators: map[JobKind]JobCreator{}, |
73 usedDeps: map[string]struct{}{}, | 105 usedDeps: map[string]struct{}{}, |
74 } | 106 } |
75 | 107 |
76 var ( | 108 var ( |
109 // ImportStateNames is a list of the states a job can be in. | |
77 ImportStateNames = []string{ | 110 ImportStateNames = []string{ |
78 "queued", | 111 "queued", |
79 "running", | 112 "running", |
80 "failed", | 113 "failed", |
81 "pending", | 114 "pending", |
147 q.creatorsMu.Lock() | 180 q.creatorsMu.Lock() |
148 defer q.creatorsMu.Unlock() | 181 defer q.creatorsMu.Unlock() |
149 q.creators[kind] = jc | 182 q.creators[kind] = jc |
150 } | 183 } |
151 | 184 |
185 // FindJobCreator looks up a JobCreator in the global import queue. | |
152 func FindJobCreator(kind JobKind) JobCreator { | 186 func FindJobCreator(kind JobKind) JobCreator { |
153 return iqueue.jobCreator(kind) | 187 return iqueue.jobCreator(kind) |
154 } | 188 } |
155 | 189 |
190 // ImportKindNames is a list of the names of the imports the | |
191 // global import queue supports. | |
156 func ImportKindNames() []string { | 192 func ImportKindNames() []string { |
157 return iqueue.importKindNames() | 193 return iqueue.importKindNames() |
158 } | 194 } |
159 | 195 |
196 // RegisterJobCreator adds a JobCreator to the global import queue. | |
197 // This a good candidate to be called in a init function for | |
198 // a particular JobCreator. | |
160 func RegisterJobCreator(kind JobKind, jc JobCreator) { | 199 func RegisterJobCreator(kind JobKind, jc JobCreator) { |
161 log.Printf("info: register import job creator for kind '%s'\n", kind) | 200 log.Printf("info: register import job creator for kind '%s'\n", kind) |
162 iqueue.registerJobCreator(kind, jc) | 201 iqueue.registerJobCreator(kind, jc) |
163 } | 202 } |
164 | 203 |
194 } | 233 } |
195 } | 234 } |
196 return id, err | 235 return id, err |
197 } | 236 } |
198 | 237 |
238 // AddJob adds a job to the global import queue to be executed | |
239 // as soon as possible. This is gone in a separate Go routine | |
240 // so this will not block. | |
199 func AddJob(kind JobKind, user, data string) (int64, error) { | 241 func AddJob(kind JobKind, user, data string) (int64, error) { |
200 return iqueue.addJob(kind, user, data) | 242 return iqueue.addJob(kind, user, data) |
201 } | 243 } |
202 | 244 |
203 type logFeedback int64 | 245 type logFeedback int64 |