Пример #1
0
// 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
}
Пример #2
0
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
}
Пример #3
0
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
}