-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
144 additions
and
21 deletions.
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,21 @@ | ||
function wait(ms) { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
addEventListener("scheduled", (event) => { | ||
event.waitUntil(handleSchedule(event.scheduledTime)); | ||
}); | ||
|
||
async function handleSchedule(scheduledDate) { | ||
console.log( | ||
"Called scheduled event:", | ||
scheduledDate, | ||
new Date(scheduledDate).toISOString() | ||
); | ||
|
||
const res = await fetch("https://example.workers.rocks/data.json"); | ||
|
||
console.log("Done waiting!", res.status, await res.json()); | ||
|
||
return "Called deploy hook!"; | ||
} |
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,42 @@ | ||
use log::debug; | ||
use log::error; | ||
use openworkers_runtime::run_js; | ||
use openworkers_runtime::AnyError; | ||
use openworkers_runtime::Task; | ||
use tokio::sync::oneshot; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), ()> { | ||
if !std::env::var("RUST_LOG").is_ok() { | ||
std::env::set_var("RUST_LOG", "debug"); | ||
} | ||
|
||
env_logger::init(); | ||
|
||
debug!("start main"); | ||
|
||
let file_path = String::from("examples/scheduled.js"); | ||
|
||
let (shutdown_tx, shutdown_rx) = oneshot::channel::<Option<AnyError>>(); | ||
|
||
let _res = { | ||
let file_path = file_path.clone(); | ||
|
||
std::thread::spawn(move || run_js(file_path.as_str(), Task::Scheduled, shutdown_tx)) | ||
}; | ||
|
||
debug!("js worker for {:?} started", file_path); | ||
|
||
// wait for shutdown signal | ||
match shutdown_rx.await { | ||
Ok(None) => debug!("js worker for {:?} stopped", file_path), | ||
Ok(Some(err)) => { | ||
error!("js worker for {:?} error: {}", file_path, err); | ||
} | ||
Err(err) => { | ||
error!("js worker for {:?} error: {}", file_path, err); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
File renamed without changes.
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
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
mod ext; | ||
|
||
mod runtime; | ||
mod task; | ||
|
||
pub use runtime::run_js; | ||
pub use ext::FetchInit; | ||
pub use deno_core::error::AnyError; | ||
pub use task::Task; | ||
pub use task::TaskType; |
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,32 @@ | ||
use crate::FetchInit; | ||
|
||
pub enum TaskType { | ||
Fetch, | ||
Scheduled, | ||
None, | ||
} | ||
|
||
impl TaskType { | ||
pub fn is_none(&self) -> bool { | ||
match self { | ||
TaskType::None => true, | ||
_ => false, | ||
} | ||
} | ||
} | ||
|
||
pub enum Task { | ||
Fetch(FetchInit), | ||
Scheduled, | ||
None, | ||
} | ||
|
||
impl Task { | ||
pub fn task_type(&self) -> TaskType { | ||
match self { | ||
Task::Fetch(_) => TaskType::Fetch, | ||
Task::Scheduled => TaskType::Scheduled, | ||
Task::None => TaskType::None, | ||
} | ||
} | ||
} |