예제 #1
0
파일: model.go 프로젝트: badoo/thunder
// get existing ids from db for provided checkIds list
func getExistingTTIds(checkIds []uint64) (map[uint64]bool, error) {
	res := make(map[uint64]bool, len(checkIds))
	if len(checkIds) == 0 {
		return res, nil
	}

	rows, err := db.Query(QUERY_CHECK_TT_IDS, "ids", db.INUint64(checkIds))
	if err != nil {
		return nil, err
	}

	defer rows.Close()

	var id uint64
	for rows.Next() {
		err = rows.Scan(&id)

		if err != nil {
			return nil, err
		}

		res[id] = true
	}

	return res, nil
}
예제 #2
0
파일: model.go 프로젝트: badoo/thunder
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
}
예제 #3
0
파일: model.go 프로젝트: badoo/thunder
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
}
예제 #4
0
파일: model.go 프로젝트: badoo/thunder
func addToQueueAndDeleteExpired(tx *db.LazyTrx, rows []*RunQueueEntry, toDelete []*TimetableEntry) error {
	if len(rows) > 0 {
		values := make([]string, 0, len(rows))
		ttIds := make([]uint64, 0, len(rows))

		fields := `(class_name, timetable_id, generation_id, hostname, hostname_idx, job_data, method, created, run_status, waiting_ts, should_init_ts, token, retry_attempt, settings_id)`

		for _, row := range rows {
			ttIds = append(ttIds, uint64(row.timetable_id.Int64))

			val := fmt.Sprint(
				"('"+db.EscapeString(row.ClassName)+"',",
				db.QNullInt64(row.timetable_id).Data, ",",
				row.generation_id.Int64, ",",
				"'"+db.EscapeString(row.hostname)+"',",
				row.hostname_idx, ",",
				"'"+db.EscapeString(row.JobData)+"',",
				"'"+db.EscapeString(row.method)+"',",
				"FROM_UNIXTIME(", row.created.Int64, "),",
				"'"+db.EscapeString(row.RunStatus)+"',",
				"FROM_UNIXTIME(", row.waiting_ts.Int64, "),",
				"FROM_UNIXTIME(", row.should_init_ts.Int64, "),",
				"'"+db.EscapeString(row.token), "',",
				row.retry_attempt, ",",
				row.settings_id, ")")

			values = append(values, val)
		}

		res, err := tx.Exec(QUERY_INSERT_INTO_RUN_QUEUE, "fields", fields, "values", &db.RawData{Data: strings.Join(values, ",")})
		if err != nil {
			return err
		}

		insId, err := res.LastInsertId()
		if err != nil {
			return err
		}

		for _, row := range rows {
			row.Id = uint64(insId)
			insId += autoIncrementIncrement
		}

		sort.Sort(common.UInt64Slice(ttIds))

		res, err = tx.Exec(QUERY_LOG_ADD_TO_QUEUE, "ids", db.INUint64(ttIds))
		if err != nil {
			return err
		}
		aff, err := res.RowsAffected()
		if err != nil {
			return err
		}
		if aff != int64(len(ttIds)) {
			return fmt.Errorf("update ur cache bro aff: %d ttIds:%+v", aff, ttIds)
		}
	}

	if len(toDelete) > 0 {
		ttIds := make([]uint64, 0, len(toDelete))
		for _, row := range toDelete {
			ttIds = append(ttIds, row.id)
		}

		sort.Sort(common.UInt64Slice(ttIds))

		res, err := tx.Exec(QUERY_DELETE_FROM_TIMETABLE, "ids", db.INUint64(ttIds), "add_where", " AND added_to_queue_ts IS NULL")
		if err != nil {
			return err
		}
		aff, err := res.RowsAffected()
		if err != nil {
			return err
		}
		if aff != int64(len(ttIds)) {
			return fmt.Errorf("addToQueueAndDeleteExpired unexpected ttIds deleted count:%d instead of %d ids:%+v", aff, len(ttIds), ttIds)
		}
	}

	return nil
}
예제 #5
0
파일: model.go 프로젝트: badoo/thunder
func selectTimetableByIds(ids []uint64) ([]*TimetableEntry, error) {
	timetableRows := make([]*TimetableEntry, 0, len(ids))
	if len(ids) == 0 {
		return timetableRows, nil
	}

	rows, err := db.Query(QUERY_SELECT_FROM_TIMETABLE, "add_where", &db.RawData{Data: fmt.Sprintf("WHERE id IN (%s)", db.INUint64(ids).Data)})
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	for rows.Next() {
		tt_entry, err := scanTimetableEntry(rows)
		if err != nil {
			return nil, err
		}
		timetableRows = append(timetableRows, tt_entry)
	}

	return timetableRows, nil
}
예제 #6
0
파일: model.go 프로젝트: badoo/thunder
func getRunningInfos(run_ids []uint64) ([]*RunQueueEntry, error) {
	res := make([]*RunQueueEntry, 0, len(run_ids))
	if len(run_ids) == 0 {
		return res, nil
	}
	rows, err := db.Query(QUERY_GET_RUNQUEUE, "where", &db.RawData{Data: fmt.Sprintf("WHERE id IN (%s)", db.INUint64(run_ids).Data)})
	if err != nil {
		return nil, err
	}

	defer rows.Close()

	for rows.Next() {
		rq_entry, err := scanRqEntry(rows)
		if err != nil {
			return nil, err
		}

		res = append(res, rq_entry)
	}

	return res, nil
}