Workers & Tasks
Overview
Fountain models background work as first-class runnables orchestrated by the Fountain singleton. The bootstrap order is Invokers → Jobs → Crons → AppInstances → Auxiliary.
- Invokers — one-shot initialization steps (e.g. connecting databases) run before servers start.
- Jobs —
JobInstanceunits of work; seefjob. - Crons — scheduled tasks; see
fcron. - Worker pool — concurrent execution via
fgo.
Example
A job runnable wired into the framework:
go
/* !!
* File: main.go
* File Created: Tuesday, 28th June 2022 4:24:43 pm
* Author: Kim Ericko ([email protected])
* -----
* Last Modified: Thursday, 30th June 2022 5:12:31 pm
* Modified By: Kim Ericko ([email protected])
* -----
* Copyright 2022 Soludian, soludian.com
* All rights reserved.
*
* Licensed under the SOLUDIAN TECHNOLOGY SOLUTION CO., LTD Software License Agreement.
* Unauthorized use, reproduction, or distribution is prohibited (the "License");
*
* You may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* [email protected] / https://www.soludian.com/license
*
* 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.
* -----
*
* HISTORY:
*
* Date By Comments
* ---------- --- ---------------------------------------------------------
*/
package main
import (
"errors"
"log"
"os"
"time"
"gitlab.soludian.com/soludian/fountain"
"gitlab.soludian.com/soludian/fountain/constants"
"gitlab.soludian.com/soludian/fountain/libs/ftasker/fjob"
"gitlab.soludian.com/soludian/fountain/libs/ftracer"
)
// export FOUNTAIN_DEBUG_MODE=true && go run main.go --job=job1 --config=config.toml
func main() {
os.Setenv(constants.KFountainJobNameEnvKey, "job1") // #nosec G104
fountain.WithHang(true).WithJobs(fjob.Job("job1", job1), fjob.Job("job2", job2)).Serving()
}
func job2(ctx fjob.Context) error {
log.Println("Job 2 sleeping...")
time.Sleep(10 * time.Second)
log.Println("i am error job runner, traceId: ", ftracer.ExtractTraceID(ctx.Ctx))
return errors.New("i am error")
}
func job1(ctx fjob.Context) error {
log.Println("i am job runner, traceId: ", ftracer.ExtractTraceID(ctx.Ctx))
return nil
}See also
- Reference:
fjob,fcron,fgo - Runnable —
AppInstance,Invoker,JobInstanceinterfaces - Examples index