Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
jio_mebibou
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Alexandra Rogova
jio_mebibou
Commits
8d6e596b
Commit
8d6e596b
authored
Jan 10, 2013
by
Sven Franck
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
jslint pass jobManager.js
parent
20b49bfa
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
288 additions
and
278 deletions
+288
-278
src/jio/jobs/jobManager.js
src/jio/jobs/jobManager.js
+288
-278
No files found.
src/jio/jobs/jobManager.js
View file @
8d6e596b
var
jobManager
=
(
function
(
spec
)
{
/*jslint indent: 2, maxlen: 80, sloppy: true */
var
that
=
{};
/*global localstorage: true, setInterval: true, clearInterval: true,
spec
=
spec
||
{};
command: true, job: true, jobRules: true */
// Attributes //
var
jobManager
=
(
function
(
spec
)
{
var
job_array_name
=
'
jio/job_array
'
;
var
that
=
{},
var
priv
=
{};
job_array_name
=
'
jio/job_array
'
,
priv
.
id
=
spec
.
id
;
priv
=
{};
priv
.
interval_id
=
null
;
priv
.
interval
=
200
;
priv
.
job_array
=
[];
// Methods //
spec
=
spec
||
{};
/**
// Attributes //
* Get the job array name in the localStorage
priv
.
id
=
spec
.
id
;
* @method getJobArrayName
priv
.
interval_id
=
null
;
* @return {string} The job array name
priv
.
interval
=
200
;
*/
priv
.
job_array
=
[];
priv
.
getJobArrayName
=
function
()
{
return
job_array_name
+
'
/
'
+
priv
.
id
;
};
/**
// Methods //
* Returns the job array from the localStorage
/**
* @method getJobArray
* Get the job array name in the localStorage
* @return {array} The job array.
* @method getJobArrayName
*/
* @return {string} The job array name
priv
.
getJobArray
=
function
()
{
*/
return
localstorage
.
getItem
(
priv
.
getJobArrayName
())
||
[];
priv
.
getJobArrayName
=
function
()
{
};
return
job_array_name
+
'
/
'
+
priv
.
id
;
};
/**
/**
* Does a backup of the job array in the localStorage.
* Returns the job array from the localStorage
* @method copyJobArrayToLocal
* @method getJobArray
*/
* @return {array} The job array.
priv
.
copyJobArrayToLocal
=
function
()
{
*/
var
new_a
=
[],
i
;
priv
.
getJobArray
=
function
()
{
for
(
i
=
0
;
i
<
priv
.
job_array
.
length
;
i
+=
1
)
{
return
localstorage
.
getItem
(
priv
.
getJobArrayName
())
||
[];
new_a
.
push
(
priv
.
job_array
[
i
].
serialized
());
};
}
localstorage
.
setItem
(
priv
.
getJobArrayName
(),
new_a
);
};
/**
/**
* Removes a job from the current job array.
* Does a backup of the job array in the localStorage.
* @method removeJob
* @method copyJobArrayToLocal
* @param {object} job The job object.
*/
*/
priv
.
copyJobArrayToLocal
=
function
()
{
priv
.
removeJob
=
function
(
job
)
{
var
new_a
=
[],
var
i
,
tmp_job_array
=
[];
i
;
for
(
i
=
0
;
i
<
priv
.
job_array
.
length
;
i
+=
1
)
{
for
(
i
=
0
;
i
<
priv
.
job_array
.
length
;
i
+=
1
)
{
if
(
priv
.
job_array
[
i
]
!==
job
)
{
new_a
.
push
(
priv
.
job_array
[
i
].
serialized
());
tmp_job_array
.
push
(
priv
.
job_array
[
i
]);
}
}
localstorage
.
setItem
(
priv
.
getJobArrayName
(),
new_a
);
}
};
priv
.
job_array
=
tmp_job_array
;
priv
.
copyJobArrayToLocal
();
};
/**
/**
* Sets the job manager id.
* Removes a job from the current job array.
* @method setId
* @method removeJob
* @param {number} id The id.
* @param {object} job The job object.
*/
*/
that
.
setId
=
function
(
id
)
{
priv
.
removeJob
=
function
(
job
)
{
priv
.
id
=
id
;
var
i
,
};
tmp_job_array
=
[];
for
(
i
=
0
;
i
<
priv
.
job_array
.
length
;
i
+=
1
)
{
if
(
priv
.
job_array
[
i
]
!==
job
)
{
tmp_job_array
.
push
(
priv
.
job_array
[
i
]);
}
}
priv
.
job_array
=
tmp_job_array
;
priv
.
copyJobArrayToLocal
();
};
/**
/**
* Starts listening to the job array, executing them regulary.
* Sets the job manager id.
* @method start
* @method setId
*/
* @param {number} id The id.
that
.
start
=
function
()
{
*/
var
i
;
that
.
setId
=
function
(
id
)
{
if
(
priv
.
interval_id
===
null
)
{
priv
.
id
=
id
;
priv
.
interval_id
=
setInterval
(
function
()
{
};
priv
.
restoreOldJio
();
for
(
i
=
0
;
i
<
priv
.
job_array
.
length
;
i
+=
1
)
{
that
.
execute
(
priv
.
job_array
[
i
]);
}
},
priv
.
interval
);
}
};
/**
/**
* Stops listening to the job arra
y.
* Starts listening to the job array, executing them regular
y.
* @method stop
* @method start
*/
*/
that
.
stop
=
function
()
{
that
.
start
=
function
()
{
if
(
priv
.
interval_id
!==
null
)
{
var
i
;
clearInterval
(
priv
.
interval_id
);
if
(
priv
.
interval_id
===
null
)
{
priv
.
interval_id
=
null
;
priv
.
interval_id
=
setInterval
(
function
()
{
if
(
priv
.
job_array
.
length
===
0
)
{
priv
.
restoreOldJio
();
localstorage
.
removeItem
(
priv
.
getJobArrayName
());
for
(
i
=
0
;
i
<
priv
.
job_array
.
length
;
i
+=
1
)
{
}
that
.
execute
(
priv
.
job_array
[
i
]);
}
}
};
},
priv
.
interval
);
}
};
/**
/**
* Try to restore an the inactive older jio instances.
* Stops listening to the job array.
* It will restore the on going or initial jobs from their job array
* @method stop
* and it will add them to this job array.
*/
* @method restoreOldJio
that
.
stop
=
function
()
{
*/
if
(
priv
.
interval_id
!==
null
)
{
priv
.
restoreOldJio
=
function
()
{
clearInterval
(
priv
.
interval_id
);
var
i
,
jio_id_a
;
priv
.
interval_id
=
null
;
priv
.
lastrestore
=
priv
.
lastrestore
||
0
;
if
(
priv
.
job_array
.
length
===
0
)
{
if
(
priv
.
lastrestore
>
(
Date
.
now
())
-
2000
)
{
return
;
}
localstorage
.
removeItem
(
priv
.
getJobArrayName
());
jio_id_a
=
localstorage
.
getItem
(
'
jio/id_array
'
)
||
[];
}
for
(
i
=
0
;
i
<
jio_id_a
.
length
;
i
+=
1
)
{
}
priv
.
restoreOldJioId
(
jio_id_a
[
i
]);
};
}
priv
.
lastrestore
=
Date
.
now
();
};
/**
/**
* Try to restore an old jio according to an id.
* Try to restore an the inactive older jio instances.
* @method restoreOldJioId
* It will restore the on going or initial jobs from their job array
* @param {number} id The jio id.
* and it will add them to this job array.
*/
* @method restoreOldJio
priv
.
restoreOldJioId
=
function
(
id
)
{
*/
var
jio_date
;
priv
.
restoreOldJio
=
function
()
{
jio_date
=
localstorage
.
getItem
(
'
jio/id/
'
+
id
)
||
0
;
var
i
,
if
(
new
Date
(
jio_date
).
getTime
()
<
(
Date
.
now
()
-
10000
))
{
// 10 sec
jio_id_a
;
priv
.
restoreOldJobFromJioId
(
id
);
priv
.
lastrestore
=
priv
.
lastrestore
||
0
;
priv
.
removeOldJioId
(
id
);
if
(
priv
.
lastrestore
>
(
Date
.
now
())
-
2000
)
{
priv
.
removeJobArrayFromJioId
(
id
);
return
;
}
}
};
jio_id_a
=
localstorage
.
getItem
(
'
jio/id_array
'
)
||
[];
for
(
i
=
0
;
i
<
jio_id_a
.
length
;
i
+=
1
)
{
priv
.
restoreOldJioId
(
jio_id_a
[
i
]);
}
priv
.
lastrestore
=
Date
.
now
();
};
/**
/**
* Try to restore all jobs from another jio according to an id.
* Try to restore an old jio according to an id.
* @method restoreOldJobFromJioId
* @method restoreOldJioId
* @param {number} id The jio id.
* @param {number} id The jio id.
*/
*/
priv
.
restoreOldJobFromJioId
=
function
(
id
)
{
priv
.
restoreOldJioId
=
function
(
id
)
{
var
i
,
jio_job_array
;
var
jio_date
;
jio_job_array
=
localstorage
.
getItem
(
'
jio/job_array/
'
+
id
)
||
[];
jio_date
=
localstorage
.
getItem
(
'
jio/id/
'
+
id
)
||
0
;
for
(
i
=
0
;
i
<
jio_job_array
.
length
;
i
+=
1
)
{
if
(
new
Date
(
jio_date
).
getTime
()
<
(
Date
.
now
()
-
10000
))
{
// 10 sec
var
command_object
=
command
(
jio_job_array
[
i
].
command
);
priv
.
restoreOldJobFromJioId
(
id
);
if
(
command_object
.
canBeRestored
())
{
priv
.
removeOldJioId
(
id
);
that
.
addJob
(
job
(
priv
.
removeJobArrayFromJioId
(
id
);
{
storage
:
that
.
storage
(
jio_job_array
[
i
].
storage
),
}
command
:
command_object
}));
};
}
}
};
/**
/**
* Removes a jio instance according to an id.
* Try to restore all jobs from another jio according to an id.
* @method removeOldJioId
* @method restoreOldJobFromJioId
* @param {number} id The jio id.
* @param {number} id The jio id.
*/
*/
priv
.
removeOldJioId
=
function
(
id
)
{
priv
.
restoreOldJobFromJioId
=
function
(
id
)
{
var
i
,
jio_id_array
,
new_array
=
[];
var
i
,
jio_id_array
=
localstorage
.
getItem
(
'
jio/id_array
'
)
||
[];
command_object
,
for
(
i
=
0
;
i
<
jio_id_array
.
length
;
i
+=
1
)
{
jio_job_array
;
if
(
jio_id_array
[
i
]
!==
id
)
{
jio_job_array
=
localstorage
.
getItem
(
'
jio/job_array/
'
+
id
)
||
[];
new_array
.
push
(
jio_id_array
[
i
]);
for
(
i
=
0
;
i
<
jio_job_array
.
length
;
i
+=
1
)
{
}
command_object
=
command
(
jio_job_array
[
i
].
command
);
}
if
(
command_object
.
canBeRestored
())
{
localstorage
.
setItem
(
'
jio/id_array
'
,
new_array
);
that
.
addJob
(
job
({
localstorage
.
removeItem
(
'
jio/id/
'
+
id
);
storage
:
that
.
storage
(
jio_job_array
[
i
].
storage
),
};
command
:
command_object
}));
/**
}
* Removes a job array from a jio instance according to an id.
}
* @method removeJobArrayFromJioId
};
* @param {number} id The jio id.
/**
*/
* Removes a jio instance according to an id.
priv
.
removeJobArrayFromJioId
=
function
(
id
)
{
* @method removeOldJioId
localstorage
.
removeItem
(
'
jio/job_array/
'
+
id
);
* @param {number} id The jio id.
};
*/
priv
.
removeOldJioId
=
function
(
id
)
{
/**
var
i
,
* Executes a job.
jio_id_array
,
* @method execute
new_array
=
[];
* @param {object} job The job object.
jio_id_array
=
localstorage
.
getItem
(
'
jio/id_array
'
)
||
[];
*/
for
(
i
=
0
;
i
<
jio_id_array
.
length
;
i
+=
1
)
{
that
.
execute
=
function
(
job
)
{
if
(
jio_id_array
[
i
]
!==
id
)
{
try
{
new_array
.
push
(
jio_id_array
[
i
]);
job
.
execute
();
}
}
catch
(
e
)
{
}
switch
(
e
.
name
)
{
localstorage
.
setItem
(
'
jio/id_array
'
,
new_array
);
case
'
jobNotReadyException
'
:
break
;
// do nothing
localstorage
.
removeItem
(
'
jio/id/
'
+
id
);
case
'
tooMuchTriesJobException
'
:
break
;
// do nothing
};
default
:
throw
e
;
/**
}
* Removes a job array from a jio instance according to an id.
}
* @method removeJobArrayFromJioId
* @param {number} id The jio id.
*/
priv
.
removeJobArrayFromJioId
=
function
(
id
)
{
localstorage
.
removeItem
(
'
jio/job_array/
'
+
id
);
};
/**
* Executes a job.
* @method execute
* @param {object} job The job object.
*/
that
.
execute
=
function
(
job
)
{
try
{
job
.
execute
();
}
catch
(
e
)
{
switch
(
e
.
name
)
{
case
'
jobNotReadyException
'
:
break
;
// do nothing
case
'
tooMuchTriesJobException
'
:
break
;
// do nothing
default
:
throw
e
;
}
}
priv
.
copyJobArrayToLocal
();
};
/**
* Checks if a job exists in the job array according to a job id.
* @method jobIdExists
* @param {number} id The job id.
* @return {boolean} true if exists, else false.
*/
that
.
jobIdExists
=
function
(
id
)
{
var
i
;
for
(
i
=
0
;
i
<
priv
.
job_array
.
length
;
i
+=
1
)
{
if
(
priv
.
job_array
[
i
].
getId
()
===
id
)
{
return
true
;
}
}
return
false
;
};
/**
* Terminate a job. It only remove it from the job array.
* @method terminateJob
* @param {object} job The job object
*/
that
.
terminateJob
=
function
(
job
)
{
priv
.
removeJob
(
job
);
};
/**
* Adds a job to the current job array.
* @method addJob
* @param {object} job The new job.
*/
that
.
addJob
=
function
(
job
)
{
var
result_array
=
that
.
validateJobAccordingToJobList
(
priv
.
job_array
,
job
);
priv
.
appendJob
(
job
,
result_array
);
};
/**
* Generate a result array containing action string to do with the good job.
* @method validateJobAccordingToJobList
* @param {array} job_array A job array.
* @param {object} job The new job to compare with.
* @return {array} A result array.
*/
that
.
validateJobAccordingToJobList
=
function
(
job_array
,
job
)
{
var
i
,
result_array
=
[];
for
(
i
=
0
;
i
<
job_array
.
length
;
i
+=
1
)
{
result_array
.
push
(
jobRules
.
validateJobAccordingToJob
(
job_array
[
i
],
job
));
}
return
result_array
;
};
/**
* It will manage the job in order to know what to do thanks to a result
* array. The new job can be added to the job array, but it can also be
* not accepted. It is this method which can tells jobs to wait for another
* one, to replace one or to eliminate some while browsing.
* @method appendJob
* @param {object} job The job to append.
* @param {array} result_array The result array.
*/
priv
.
appendJob
=
function
(
job
,
result_array
)
{
var
i
;
if
(
priv
.
job_array
.
length
!==
result_array
.
length
)
{
throw
new
RangeError
(
"
Array out of bound
"
);
}
for
(
i
=
0
;
i
<
result_array
.
length
;
i
+=
1
)
{
if
(
result_array
[
i
].
action
===
'
dont accept
'
)
{
return
job
.
notAccepted
();
}
}
for
(
i
=
0
;
i
<
result_array
.
length
;
i
+=
1
)
{
switch
(
result_array
[
i
].
action
)
{
case
'
eliminate
'
:
result_array
[
i
].
job
.
eliminated
();
priv
.
removeJob
(
result_array
[
i
].
job
);
break
;
case
'
update
'
:
result_array
[
i
].
job
.
update
(
job
);
priv
.
copyJobArrayToLocal
();
priv
.
copyJobArrayToLocal
();
};
return
;
case
'
wait
'
:
/**
job
.
waitForJob
(
result_array
[
i
].
job
);
* Checks if a job exists in the job array according to a job id.
break
;
* @method jobIdExists
default
:
* @param {number} id The job id.
break
;
* @return {boolean} true if exists, else false.
}
*/
}
that
.
jobIdExists
=
function
(
id
)
{
priv
.
job_array
.
push
(
job
);
var
i
;
priv
.
copyJobArrayToLocal
();
for
(
i
=
0
;
i
<
priv
.
job_array
.
length
;
i
+=
1
)
{
};
if
(
priv
.
job_array
[
i
].
getId
()
===
id
)
{
that
.
serialized
=
function
()
{
return
true
;
var
a
=
[],
}
i
,
}
job_array
=
priv
.
job_array
||
[];
return
false
;
for
(
i
=
0
;
i
<
job_array
.
length
;
i
+=
1
)
{
};
a
.
push
(
job_array
[
i
].
serialized
());
}
/**
return
a
;
* Terminate a job. It only remove it from the job array.
};
* @method terminateJob
return
that
;
* @param {object} job The job object
}());
*/
\ No newline at end of file
that
.
terminateJob
=
function
(
job
)
{
priv
.
removeJob
(
job
);
};
/**
* Adds a job to the current job array.
* @method addJob
* @param {object} job The new job.
*/
that
.
addJob
=
function
(
job
)
{
var
result_array
=
that
.
validateJobAccordingToJobList
(
priv
.
job_array
,
job
);
priv
.
appendJob
(
job
,
result_array
);
};
/**
* Generate a result array containing action string to do with the good job.
* @method validateJobAccordingToJobList
* @param {array} job_array A job array.
* @param {object} job The new job to compare with.
* @return {array} A result array.
*/
that
.
validateJobAccordingToJobList
=
function
(
job_array
,
job
)
{
var
i
,
result_array
=
[];
for
(
i
=
0
;
i
<
job_array
.
length
;
i
+=
1
)
{
result_array
.
push
(
jobRules
.
validateJobAccordingToJob
(
job_array
[
i
],
job
));
}
return
result_array
;
};
/**
* It will manage the job in order to know what to do thanks to a result
* array. The new job can be added to the job array, but it can also be
* not accepted. It is this method which can tells jobs to wait for another
* one, to replace one or to eliminate some while browsing.
* @method appendJob
* @param {object} job The job to append.
* @param {array} result_array The result array.
*/
priv
.
appendJob
=
function
(
job
,
result_array
)
{
var
i
;
if
(
priv
.
job_array
.
length
!==
result_array
.
length
)
{
throw
new
RangeError
(
"
Array out of bound
"
);
}
for
(
i
=
0
;
i
<
result_array
.
length
;
i
+=
1
)
{
if
(
result_array
[
i
].
action
===
'
dont accept
'
)
{
return
job
.
notAccepted
();
}
}
for
(
i
=
0
;
i
<
result_array
.
length
;
i
+=
1
)
{
switch
(
result_array
[
i
].
action
)
{
case
'
eliminate
'
:
result_array
[
i
].
job
.
eliminated
();
priv
.
removeJob
(
result_array
[
i
].
job
);
break
;
case
'
update
'
:
result_array
[
i
].
job
.
update
(
job
);
priv
.
copyJobArrayToLocal
();
return
;
case
'
wait
'
:
job
.
waitForJob
(
result_array
[
i
].
job
);
break
;
default
:
break
;
}
}
priv
.
job_array
.
push
(
job
);
priv
.
copyJobArrayToLocal
();
};
that
.
serialized
=
function
()
{
var
a
=
[],
i
,
job_array
=
priv
.
job_array
||
[];
for
(
i
=
0
;
i
<
job_array
.
length
;
i
+=
1
)
{
a
.
push
(
job_array
[
i
].
serialized
());
}
return
a
;
};
return
that
;
}());
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment