Skip to content

Commit

Permalink
Merge pull request #3193 from cnviradiya/add-config-max-attempt
Browse files Browse the repository at this point in the history
Added configuration for the max attempt request
  • Loading branch information
patzick committed Jul 10, 2019
2 parents 7f3a027 + f6a22e6 commit 771507c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Moves theme specific stores and components into themes - @michasik (#3139)
- Decreased the `localStorage` quota usage + error handling by introducing new config variables: `config.products.disablePersistentProductsCache` to not store products by SKU (by default it's on). Products are cached in ServiceWorker cache anyway so the `product/list` will populate the in-memory cache (`cache.setItem(..., memoryOnly = true)`); `config.seo.disableUrlRoutesPersistentCache` - to not store the url mappings; they're stored in in-memory cache anyway so no additional requests will be made to the backend for url mapping; however it might cause some issues with url routing in the offline mode (when the offline mode PWA installed on homescreen got reloaded, the in-memory cache will be cleared so there won't potentially be the url mappings; however the same like with `product/list` the ServiceWorker cache SHOULD populate url mappings anyway); `config.syncTasks.disablePersistentTaskQueue` to not store the network requests queue in service worker. Currently only the stock-check and user-data changes were using this queue. The only downside it introuces can be related to the offline mode and these tasks will not be re-executed after connectivity established, but just in a case when the page got reloaded while offline (yeah it might happen using ServiceWorker; `syncTasks` can't be re-populated in cache from SW) - @pkarw (#3180)
- Translation file improvements - @vishal-7037 (#3198)
- Added configuration for max attempt task & cart by pass - @cnviradiya (#3193)

## [1.10.0-rc.2] - UNRELEASED

Expand Down
4 changes: 4 additions & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@
"initialStateFilter": ["__DEMO_MODE__", "version", "storeView"],
"useInitialStateFilter": true
},
"queues": {
"maxNetworkTaskAttempts": 1,
"maxCartBypassAttempts": 1
},
"defaultStoreCode": "",
"storeViews": {
"multistore": false,
Expand Down
5 changes: 2 additions & 3 deletions core/lib/sync/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import { serial } from '@vue-storefront/core/helpers'
import config from 'config'
import { onlineHelper } from '@vue-storefront/core/helpers'

const AUTO_REFRESH_MAX_ATTEMPTS = 20

export function _prepareTask (task) {
const taskId = entities.uniqueEntityId(task) // timestamp as a order id is not the best we can do but it's enough
Expand Down Expand Up @@ -84,7 +83,7 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
if (config.users.autoRefreshTokens) {
if (!rootStore.state.userTokenInvalidateLock) {
rootStore.state.userTokenInvalidateLock++
if (rootStore.state.userTokenInvalidateAttemptsCount >= AUTO_REFRESH_MAX_ATTEMPTS) {
if (rootStore.state.userTokenInvalidateAttemptsCount >= config.queues.maxNetworkTaskAttempts) {
Logger.error('Internal Application error while refreshing the tokens. Please clear the storage and refresh page.', 'sync')()
rootStore.state.userTokenInvalidateLock = -1
rootStore.dispatch('user/logout', { silent: true })
Expand Down Expand Up @@ -120,7 +119,7 @@ function _internalExecute (resolve, reject, task: Task, currentToken, currentCar
})
}
}
if (rootStore.state.userTokenInvalidateAttemptsCount <= AUTO_REFRESH_MAX_ATTEMPTS) _internalExecute(resolve, reject, task, currentToken, currentCartId) // retry
if (rootStore.state.userTokenInvalidateAttemptsCount <= config.queues.maxNetworkTaskAttempts) _internalExecute(resolve, reject, task, currentToken, currentCartId) // retry
} else {
Logger.info('Invalidation process is disabled (autoRefreshTokens is set to false)', 'sync')()
rootStore.dispatch('user/logout', { silent: true })
Expand Down
5 changes: 2 additions & 3 deletions core/modules/cart/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { isServer } from '@vue-storefront/core/helpers'
import config from 'config'
import Task from '@vue-storefront/core/lib/sync/types/Task'

const MAX_BYPASS_COUNT = 10
let _connectBypassCount = 0

function _getDifflogPrototype () {
Expand Down Expand Up @@ -207,7 +206,7 @@ const actions: ActionTree<CartState, RootState> = {
diffLog = await dispatch('merge', { serverItems: task.result, clientItems: getters.getCartItems, dryRun: dryRun, forceClientState: forceClientState })
} else {
Logger.error(task.result, 'cart') // override with guest cart()
if (_connectBypassCount < MAX_BYPASS_COUNT) {
if (_connectBypassCount < config.queues.maxCartBypassAttempts) {
Logger.log('Bypassing with guest cart' + _connectBypassCount, 'cart')()
_connectBypassCount = _connectBypassCount + 1
await dispatch('connect', { guestCart: true })
Expand Down Expand Up @@ -524,7 +523,7 @@ const actions: ActionTree<CartState, RootState> = {
} else {
let resultString = task.result ? toString(task.result) : null
if (resultString && (resultString.indexOf(i18n.t('not authorized')) < 0 && resultString.indexOf('not authorized')) < 0) { // not respond to unathorized errors here
if (_connectBypassCount < MAX_BYPASS_COUNT) {
if (_connectBypassCount < config.queues.maxCartBypassAttempts) {
Logger.log('Bypassing with guest cart' + _connectBypassCount, 'cart')()
_connectBypassCount = _connectBypassCount + 1
Logger.error(task.result, 'cart')()
Expand Down
15 changes: 15 additions & 0 deletions docs/guide/basics/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,21 @@ The SSR data is being completed in the `asyncData` static method. If this config
If it's set to `false`, then **just the** `src/themes/default/pages/Product.vue` -> `asyncData` will be executed.
This option is referenced in the [core/client-entry.ts](https://github.com/DivanteLtd/vue-storefront/blob/master/core/client-entry.ts) line: 85.

## Max attempt of tasks

```json
"queues": {
"maxNetworkTaskAttempts": 1,
"maxCartBypassAttempts": 1
},
```

This both option is used when you don't want re-attempting task of just X number time attempt task.

`maxNetworkTaskAttempts` config variable is referenced in the [core/lib/sync/task.ts](https://github.com/DivanteLtd/vue-storefront/blob/master/core/lib/sync/task.ts) and It's reattempt if user token is invalid.

`maxCartBypassAttempts` config variable is referenced in the [core/modules/cart/store/actions.ts](https://github.com/DivanteLtd/vue-storefront/blob/master/core/modules/cart/store/actions.ts)

## Default store code

```json
Expand Down

0 comments on commit 771507c

Please sign in to comment.