本文整理匯總了Golang中github.com/badoo/thunder/db.LazyTrx類的典型用法代碼示例。如果您正苦於以下問題:Golang LazyTrx類的具體用法?Golang LazyTrx怎麽用?Golang LazyTrx使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了LazyTrx類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: batchSetNextGenerateJobTs
func batchSetNextGenerateJobTs(tx *db.LazyTrx, className string, params []NextGenParams, settings *ScriptSettings) (err error) {
if len(params) == 0 {
return
}
fields := `(class_name, location, next_generate_job_ts, jobs_generated_ts, jobs_finished_ts, init_jobs_ts, finish_jobs_ts, generation_id, settings_id)`
values := make([]string, 0, len(params))
for _, par := range params {
firstRun := !par.JobInfo.jobs_generated_ts.Valid
var prevTs, nextTs uint64
if !firstRun {
prevTs = uint64(par.JobInfo.jobs_generated_ts.Int64)
}
nextTs = getNextJobGenerateTs(className, firstRun, prevTs, settings)
values = append(values, fmt.Sprintf("('%s', '%s', FROM_UNIXTIME(%d), NULL, NULL, NULL, NULL, 1, %d)",
db.EscapeString(className), db.EscapeString(par.Location), nextTs, settings.id))
}
_, err = tx.Exec(
QUERY_BATCH_SET_NEXT_GENERATE_JOB_TS,
"fields", fields,
"values", &db.RawData{Data: strings.Join(values, ", ")})
return
}
示例2: setRunStatusToInit
func setRunStatusToInit(tx *db.LazyTrx, run_id uint64, max_time int) (err error) {
_, err = tx.Exec(
QUERY_UPDATE_RUN_STATUS_INIT,
"max_time", max_time,
"id", run_id)
return
}
示例3: setMaxFinishedTs
func setMaxFinishedTs(tx *db.LazyTrx, className string, employeeId int64, ts int64) (err error) {
_, err = tx.Exec(
QUERY_SET_MAX_FINISHED_TS_NOW,
"class_name", className,
"employee_id", employeeId,
"ts", ts)
return
}
示例4: setNextGenerateJobTs
func setNextGenerateJobTs(tx *db.LazyTrx, className string, settings *ScriptSettings) (err error) {
nextTs := getNextJobGenerateTs(className, true, 0, settings)
_, err = tx.Exec(
QUERY_SET_NEXT_GENERATE_JOB_TS,
"class_name", className,
"settings_id", settings.id,
"next_generate_job_ts", nextTs)
return
}
示例5: clearJobsResultsForLocations
func clearJobsResultsForLocations(tx *db.LazyTrx, className string, locations []string) (err error) {
if len(locations) == 0 {
return
}
_, err = tx.Exec(
QUERY_CLEAR_JOB_RESULTS_FOR_LOCATIONS,
"class_name", className,
"locations", db.INStr(locations))
return
}
示例6: setJobsGeneratedTs
func setJobsGeneratedTs(tx *db.LazyTrx, className string, locations []string) (err error) {
if len(locations) == 0 {
return
}
_, err = tx.Exec(
QUERY_SET_JOBS_GENERATED_TS,
"class_name", className,
"locations", db.INStr(locations))
return
}
示例7: setInitJobsTs
func setInitJobsTs(tx *db.LazyTrx, className string, locations []string) (err error) {
if len(locations) == 0 {
return
}
_, err = tx.Exec(
QUERY_SET_INIT_JOBS_TS,
"class_name", className,
"locations", db.INStr(locations))
return
}
示例8: deleteAddedFromTimetable
func deleteAddedFromTimetable(tx *db.LazyTrx, ids []uint64) error {
if len(ids) == 0 {
return nil
}
res, err := tx.Exec(QUERY_DELETE_FROM_TIMETABLE, "ids", db.INUint64(ids), "add_where", " AND added_to_queue_ts IS NOT NULL")
if err != nil {
return err
}
aff, err := res.RowsAffected()
if err != nil {
return err
}
if aff != int64(len(ids)) {
return fmt.Errorf("deleteAddedFromTimetable unexpected ttIds deleted count:%d instead of %d ids:%+v", aff, len(ids), ids)
}
return nil
}
示例9: addToTimetable
// method inserts rows into timetable and sets insert id
func addToTimetable(tx *db.LazyTrx, ttRows []*TimetableEntry) error {
if len(ttRows) == 0 {
return nil
}
values := make([]string, 0)
for _, row := range ttRows {
val := fmt.Sprintf(
"('%s', %d, %s, '%s', %d, %s, %d, '%s', '%s', %d, FROM_UNIXTIME(%d), FROM_UNIXTIME(%d))",
db.EscapeString(row.class_name),
row.default_retry,
db.QNullInt64(row.repeat).Data,
row.method,
row.finished_successfully,
db.QNullInt64(row.generation_id).Data,
row.settings_id,
db.EscapeString(row.location),
db.EscapeString(row.JobData),
0,
row.created,
row.NextLaunchTs.Int64)
values = append(values, val)
}
res, err := tx.Exec(QUERY_INSERT_INTO_TIMETABLE, "values", &db.RawData{Data: strings.Join(values, ", ")})
if err != nil {
return err
}
insId, err := res.LastInsertId()
if err != nil {
log.Errorf("Could not get insert id even though insert was successfull: %s", err.Error())
return err
}
for _, row := range ttRows {
row.id = uint64(insId)
insId += autoIncrementIncrement
}
return nil
}
示例10: addJobInfo
func addJobInfo(tx *db.LazyTrx, rows []*JobInfoEntry) (err error) {
if len(rows) == 0 {
return
}
fields := `(class_name, location, next_generate_job_ts, settings_id)`
values := make([]string, 0, len(rows))
for _, row := range rows {
values = append(values, fmt.Sprintf(
"('%s', '%s', FROM_UNIXTIME(%d), %d)",
db.EscapeString(row.class_name), db.EscapeString(row.location), row.next_generate_job_ts.Int64, row.settings_id))
}
_, err = tx.Exec(
QUERY_INSERT_INTO_JOB_INFO,
"fields", fields,
"values", &db.RawData{Data: strings.Join(values, ",")})
return
}
示例11: deleteFromRunQueue
func deleteFromRunQueue(tx *db.LazyTrx, ids []uint64, prevStatus string) error {
if len(ids) == 0 {
return nil
}
res, err := tx.Exec(
QUERY_DELETE_FROM_QUEUE,
"ids", db.INUint64(ids),
"status", prevStatus)
if err != nil {
return err
}
aff, err := res.RowsAffected()
if err != nil {
return err
}
if aff != int64(len(ids)) {
return fmt.Errorf("deleteFromRunQueue failed aff:%d expected:%d ids:%+v prevStatus:%s", aff, len(ids), ids, prevStatus)
}
return nil
}
示例12: logTTFinish
func logTTFinish(tx *db.LazyTrx, row *TimetableEntry, haveOldFinishCount bool, oldFinishCount uint) error {
var addedToQueueWhere, addedFinishCountWhere string
if row.added_to_queue_ts.Valid {
addedToQueueWhere = "AND added_to_queue_ts IS NULL"
} else {
addedToQueueWhere = "AND added_to_queue_ts IS NOT NULL"
}
if haveOldFinishCount {
addedFinishCountWhere = fmt.Sprintf("AND finish_count = %d", oldFinishCount)
} else {
addedFinishCountWhere = fmt.Sprintf("AND finish_count = %d", row.finish_count-1)
}
res, err := tx.Exec(QUERY_UPDATE_TIMETABLE_STATUS,
"finished_ts", db.QNullInt64(row.finished_ts),
"next_launch_ts", db.QNullInt64(row.NextLaunchTs),
"added_to_queue_ts", db.QNullInt64(row.added_to_queue_ts),
"finish_count", row.finish_count,
"retry_count", row.retry_count,
"finished_successfully", row.finished_successfully,
"id", row.id,
"add_where", fmt.Sprintf("%s %s", addedToQueueWhere, addedFinishCountWhere))
if err != nil {
return err
}
aff, err := res.RowsAffected()
if err != nil {
return err
}
if aff != 1 {
return fmt.Errorf("unexpected affected rows = %d for tt_row = %+v", aff, row)
}
return nil
}
示例13: updateRunStatus
func updateRunStatus(tx *db.LazyTrx, run_id uint64, status, prevStatus string) error {
res, err := tx.Exec(
QUERY_UPDATE_RUN_STATUS,
"status", status,
"prev_status", prevStatus,
"id", run_id)
if err != nil {
return err
}
aff, err := res.RowsAffected()
if err != nil {
return err
}
if aff != 1 {
return fmt.Errorf("Previous status mismatch for rq row id=%d: tried %s -> %s", run_id, prevStatus, status)
}
return nil
}
示例14: doCycle
func doCycle() bool {
var (
jiRows map[string]map[string]*JobInfoEntry
scripts map[string]*ScriptEntry
flags map[string]*FlagEntry
scriptsRusage map[string]*ScriptRusageEntry
classLocTTRows map[string]map[string][]*TimetableEntry
)
unifiedStartTs := time.Now().UnixNano()
startTs := time.Now().UnixNano()
err := loadFullState(
&LoadStateFunc{name: "Scripts", fun: func() (err error) { scripts, err = getGroupedScriptsForPlatform(); return }},
&LoadStateFunc{name: "JobInfo", fun: func() (err error) { jiRows, err = getGroupedJobInfo(); return }},
&LoadStateFunc{name: "Flags", fun: func() (err error) { flags, err = getFlags(); return }},
&LoadStateFunc{name: "ScriptsRusage", fun: func() (err error) { scriptsRusage, err = getScriptRusageStats(); return }},
&LoadStateFunc{name: "ScriptTimetable", fun: func() (err error) { classLocTTRows, err = selectTimetable(); return }})
if err != nil {
log.Errorf("Failed to select state in doCycle: %s", err.Error())
return false
}
log.Debugf("Loaded for %.5f sec", float64(time.Now().UnixNano()-startTs)/1e9)
startTs = time.Now().UnixNano()
err = loadSettingsFromRows(jiRows, scripts)
if err != nil {
log.Errorf("Could not load settings from rows: %s", err.Error())
return false
}
func() {
allSettingsMutex.Lock()
defer allSettingsMutex.Unlock()
for _, row := range scripts {
row.settings = allSettings[row.settings_id]
}
}()
scriptsMap.Lock()
scriptsMap.v = scripts
scriptsMap.Unlock()
log.Debugf(" Selected %d rows from flags", len(flags))
log.Debugf(" Selected %d rows from scripts rusage", len(scriptsRusage))
log.Debugf("Load settings for %.5f sec", float64(time.Now().UnixNano()-startTs)/1e9)
startTs = time.Now().UnixNano()
// We should not try to generate jobs for scripts that are not present in Script table
// But we should not forget settings (e.g. last generation_id) for that script
for class_name := range jiRows {
if _, ok := scripts[class_name]; !ok {
delete(jiRows, class_name)
}
}
log.Debugf("Selected all for %.5f sec", float64(time.Now().UnixNano()-unifiedStartTs)/1e9)
startTs = time.Now().UnixNano()
updateLoadEstimates()
log.Debugf("Load estimates updated for %.5f sec", float64(time.Now().UnixNano()-startTs)/1e9)
func() {
rusageInfo.Lock()
defer rusageInfo.Unlock()
log.Debugf("Group hosts: %+v", rusageInfo.groupHosts)
}()
startTs = time.Now().UnixNano()
failedLocationsMutex.Lock()
failedLocations = make(map[string]bool)
failedLocationsMutex.Unlock()
success := true
if len(scripts) > 0 {
throttle.setIntervalCh <- time.Second / time.Duration(len(scripts))
}
trigger(throttle.c, "throttle, start of cycle")
for className, script := range scripts {
<-throttle.c
tx := new(db.LazyTrx)
err := tx.Begin()
if err != nil {
log.Errorf("Could not start transaction in job generate: %s", err.Error())
success = false
continue
}
have := make(map[string]bool)
locTtRows := classLocTTRows[className]
if locTtRows != nil {
//.........這裏部分代碼省略.........
示例15: generateJobs
// haveTTRows must be nil if there are no timetable entries for any location
// otherwise it must have only true entries like map["location"] => true
// probably jobs generation can be simplified, it is just the way it is
func generateJobs(tx *db.LazyTrx, className string, settings *ScriptSettings, jiRows map[string]*JobInfoEntry, haveTTRows map[string]bool, flags *FlagEntry) (add_to_timetable []*TimetableEntry, err error) {
if haveTTRows != nil && len(haveTTRows) == 0 {
haveTTRows = nil
}
now := time.Now().Unix()
add_to_timetable = make([]*TimetableEntry, 0)
add_job_info := make([]*JobInfoEntry, 0)
set_finish_jobs := make([]string, 0)
set_init_jobs := make([]string, 0)
set_jobs_generated_js := make([]string, 0)
prepare_next_generation := make([]NextGenParams, 0)
have_finish_jobs := settings.jobs.Have_finish_jobs
is_any := (settings.location_type == LOCATION_TYPE_ANY)
is_temporary := settings.jobs.Temporary
temporary_can_run := false
if flags != nil {
if flags.kill_requested_ts.Valid {
is_done := (haveTTRows == nil)
if is_done {
log.Printf("Class %s is done, all is ok", className)
if !flags.killed_ts.Valid {
tx.AddCommitCallback(func() { continueDispatchAfterKill(className) })
if err = setKilledFlag(tx, className); err != nil {
return
}
if err = prepareNextGeneration(tx, have_finish_jobs, className, settings); err != nil {
return
}
}
} else {
log.Printf("Class %s is not done", className)
startKilling(className)
// not the best place to put it, but it works
if err = setMaxFinishedTs(tx, className, flags.kill_request_employee_id.Int64, flags.kill_requested_ts.Int64); err != nil {
return
}
}
return
}
// Stop generating new job generations when we are on pause
if flags.pause_requested_ts.Valid {
is_done := generationFinished(className, haveTTRows, jiRows, settings)
if is_done && !flags.paused_ts.Valid {
if err = setPausedFlag(tx, className); err != nil {
return
}
flags.paused_ts = sql.NullInt64{Int64: now, Valid: true}
}
if !is_any || flags.paused_ts.Valid {
return
}
}
if is_temporary && flags.run_requested_ts.Valid && is_any {
// We accepted run request, which means that we already generated jobs
if flags.run_accepted_ts.Valid {
if generationFinished(className, haveTTRows, jiRows, settings) {
if err = resetRunRequest(tx, className); err != nil {
return
}
if err = prepareNextGeneration(tx, have_finish_jobs, className, settings); err != nil {
return
}
return
}
} else {
if err = setRunAccepted(tx, className); err != nil {
return
}
}
temporary_can_run = true
}
}
if is_temporary && !temporary_can_run || settings.jobs.Type == JOBS_TYPE_NONE {
return
}
locations := make([]string, 0)
if !is_any {
//.........這裏部分代碼省略.........