MCPcopy Index your code
hub / github.com/omkarcloud/botasaurus / createTask

Function createTask

js/botasaurus-js/src/task.ts:48–235  ·  view source on GitHub ↗
(options: TaskOptions<I>, is_async_queue: boolean)

Source from the content-addressed store, hash-verified

46
47
48function createTask<I>(options: TaskOptions<I>, is_async_queue: boolean) {
49 const performTask = async function (data: any = null, overrideOptions: Omit<TaskOptions<any>, 'run'> = {}) {
50 const combined = { ...options, ...overrideOptions }
51 printRunning()
52
53 let {
54 parallel = 1, name = null, run, metadata = null, cache = null,
55 // beep = false,
56 closeOnCrash = false, output = 'default', outputFormats = null, raiseException = false, mustRaiseExceptions = null, maxRetry = null, retryWait = null, createErrorLogs = true,
57 } = combined
58
59 performTask.__name__ = isNotEmptyString(name) ? name!.trim() : performTask.__name__
60 // @ts-ignore
61 const returnDontCacheAsIs = combined.returnDontCacheAsIs
62 // @ts-ignore
63 const isAborted = combined.isAborted ?? (() => false)
64 // @ts-ignore
65 const pushData = combined.pushData ?? (() => {})
66 // @ts-ignore
67 const taskId = combined.taskId ?? null
68 // @ts-ignore
69 const parentTaskId = combined.parentTaskId ?? null
70 const fn_name = performTask.__name__
71
72 if (cache) {
73 createCacheDirectoryIfNotExists(fn_name)
74 }
75
76 async function runTask(
77 data: any,
78 retryAttempt: number
79 ): Promise<any> {
80 let path: string | undefined
81
82 if (cache === true) {
83 path = _getCachePath(fn_name, data)
84 if (_has(path)) {
85 try {
86 return _get(path)
87 } catch (error) {
88 if (!(error instanceof CacheMissException)) throw error
89 }
90 }
91 } else if (cache === 'REFRESH') {
92 path = _getCachePath(fn_name, data)
93 }
94
95 let result: any
96 try {
97 result = await run({ data, metadata, taskId, parentTaskId, isAborted, pushData })
98 if (result === undefined) {
99 result = null
100 }
101
102 if (cache === true || cache === 'REFRESH') {
103 if (isDontCache(result)) {
104 _remove(path!)
105 } else {

Callers 2

taskFunction · 0.70
taskQueueFunction · 0.70

Calls 1

isNotEmptyStringFunction · 0.90

Tested by

no test coverage detected