-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/job config #767
Open
matthew-nguyen-20032023
wants to merge
7
commits into
develop
Choose a base branch
from
feat/job_config
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Feat/job config #767
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4370b06
feat: config job
matthew-nguyen-20032023 f1b3cc6
Merge branch 'develop' of github.com:aura-nw/horoscope-v2 into feat/j…
matthew-nguyen-20032023 ea04fa3
feat: config job
matthew-nguyen-20032023 5417bd2
fix: fix unit test
matthew-nguyen-20032023 3d66324
fix: fix comment
matthew-nguyen-20032023 08de274
feat: unit test for config job
matthew-nguyen-20032023 9d365df
Merge branch 'develop' of github.com:aura-nw/horoscope-v2 into feat/j…
matthew-nguyen-20032023 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Knex } from 'knex'; | ||
import { ConfigJob } from '../src/models'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
await knex.schema.createTable(ConfigJob.tableName, (table) => { | ||
table.increments(); | ||
table.string('job_name').index().notNullable(); | ||
table.integer('expected_row').notNullable(); | ||
table.integer('block_range').notNullable(); | ||
table.integer('acceptance_error').notNullable(); | ||
table.integer('block_balance').notNullable(); | ||
table.boolean('is_sync').defaultTo(false).notNullable(); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import BaseModel from './base'; | ||
import { BlockCheckpoint } from './block_checkpoint'; | ||
|
||
export class ConfigJob extends BaseModel { | ||
id!: number; | ||
|
||
job_name!: string; | ||
|
||
// Expected row get for handle when each time job run | ||
expected_row!: number; | ||
|
||
// Range query by block | ||
block_range!: number; | ||
|
||
// ABS(rows retrieve - expected_row) should less than acceptance_error, if not do the block_balance below | ||
acceptance_error!: number; | ||
|
||
// When get rows by block range not fit with expected_row, then increase/decrease block_range for fit with | ||
block_balance!: number; | ||
|
||
// Keep job run normal or use config job and sync | ||
is_sync!: boolean; | ||
|
||
static get tableName() { | ||
return 'config_jobs'; | ||
} | ||
|
||
/** | ||
* @description: | ||
* @param jobName Job name want to query to get config | ||
* job crawl block complete on that block we want to crawl | ||
*/ | ||
static async getConfigJob(jobName: string): Promise<ConfigJob | undefined> { | ||
return ConfigJob.query().select('*').where('job_name', jobName).first(); | ||
} | ||
|
||
// Range job will from currentStartBlock to bestEndBlock | ||
static determineBestRangeBlockForRunJob( | ||
currentStartBlock: number, | ||
currentEndBlock: number, | ||
dependingCheckPointJob: BlockCheckpoint | undefined, | ||
configJob: ConfigJob | undefined | ||
): number { | ||
let bestEndBlock = currentEndBlock; | ||
|
||
// No config job, then use default config from block checkpoint | ||
if (!configJob || !configJob.is_sync) return bestEndBlock; | ||
|
||
if (dependingCheckPointJob) { | ||
if ( | ||
currentStartBlock + configJob.block_range <= | ||
dependingCheckPointJob.height | ||
) { | ||
bestEndBlock = currentStartBlock + configJob.block_range; | ||
} else { | ||
bestEndBlock = dependingCheckPointJob.height; | ||
} | ||
} else { | ||
bestEndBlock = currentStartBlock + configJob.block_range; | ||
} | ||
|
||
return bestEndBlock; | ||
} | ||
|
||
// Balance config job | ||
static prepareBalanceJob( | ||
bestEndBlock: number, | ||
dependingCheckPointJob: BlockCheckpoint | undefined, | ||
configJob: ConfigJob | undefined, | ||
currentTotalRowFetch: number | ||
): ConfigJob | null { | ||
// No job config so keep every thing normal | ||
if (!configJob || !configJob.is_sync) return null; | ||
|
||
// Job depending on another job, so keep current config job | ||
if (bestEndBlock === dependingCheckPointJob?.height) return null; | ||
|
||
const errorRow = configJob.expected_row - currentTotalRowFetch; | ||
// error acceptance | ||
if (Math.abs(errorRow) <= configJob.acceptance_error) return null; | ||
|
||
if (errorRow > 0) { | ||
// increase range block if total row fetch too low with expected row | ||
// eslint-disable-next-line no-param-reassign | ||
configJob.block_range += configJob.block_balance; | ||
} else { | ||
// decrease range block if total row fetch too low with expected row | ||
// eslint-disable-next-line no-param-reassign | ||
configJob.block_range -= configJob.block_balance; | ||
} | ||
|
||
return configJob; | ||
} | ||
|
||
static get jsonSchema() { | ||
return { | ||
type: 'object', | ||
required: [ | ||
'job_name', | ||
'expected_row', | ||
'block_range', | ||
'acceptance_error', | ||
'block_balance', | ||
], | ||
properties: { | ||
id: { type: 'number' }, | ||
job_name: { type: 'string' }, | ||
expected_row: { type: 'number' }, | ||
block_range: { type: 'number' }, | ||
acceptance_error: { type: 'number' }, | ||
block_balance: { type: 'number' }, | ||
}, | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vậy là chưa dùng cái param cuối à?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
à, chỉ return thêm
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Đúng rồi anh, em extend cái func của Phong ra để handle thêm cái logic job A phụ thuộc vào job B,C,D